470,631 Members | 1,621 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

Changing a type at runtime?

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!
Nov 16 '05 #1
4 2121
Are you trying to create a new instance of Dog or Cat and assign it to
animal or is animal already set and you're trying to determine its type?

"AdamM" <no****@nospam.com> wrote in message
news:g-********************@comcast.com...
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!

Nov 16 '05 #2
I already have instances of dog or cat, and I want the generic object to
become a reference to either at runtime, then be able to use their methods.
I keep getting the errors that "object animal has no method bark, or has no
method meow" when I try to use the methods.

Problem is I don't know whether I got a dog or cat instance until runtime,
thus I am trying to make use of a generic object to stand in their place, so
my code isn't a million if-then statements trying to determine whether I got
a dog or cat. I'm trying to reuse code better.

Does that make a bit more sense?

Thanks!


"Dave Fancher" <ei*****@comcast.net> wrote in message
news:rt********************@comcast.com...
Are you trying to create a new instance of Dog or Cat and assign it to
animal or is animal already set and you're trying to determine its type?

"AdamM" <no****@nospam.com> wrote in message
news:g-********************@comcast.com...
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!


Nov 16 '05 #3
Take a look at my blog. I actually just posted an article about this very
subject (coincidentally using animal, cat, and dog).
http://davefancher.blogspot.com/ The article titled "Inheritance,
Polymorphism, and Casting" may be a little more detailed than you're looking
for but it covers exactly what you're trying to do. In particular, I'll
direct your attention to the "IS" keyword.

HTH

"AdamM" <no****@nospam.com> wrote in message
news:8-********************@comcast.com...
I already have instances of dog or cat, and I want the generic object to
become a reference to either at runtime, then be able to use their methods.
I keep getting the errors that "object animal has no method bark, or has no
method meow" when I try to use the methods.

Problem is I don't know whether I got a dog or cat instance until runtime,
thus I am trying to make use of a generic object to stand in their place,
so my code isn't a million if-then statements trying to determine whether
I got a dog or cat. I'm trying to reuse code better.

Does that make a bit more sense?

Thanks!


"Dave Fancher" <ei*****@comcast.net> wrote in message
news:rt********************@comcast.com...
Are you trying to create a new instance of Dog or Cat and assign it to
animal or is animal already set and you're trying to determine its type?

"AdamM" <no****@nospam.com> wrote in message
news:g-********************@comcast.com...
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!



Nov 16 '05 #4
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...
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Cengiz Ulku | last post: by
4 posts views Thread by Kasper Heftholm Kristensen | last post: by
7 posts views Thread by Stefan Finzel | last post: by
2 posts views Thread by tfsimes | last post: by
5 posts views Thread by Kimmo Laine | last post: by
reply views Thread by jwallis | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.