Solved

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

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WKHTMLTOPDF - --disable-smart-shrinking not working 10 88
Filename to be appended with DateTimeof Download 3 55
Device Location 4 29
C# guarantee sql connection close 6 39
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

770 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