Adam.... You cannot really change an objects type, but you can create a reference of type dog or cat.
So objects have class and references (reference variables) have type. In pseudocode:
if (referenceV is dog)
safe to do dogReferenceVariable= (dog)referenceV
else if (referenceV is cat)
safe to do catReferenceVariable= (cat)referenceV
Whenever you see a "switch" like this, a light should go off and you should think is there a better way?
So class abstract animal has virtual method Voice
class cat extends from animal and overrides Voice to Meow
class dog extends from animal and overrides Voice to Bark
This way there is no need to know the concrete class at runtime, just do
referenceV.Voice()
which gets a Meow a Bark or even a Hiss :)
Hope that helps,
Jeff
How can I change an object's type at runtime?
For example, here's what I want to do in psedocode:
object animal;
if (dog)
{
animal=(dog)animal;
}
else
{
animal=(cat)animal;
}
Thanks! <
************************************************** ********************
Sent via Fuzzy Software @
http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...