Re: Simple Inheritance : Creating derived classes from instances of base class.



<nyathancha@xxxxxxxxxxx> wrote in message news:1169170595.113603.79560@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

I would create a copy constructor in the base Animal class that takes in an Animal and sets all the data in 'this' with the values from the passed in instance of Animal.

public Animal(Animal copy)
{
this.x = Animal.GetX();
// etc
}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}

Then add a constructor to TaggedAnimal that takes in an Animal.

public TaggedAnimal(Animal an) : base (an)

In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);

In here, for each instance of Animal in allAnimals you would create a TaggedAnimal, passing in the Animal to the constructor.

/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(allTaggedAnimals);

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

You can't assign to the base instance like this. You could assign properties in base based on the same property value in currentAnimal, or add the copy constructor to Animal as I have stated above.

Public string GetTag()
{/**/}

}


but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

You can't. Once you are within your derived classes constructor, your base instance is already created. You can't assign a new base instance.
--
Tom Porterfield

.



Relevant Pages

  • Re: Simple Inheritance : Creating derived classes from instances of base class.
    ... always able to control/create all the different constructors the base ... Then add a constructor to TaggedAnimal that takes in an Animal. ... I would like to convert them to TaggedAnimals and start ... /* Animals get constructed here, I don't have much control here and ...
    (microsoft.public.dotnet.languages.csharp)
  • Simple Inheritance : Creating derived classes from instances of base class.
    ... base class, essentially wrapping up an existing base class with some ... always able to control/create all the different constructors the base ... /* Animals get constructed here, I don't have much control here and ... creating the derived class to begin with. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Can Java Programmer Learn C++ Quickly?
    ... >>whose constructor has not even started yet. ... > two different animals, if that's what you're referring to. ... virtual function from the constructor, you are very likely to end up in ...
    (comp.lang.java.programmer)
  • Re: Very Confused on Page 33
    ... > base class you can also send to objects of the derived class. ... since many animals don't like to be milked. ... > If anyone can help get me past this confusion I'd appreciate it. ...
    (comp.lang.java.help)

Loading