Casting object
-
I was trying to "upper" cast a class. I looked like this but I get a Exception. Is there anyway to have a extended class and cast to it?
public class FirstClass { } public class SecondClass : FirstClass { } public class Main { public Main() { FirstClass iFirstC = new FirstClass(); SecondClass iSecondC = (SecondClass)iFirstC; } }
I get a error on the line.
SecondClass iSecondC = (SecondClass)iFirstC;
-
I was trying to "upper" cast a class. I looked like this but I get a Exception. Is there anyway to have a extended class and cast to it?
public class FirstClass { } public class SecondClass : FirstClass { } public class Main { public Main() { FirstClass iFirstC = new FirstClass(); SecondClass iSecondC = (SecondClass)iFirstC; } }
I get a error on the line.
SecondClass iSecondC = (SecondClass)iFirstC;
While syntactically correct, executing that statement will throw an exception, since an instance of
iFirstC
is not of typeSecondClass
. If you prefer not to handle exceptions, you should use theas
operator and check fornull
.SecondClass iSecondC = iFirstC as SecondClass;
if (iSecondC == null) {
// Handle error
}/ravi
This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com
-
I was trying to "upper" cast a class. I looked like this but I get a Exception. Is there anyway to have a extended class and cast to it?
public class FirstClass { } public class SecondClass : FirstClass { } public class Main { public Main() { FirstClass iFirstC = new FirstClass(); SecondClass iSecondC = (SecondClass)iFirstC; } }
I get a error on the line.
SecondClass iSecondC = (SecondClass)iFirstC;
-
I was trying to "upper" cast a class. I looked like this but I get a Exception. Is there anyway to have a extended class and cast to it?
public class FirstClass { } public class SecondClass : FirstClass { } public class Main { public Main() { FirstClass iFirstC = new FirstClass(); SecondClass iSecondC = (SecondClass)iFirstC; } }
I get a error on the line.
SecondClass iSecondC = (SecondClass)iFirstC;
Matglas wrote:
Is there anyway to have a extended class and cast to it?
Only if the object is of the "extended class" (aka derived class). The following will work:
FirstClass iFirstC = new SecondClass();
SecondClass iSecondC = (SecondClass)iFirstC;This is because the actual object is a
SecondClass
even although it is initially referenced as aFirstClass
. Does this help?
Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
-
Matglas wrote:
Is there anyway to have a extended class and cast to it?
Only if the object is of the "extended class" (aka derived class). The following will work:
FirstClass iFirstC = new SecondClass();
SecondClass iSecondC = (SecondClass)iFirstC;This is because the actual object is a
SecondClass
even although it is initially referenced as aFirstClass
. Does this help?
Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos