Z
Zark3
Hi all,
I was wondering if anybody could enlighten me on the possibility of
dynamic casting. Or, well, whether or not I'm actually trying to do
this the right way.
What I have is a base class that several classes inherit from. From
time to time I need to add new inherited types so I want something that
is as versatile as possible, preferably not requiring naming all
subtypes in a switch statement.
What I want is to call a function (that every class that inherits from
the base type implents differently) but without knowing the specific
objects' types. I.e. I want to be able to (from a for loop) call this
function for every pet in a pet store without separating dogs from cats
in different arrays, but keeping them in an array of the base type.
A code snippet would look roughly as follows:
private string GetAnimalSound(string animal_name) { // This function is
in the program, not the base class or anything
BaseAnimal x = null;
switch(animal_name) {
case "dog": x = new Dog(); break;
case "cat": x = new Cat(); break;
}
x.AnimalType = "Pet"; // Set different properties that each type
implements
return x.ReturnSound(); // defined as abstract in base class,
different implementation per inherited class
}
Now, adding support for new animals requires me to update the switch
statements for each function in the program, which I find sub-optimal.
Therefore I tried to cancel the switch out into something like:
x = (Type.GetType(animal_name))Activator.CreateInstance(null,
animal_name).Unwrap();
which unfortunately does not work. Without the cast it will at least
compile, but I'm left with a System.Object instead of a MyNamespace.Dog
or Cat etc.
My hunch is that what I need is dynamic casting, but I could of course
be wrong. Who can help me out here and advise me on how to achieve this
goal?
Thanks in advance,
Chris
I was wondering if anybody could enlighten me on the possibility of
dynamic casting. Or, well, whether or not I'm actually trying to do
this the right way.
What I have is a base class that several classes inherit from. From
time to time I need to add new inherited types so I want something that
is as versatile as possible, preferably not requiring naming all
subtypes in a switch statement.
What I want is to call a function (that every class that inherits from
the base type implents differently) but without knowing the specific
objects' types. I.e. I want to be able to (from a for loop) call this
function for every pet in a pet store without separating dogs from cats
in different arrays, but keeping them in an array of the base type.
A code snippet would look roughly as follows:
private string GetAnimalSound(string animal_name) { // This function is
in the program, not the base class or anything
BaseAnimal x = null;
switch(animal_name) {
case "dog": x = new Dog(); break;
case "cat": x = new Cat(); break;
}
x.AnimalType = "Pet"; // Set different properties that each type
implements
return x.ReturnSound(); // defined as abstract in base class,
different implementation per inherited class
}
Now, adding support for new animals requires me to update the switch
statements for each function in the program, which I find sub-optimal.
Therefore I tried to cancel the switch out into something like:
x = (Type.GetType(animal_name))Activator.CreateInstance(null,
animal_name).Unwrap();
which unfortunately does not work. Without the cast it will at least
compile, but I'm left with a System.Object instead of a MyNamespace.Dog
or Cat etc.
My hunch is that what I need is dynamic casting, but I could of course
be wrong. Who can help me out here and advise me on how to achieve this
goal?
Thanks in advance,
Chris