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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:05:53+00:00 2026-06-05T23:05:53+00:00

I have a class Field of which there are two sub-classes AccountField and DecimalField

  • 0

I have a class Field of which there are two sub-classes AccountField and DecimalField.

ie

class Field{//some properties} 

class DecimalField : Field {...}

class AccountField : Field {...}

I then have another class Data which has a member property of type Field.

But it has problems. If I assign a DecimalField variable to the Field property within Data then I cannot use some properties of DecimalFields and also same for Accountfield variables…

What must I do? Which pattern I must use?

I am using C# 4.0 and MS Studio 2010

  • 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-05T23:05:55+00:00Added an answer on June 5, 2026 at 11:05 pm

    Yes you can.

    Let’s say that you have your field class like this

    public class Field
    {
       public string Property1{get;set;}
       public int Property2 {get;set;}
    }
    

    and the decimal field class like this

      public class DecimalField:Field
       {
        public int DecimalFieldProperty {get;set;}
    }
    

    ,and the Accountfield like this

    public class AccountField:Field
        {
           public double Balance{get;set;}
    }
    

    , and suppose you have a method called DoSomething, which expects a parameter of Field type like the one below

    void DoSomething(Field field)
    {}
    

    Then to get the concrete type passed in, you can do something like

        void DoSomething(Field field)
        {
            var decimalField = field as DecimalField;
    
            //Do something with the decimal field instance
            if(decimalField  !=null)
             {
               Console.WriteLine("Decimal Field Property {0}",decimalField .DecimalFieldProperty );
              return;
    
            }
    
            //Cast the field as a account field instance
          var accountField = field as AccountField;
           if(accountField !=null)
           {
             Console.WriteLine("Balance {0}", accountField.Balance );
    
            return;
          }
    
          //Do something else with the normal field
        Console.WriteLine("Normal Field");
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes: the parent class A and some subclass B, which extends
I have an Item class. There's an itemType field inside of that class which
I have a class User with one field called birthDate which is a java.sql.Date
I have a Customer class which has a representative field....this field is initially blank
I have a Data class with one field, price. I referenced the price field
I have a script which contains two classes. (I'm obviously deleting a lot of
I have the following problem: 1) There is some abstract class A with several
I have a class with a template parameter which should decide which of two
In my discussion board I have a Post class. There are two special functions,
I have a Person class with two properties: name and address . I want

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.