Solved

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

Posted on 2004-09-23
4
381 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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