?
Solved

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

Posted on 2004-09-23
4
Medium Priority
?
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 10

Accepted Solution

by:
eternal_21 earned 1500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 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