Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

17 Experts available now in Live!

Get 1:1 Help Now