473,382 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 2262
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cengiz Ulku | last post by:
Hi all, I know now how to change the font name of a RTF file in a RTB control using, for example: RTextBox1.SelFontName = cmbFonts.Text I have a RTF file -created with Word- displayed in a...
1
by: Tom Rahav | last post by:
Hello. I use visual basic .net 2003 and Crystal Reports for creating win-reports. My question is if there is any possibility to set font properties (type, color and size) of a text control or...
4
by: Kasper Heftholm Kristensen | last post by:
Hi. I want to change the used namespace at runtime depending on another variable, but I'm not sure how to does this, if possible. The code below will not compile, but I hope it gives you an...
7
by: Stefan Finzel | last post by:
Hi, is there a way to change the display property on Windows Mobile 2003 SE Mobile/Pocket Internet Explorer? See following example. Please note: visibilty property has the same problem. Is...
1
by: Geoff Jones | last post by:
Hi Is it possible to change the type of data a column holds in DataTable at runtime? For example, suppose that the table column originally holds Strings, can we, at runtime, change it to...
2
by: tfsimes | last post by:
Hi, I am a long time ASP developer learning .NET, so please bear with me. I am trying to find an article or such that will help me understand how to change control properties at runtime based on...
5
by: Kimmo Laine | last post by:
Hi is there a way to change propertys attribute from the code? Letīs say that i have the following property in my class: public int Count } Is there a way to change the displayname, from...
0
by: jwallis | last post by:
I have two systems, one for 'Test' and one for 'Live' and each has a Sql Server. When 'Test' code is published to 'Live' I currently have to edit connection strings in web.config to ensure that...
17
by: blufox | last post by:
Hi All, Can i change the execution path of methods in my process at runtime? e.g a()->b()->c()->d()->e() Now, i want execution to be altered at runtime as -
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.