Passing extended object information...

If i have an object and then another object that extends that original object...
and I want to pass an object i already have loaded and set with information to a new extended version of that object....
what would be the easiest way for me me to do that?  
Here's what i'm doing now....


public static main()
{
   BaseObject b1 = new BaseObject("MyObject");
   ExtendedObject e1 = new ExtendedObject( b1 );
}


class BaseObject
{
  private string Name = "";
  public string Field1 = "";
  public string Field2 = "";
  public string Field3 = "";
  public string Field4 = "";
  public string Field5 = "";
  public baseObject (string strName)
  {
    this.Name = strName;
  }
}

class ExtendedObject : BaseObject
{
  public extendedObject( baseObject baseobj )
  {
    this.Name = baseobj.Name;
    this.Field1 = baseobj.Field1;
    //etc.. you get the point.
  }
}


LVL 1
collagesAsked:
Who is Participating?
 
sumixCommented:

Sorry collages, I gave you a wrong answer!
you cannot have an implicit conversion like
  ExtendedObject e1 = b1;
 only backwards...

Even though you may have a constructor like the one I indicated, for what you try to do, your solution (constructor that takes a parameter of base class) is a good one.

regards.
0
 
sumixCommented:

 What you tried to do with a constructor for ExtendedObject that takes a parameter of BaseObject type is achieved with
    BaseObject b1 = new BaseObject("MyObject");
    ExtendedObject e1 = b1;
  you don't need that type of constructor.

Instead, you may have a constructor for ExtendedObject that calls the base class constructor with the same parameters like

  public extendedObject(string strName) : base(strName)
  {
    // you get the point ...
  }

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.