Solved

How do you set derived props in addition to base props?

Posted on 2004-09-23
4
379 Views
Last Modified: 2010-04-15
Hi all. I have a BaseClass with some properties that is common to all my class:

class BaseClass
{
  string BaseProperty
  {
    get{...;}
    set{...;}
  }

  protected static BaseClass<not sure if this is correct return type> ParseLine(string CurrentLine)
  {
    BaseClass baseClass = new BaseClass();
    baseClass.BaseProperty = "some value";
    return baseClass;
  }
}

And then a derived class with additional props, and a method that parses a file specific for this class:

class DerivedClass : BaseClass
{
  string SomeOtherProp
  {
    get{...;}
    set{...;}
  }

  public void SetFields(string FileName)
  {
    //Within FileName resides fields that are common to all base classes
    //It also has additional fields specific to this class
    //I need to be able to set the common fields in the base class, but also continue setting props for the derived class
   
    //this would be where I open the file and get the current line
   
    //Next step -- I think
    BaseClass someClass = BaseClass.ParseLine(currentLine);
   
    //NOW I want to be able to set the additional properties in this derived class but still keep the properties that were set in the ParseLine above
    //How the heck do I do it?    
  }
}
0
Comment
Question by:Junoti
4 Comments
 
LVL 10

Accepted Solution

by:
eternal_21 earned 500 total points
ID: 12138698
I have thrown together a similar structure to what you have described, which demonstrated TWO ways to accomplish this.  You can do it through constructor chaining, or by calling virtual or base methods:

    public static void Main(string[] args)
    {
      DerivedClass dClass = new DerivedClass("BaseProp,DerProp");
     
      Console.WriteLine(dClass.BaseProperty); //"BaseProp"
      Console.WriteLine(dClass.DerivedProperty); //"DerProp"

      dClass.ResetProperties("xProp,yProp");

      Console.WriteLine(dClass.BaseProperty); //"xProp"
      Console.WriteLine(dClass.DerivedProperty); //"yProp"
    }

    abstract class BaseClass
    {
      string baseProperty;
      public string BaseProperty
      {
        get { return baseProperty; }
      }

      protected BaseClass(string propertyString)
      {
        baseProperty = propertyString.Split(',')[0];
      }

      public virtual void ResetProperties(string propertyString)
      {
        baseProperty = propertyString.Split(',')[0];
      }
    }

    class DerivedClass: BaseClass
    {
      string derivedProperty;
      public string DerivedProperty
      {
        get { return derivedProperty; }
      }

      public DerivedClass(string propertyString): base(propertyString)
      {
        derivedProperty = propertyString.Split(',')[1];
      }

      public override void ResetProperties(string propertyString)
      {
        base.ResetProperties(propertyString);
        derivedProperty = propertyString.Split(',')[1];
      }
    }
0
 
LVL 10

Expert Comment

by:123654789987
ID: 12168539
If u want to set the derived properties in addition to the base properties. Then u have to create an instance of the derived class. This will show all the properties - ones of the base cass and that of the derived class
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now