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 7087369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:39:57+00:00 2026-05-28T07:39:57+00:00

I have a data object which has a base class with three derived classes,

  • 0

I have a data object which has a base class with three derived classes,

public enum ModelType
{
   A,
   B, 
   C
}
public abstract class Parent
{
   protected ModelType type; 
}

public class ChildA: Parent
{
    private new ModelType type = ModelType.A;
}

public class ChildB: Parent

public class ChildC: Parent

The same type field is in the other 2 children with corresponding ModelTypes.

I have another layer between the forms that has some higher level additional information about the data object and an instance of Parent(). I am trying to simplify the problem a lot so I apologise if I am not being clear and have not given enough information.

public enum UIModelType
{
   A,
   B, 
   C,
   None
}
public class DataObject
{
    private Parent someData;
    private UIModelType type;
}

The fields have getter and setter properties.

The UI only communicates with the DataObject class and cannot see the lower level someData object.

Now at some point during the UI (which is a wizard to fill in the information in the data objects), the user can select A, B or C. My problem is how to communicate this information without too much code repetition. Right now I have an enum between the UI and DataObject class. So if user selects A, it assigns a data type as A using the enum. The DataObject class now instantiates the someData object with ChildA(). The problem is to communicate between the DataObject and someData object I have another enum with A, B and C to communicate the type.

Would it be healthier to use typeof instead, although I have read that that is not the best way to go.

I need to do many equality checks with the data type (ABC) from the UI all the way to the lower level data object and hence I had thought enums is the fastest way but it doesn’t feel right to have so many enums of the same type in different classes. Am I missing something very obvious 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-05-28T07:39:58+00:00Added an answer on May 28, 2026 at 7:39 am

    Rather than using typeof or enum, you could use is.

    private Parent someData = new ChildA();
    
    if (someData is Parent) { } // Returns true.
    if (someData is ChildA) { } // Returns true.
    if (someData is ChildB) { } // Returns false.
    

    However, if I understand your question correctly, it sounds like you’re doing these checks because you need to continually downcast the someData object to provide functionality for the DataObject class that depends on the type of someData. If this is the case, you might want to consider refactoring your code, as repeated downcasting can can make code difficult to read and defies some traditional concepts of object-oriented programming.

    Edit:

    Ok, I think I understand your question better, now. You’re looking for a better way of creating children of the Parent object (i.e. using the factory pattern) than by using an enum.

    You could create the instance dynamically based on the type name:

    var typeName = GetTheTypeTheUserWants();
    var type = System.Type.GetType(typeName, false, true);
    if (type == null) throw new Exception();
    
    var someData = Activator.CreateInstance(type) as Parent;
    if (someData == null) throw new Exception();
    

    Using an enum might be easier, but this method allows you to create new subclasses of Parent without updating your enum.

    If I still missed the point of your question, sorry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Abstract class called Function which has 2 inheriting classes (actually 6,
I have a fairly involved managed data model which has a central object with
I have two classes: a base class, Foo::Base and a derived class, Foo::Base::Sub .
I have a base class called SpriteSheet which has two children, RotatedSpriteSheet and FlippedSpriteSheet.
HI all, I created a .net object (ex: A ) which contain data have
I have a model which gets its data from a parser object. I'm thinking
In my application, I have three collection objects which store data. The data which
I have a data object with three fields, A, B and C. The problem
I have a data object -- a custom class called Notification -- that exposes
I have two arrays of System.Data.DataRow objects which I want to compare. The rows

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.