Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7838093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:58:36+00:00 2026-06-02T14:58:36+00:00

I have two classes (this is C#) that are very similar except they each

  • 0

I have two classes (this is C#) that are very similar except they each contain their own nested class and enum.

I would like to refactor them to both inherit from a single abstract class, but I’m running into a problem because the methods are all tightly coupled to the nested class types.

My first plan was to pull out the ItemDetails Class, but it is linked to the ItemType, which is an enum that is specific to each view item class. Further, I can’t just use System.Enum as the type since I need to be able to serialize the details to an xml file.

How could I reduce the duplication within these classes?

public class FirstViewItem
{
    [Serializable]
    public class ItemDetails
    {
        public ItemType Type;
        public int Width;
        public string Text;
        public int DisplayOrder;
    }

    public enum ItemType
    {
        None = 0,
        A,
        B,
        C
    }

    public FirstViewItem()
    {
        // ...
    }

    public List<ItemDetails>()
    {
        // code here ...
    }
}

public class SecondViewItem
{
    [Serializable]
    public class ItemDetails
    {
        public ItemType Type;
        public int Width;
        public string Text;
        public int DisplayOrder;
    }

    public enum ItemType
    {
        None = 0,
        X,
        Y,
        X
    }

    public SecondViewItem()
    {
        // ...
    }

    public List<ItemDetails>()
    {
        // code here ...
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:58:37+00:00Added an answer on June 2, 2026 at 2:58 pm

    You want to make a generic class that is dependent on the item type enum being passed in:

    public class ViewItem<T>
    {
        [Serializable]
        public class ItemDetails
        {
            public T Type; // the generic type is inserted here
            public int Width;
            public string Text;
            public int DisplayOrder;
        }
    
        // common code that uses ItemDetails
    }
    

    Then some item types:

    public enum FirstItemType
    {
        None = 0,
        A,
        B,
        C
    }
    
    public enum SecondItemType
    {
        None = 0,
        X,
        Y,
        Z
    }
    

    Then usage:

    var firstViewItem = new ViewItem<FirstItemType>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two very similar classes that do essentially the same thing. The only
my code is like this: i have two classes first class: public class Box<E>
I have two classes that refer to each other, but obviously the compiler complains.
I have a very simple composite model made of two classes: Public Class ParentModelVM
I'm new to C++, so this question may be basic: I have two classes
I have a one-to-many relationship between two classes for this situation. I have a
having this two classes I always have a doubt of which is the best
I have two classes generated by LINQ2SQL both from the same table so they
I have two classes that I would like to merge into a composite. These
I have two classes in a PHP application, B extending A for example. Class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.