|
-
Apr 26th, 2006, 08:32 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Typecasting and how to make use of it real world
Fellow C# erper's,
Please bear with me for asking all sorts of basic questions. In another 2 to 3 weeks I should be up to speed on at least most of the fundamental concepts and will actually be developing code..
I am aware that, we can typecast from one type variable (either value type variable or reference type variable) to another type.
I have 2 questions about casting:
Question 1:
I am trying out the following example and I am getting this error "cannont convert type 'int' to 'string'
Code:
int x = 5;
string abc = "hello";
abc = (string)x;
abc variable is a reference type variable (string) and x is a value type. why can't cast int value type to string type? doesn't (string) do the type casting?
Is there a list some where that lists all the possible type casts?
Question 2
suppose I have 2 derived classes. say "CommercialPilot" and "AirforcePilot" and both derive from a base class called "Pilot".
Can't I type cast one objects from one derived class to another without any issues? (I mean "commericialpilot" object to "airforcepilot" object)
thanks
nath
-
Apr 26th, 2006, 08:37 AM
#2
Re: Typecasting and how to make use of it real world
1. String/int are not cross-castable types. Use the ToString() method to convert an number/object to a string representation of it, and use Convert.ToInt32 and related methods to convert strings to numbers.
2. No. You can cast up and down the inheritance chain (Pilot to CommercialPilot/AirforcePilot and vice versa) but not sideways.
-
Apr 26th, 2006, 08:45 AM
#3
Thread Starter
Hyperactive Member
Re: Typecasting and how to make use of it real world
thanks for the response...
if string and int are not cross-castable types, how come ConvertTo and Tostring are able to convert between them??
thanks
-
Apr 26th, 2006, 08:48 AM
#4
Re: Typecasting and how to make use of it real world
Conversion does not have the same meaning as casting. A typecast operation forces one object into another type, which is possible in certain situations. The ToString member of a numeric type converts the number to a string representation, it is not a cast operation. Likewise, the ConvertTo.* methods use their own algorithms to get the intended result.
-
Apr 26th, 2006, 09:11 AM
#5
Thread Starter
Hyperactive Member
Re: Typecasting and how to make use of it real world
thank you very much
regards
nath
-
Apr 26th, 2006, 05:44 PM
#6
Re: [RESOLVED] Typecasting and how to make use of it real world
Casting creates a new variable of a different type but it does not affect the underlying object. For this reason the underlying object must already be of the type that you are casting too. If you add a string variable to an ArrayList and then retrieve it you get it back as an Object reference. The underlying object is a string though so you can cast it as a string. "Conversion" actually creates a new object that is the equivalent representation of the existing object in a different type. The lines are blurred a bit with value type objects because the variable contains the object, so creating a new variable actually does create a new object. With reference types they are not the same thing though.
Think about real world examples. Let's say that you have a Dog. You can cast that Dog as an Animal. A vet treats Animals and you can take your Dog to the vet. If the vet says "what type of Animal do you have" you would say "it's a Dog". The vet had an Animal reference and you've just cast it as a Dog, but your pet hasn't changed in any way. I might take my Cat to the vet and do something similar, but there's no way that I can cast my Cat as a Dog, or your Dog as a Cat.
It is importatnt to distinguish between operations that act on a variable and those that act on an object. Not understanding the difference is the cause of many problems. For instance, passing method arguments by value or by reference is an operation that acts on the variable, not the object. That's why the mechanism seems to treat value types and reference types differently. Casting acts on the variable while converting acts on the object.
Last edited by jmcilhinney; Apr 26th, 2006 at 05:48 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|