Passing extended object information...

Posted on 2006-05-08
Last Modified: 2010-04-16
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.

Question by:collages
    LVL 12

    Expert Comment


     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 ...

    LVL 12

    Accepted Solution


    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 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

    11 Experts available now in Live!

    Get 1:1 Help Now