N
nyathancha
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)
{/**/}
}
/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal
{
Public string GetTag()
{/**/}
}
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);
/* 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;
}
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.
Any Help is much appreciated.
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)
{/**/}
}
/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal
{
Public string GetTag()
{/**/}
}
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);
/* 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;
}
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.
Any Help is much appreciated.