Hi there,
Is there anyone that knows how to do the following?
I have a class A and a class B, that 100% inherits from class A (this means
that I don't have other code in class B, than the Inherit statement).
Now I want to make a Type Casting on an object of the type A, so that object
become a B type.
I have tryied it, but gets the error 'System.InvalidCastException: Specified
cast is not valid.', so want am I missing.
Please give a hint.
René 23 3463
"René Nordby" <re**@bluedot.dk> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... I have a class A and a class B, that 100% inherits from class A (this
means that I don't have other code in class B, than the Inherit statement). Now I want to make a Type Casting on an object of the type A, so that
object become a B type. I have tryied it, but gets the error 'System.InvalidCastException:
Specified cast is not valid.', so want am I missing.
You're missing the fact that it can't be done. You can cast a derived class
into its base class, but you can't cast a base class to a derived class.
Someone correct me if I'm wrong.
Hi Jeff,
Thanks for your quick answer.
When you say so, then I thnink of String and Integer datatypes, where you
can cast from String to Integer and the other way around, so how is that
done?
I know that both String and Integer is derived from Object, so I have also
tryied to let my two classes inherit from a base class, but it give me the
same result.
"Jeff Johnson [MVP: VB]" <i.***@enough.spam> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... "René Nordby" <re**@bluedot.dk> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than the Inherit statement). Now I want to make a Type Casting on an object of the type A, so that object become a B type. I have tryied it, but gets the error 'System.InvalidCastException: Specified cast is not valid.', so want am I missing.
You're missing the fact that it can't be done. You can cast a derived
class into its base class, but you can't cast a base class to a derived class. Someone correct me if I'm wrong.
Hi Jeff, You're missing the fact that it can't be done. You can cast a derived
class into its base class, but you can't cast a base class to a derived class. Someone correct me if I'm wrong.
You can indeed cast a base class reference to a sub-class reference and the
operation would result in a 'narrowing' conversion. BUT the conversion will
succeed ONLY if the base class reference you are trying to cast was
originally obtained from an earlier widening conversion from sub-class to
base-class.
Could you please spell it out a little more?
René
"Nice Chap" <Ni******@PlasmaDyne.com> wrote in message
news:uk**************@TK2MSFTNGP12.phx.gbl... Hi Jeff,
You're missing the fact that it can't be done. You can cast a derived class into its base class, but you can't cast a base class to a derived class. Someone correct me if I'm wrong.
You can indeed cast a base class reference to a sub-class reference and
the operation would result in a 'narrowing' conversion. BUT the conversion
will succeed ONLY if the base class reference you are trying to cast was originally obtained from an earlier widening conversion from sub-class to base-class.
"Nice Chap" <Ni******@PlasmaDyne.com> wrote in
news:uk**************@TK2MSFTNGP12.phx.gbl: You can indeed cast a base class reference to a sub-class reference and the operation would result in a 'narrowing' conversion. BUT the conversion will succeed ONLY if the base class reference you are trying to cast was originally obtained from an earlier widening conversion from sub-class to base-class.
??? Can someone re-write this in clearer terms?
Thanks.
In article <Xn********************************@140.99.99.130> , Anon-E-Moose wrote: "Nice Chap" <Ni******@PlasmaDyne.com> wrote in news:uk**************@TK2MSFTNGP12.phx.gbl:
You can indeed cast a base class reference to a sub-class reference and the operation would result in a 'narrowing' conversion. BUT the conversion will succeed ONLY if the base class reference you are trying to cast was originally obtained from an earlier widening conversion from sub-class to base-class.
??? Can someone re-write this in clearer terms?
Thanks.
What the op was saying was that, you can only cast a base class
reference to a subclass if that base class reference points to a
subclass instance.
In other words, casting a subclass to a base class is a narrowing
conversion, in that you loose access to some methods/properties of the
subclass since you'll be accessing it through the base class variable.
Casting a baseclass reference to a subclass would be a widening
conversion, since you would be picking up methods/properties. This is
not a safe operation, unless the base class reference actually points
to a subclass instance... Boy, this is confusing isn't it :) Here is
some psuedo code that may help...
class Base
end class
class SubClass
inherits base
end class
' this works
dim b as base = new subclass
dim s as subclass = ctype(b, subclass)
' and this works
dim s as subclass = new subclass
dim b as base = ctype(s, base)
' this doesn't
dim b as base = new base
dim s as subclass = ctype(b, subclass)
--
Tom Shelton [MVP]
I believe the framework uses TypeConverters to cast between Strings &
Integer and vice versa. Google TypeConverter to get more info.
hth,
Luhar
"René Nordby" <re**@bluedot.dk> wrote in message
news:OF**************@tk2msftngp13.phx.gbl... Hi Jeff,
Thanks for your quick answer.
When you say so, then I thnink of String and Integer datatypes, where you can cast from String to Integer and the other way around, so how is that done?
I know that both String and Integer is derived from Object, so I have also tryied to let my two classes inherit from a base class, but it give me the same result.
"Jeff Johnson [MVP: VB]" <i.***@enough.spam> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... "René Nordby" <re**@bluedot.dk> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than the Inherit statement). Now I want to make a Type Casting on an object of the type A, so that object become a B type. I have tryied it, but gets the error 'System.InvalidCastException: Specified cast is not valid.', so want am I missing.
You're missing the fact that it can't be done. You can cast a derived
class into its base class, but you can't cast a base class to a derived class. Someone correct me if I'm wrong.
Rene, I think you should be able to do this. I think there is something
else going on. I have a project right now for example with a base class
LineItem and two classes deriving (inheriting) from it OrderItem and
ShipmentItem. I can use CTYPE() to perform any of the following
conversions:
OrderItem to LineItem
LineItem to OrderItem
ShipmentItem to LineItem
LineItem to ShipmentItem
The only thing I can not do is convert OrderItem to ShipmentItem, which
makes sense because they are not the same "thing". If you think about it,
OrderItem is a LineItem and ShipmentItem is a LineItem but OrderItem is not
a ShipmentItem.. You need to ask yourself what kind of real world objects
your Classes A and B represent and whether the conversion makes sense. Or
if you want to post you code we could look at it.
Good luck!
-Carl
"René Nordby" <re**@bluedot.dk> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this
means that I don't have other code in class B, than the Inherit statement). Now I want to make a Type Casting on an object of the type A, so that
object become a B type. I have tryied it, but gets the error 'System.InvalidCastException:
Specified cast is not valid.', so want am I missing. Please give a hint. René
Hi Tom, Casting a baseclass reference to a subclass would be a widening
conversion, since you would be picking up methods/properties.<
Sorry, it is a 'Narrowing Conversion' ....
OK, thanks everyone for your replies.
I will try to explain what I'm trying to do, and hopefully some one knows
how to do it.
I want a Function to return a Class. With that Class in hand, I should be
able to Cast/Convert it, to two other Classes.
In pseudo code it would look like this
Sub GetClassA
myClassA = CType(GetClass, ClassA)
End Sub
Sub GetClassB
myClassB = CType(GetClass, ClassB)
End Sub
Function GetClass As ClassC
Return ClassC
End Function
Is that possible, and how?
Keep up the good work
René
"René Nordby" <re**@bluedot.dk> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this
means that I don't have other code in class B, than the Inherit statement). Now I want to make a Type Casting on an object of the type A, so that
object become a B type. I have tryied it, but gets the error 'System.InvalidCastException:
Specified cast is not valid.', so want am I missing. Please give a hint. René
Nice Chap & Tom,
Casting a class from a base class or to a derived class is NOT a conversion!
So it can be neither Widening nor Narrowing!
It is a Cast.
A Conversion is physically changing the type of the object for example from
Integer to String, or String to Double.
A Cast the object stays the same type, however the variable it is placed
into changes. For example casting the sender parameter to the specific type
the object is. The object itself stays the same type however how you program
uses it changes.
You should use DirectCast for casting operations and use CType (or their
short cuts) for Conversions, for in VS.NET 2005 (aka Whidbey, due out
sometime in 2005) we can overload the CType operator to define how to
convert our class from one type to another (for example a Complex number
class can define how to convert itself to & from a Double).
Hope this helps
Jay
"Nice Chap" <Ni******@PlasmaDyne.com> wrote in message
news:uT**************@tk2msftngp13.phx.gbl... Hi Tom,
Casting a baseclass reference to a subclass would be a widening conversion, since you would be picking up methods/properties.<
Sorry, it is a 'Narrowing Conversion' ....
I just tried this and it works. Is this what you are looking for? -Carl.
Option Explicit On
Option Strict On
Public Class A
Inherits C
End Class
Public Class B
Inherits C
End Class
Public Class C
End Class
Public Class Test
Public Function GetClassA() As A
Return CType(GetClassC(), A)
End Function
Public Function GEtClassB() As B
Return CType(GetClassC(), B)
End Function
Public Function GetClassC() As C
Return New C
End Function
End Class
"René Nordby" <re**@bluedot.dk> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl... OK, thanks everyone for your replies.
I will try to explain what I'm trying to do, and hopefully some one knows how to do it.
I want a Function to return a Class. With that Class in hand, I should be able to Cast/Convert it, to two other Classes.
In pseudo code it would look like this
Sub GetClassA myClassA = CType(GetClass, ClassA) End Sub
Sub GetClassB myClassB = CType(GetClass, ClassB) End Sub
Function GetClass As ClassC Return ClassC End Function
Is that possible, and how?
Keep up the good work René
"René Nordby" <re**@bluedot.dk> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than the Inherit statement). Now I want to make a Type Casting on an object of the type A, so that object become a B type. I have tryied it, but gets the error 'System.InvalidCastException: Specified cast is not valid.', so want am I missing. Please give a hint. René
René,
I would have GetClassA & GetClassB create the correct type of object, and
not include GetClass. Sub GetClassA
myClassA = New ClassA End Sub
Sub GetClassB
myClassB = New ClassB End Sub
Alternatively you would need to pass a parameter to GetClass that indicates
what kind of object to actually create. I would either use an Enum or the
Type & Activator.CreateInstance.
Hope this helps
Jay
"René Nordby" <re**@bluedot.dk> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl... OK, thanks everyone for your replies.
I will try to explain what I'm trying to do, and hopefully some one knows how to do it.
I want a Function to return a Class. With that Class in hand, I should be able to Cast/Convert it, to two other Classes.
In pseudo code it would look like this
Sub GetClassA myClassA = CType(GetClass, ClassA) End Sub
Sub GetClassB myClassB = CType(GetClass, ClassB) End Sub
Function GetClass As ClassC Return ClassC End Function
Is that possible, and how?
Keep up the good work René
"René Nordby" <re**@bluedot.dk> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than the Inherit statement). Now I want to make a Type Casting on an object of the type A, so that object become a B type. I have tryied it, but gets the error 'System.InvalidCastException: Specified cast is not valid.', so want am I missing. Please give a hint. René
Carl,
No! it doesn't work, as soon as you attempt to run either GetClassA or
GetClassB, you get InvalidCastException!
The other posts in this thread attempt to explain why your code fails.
Hope this helps
Jay
"Carl Tribble" <ca*********@sbcglobal.net> wrote in message
news:OD**************@tk2msftngp13.phx.gbl... I just tried this and it works. Is this what you are looking for? -Carl.
Option Explicit On Option Strict On Public Class A Inherits C End Class Public Class B Inherits C End Class Public Class C End Class Public Class Test Public Function GetClassA() As A Return CType(GetClassC(), A) End Function Public Function GEtClassB() As B Return CType(GetClassC(), B) End Function Public Function GetClassC() As C Return New C End Function End Class
"René Nordby" <re**@bluedot.dk> wrote in message news:%2***************@TK2MSFTNGP12.phx.gbl... OK, thanks everyone for your replies.
I will try to explain what I'm trying to do, and hopefully some one
knows how to do it.
I want a Function to return a Class. With that Class in hand, I should
be able to Cast/Convert it, to two other Classes.
In pseudo code it would look like this
Sub GetClassA myClassA = CType(GetClass, ClassA) End Sub
Sub GetClassB myClassB = CType(GetClass, ClassB) End Sub
Function GetClass As ClassC Return ClassC End Function
Is that possible, and how?
Keep up the good work René
"René Nordby" <re**@bluedot.dk> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than the Inherit statement). Now I want to make a Type Casting on an object of the type A, so that object become a B type. I have tryied it, but gets the error 'System.InvalidCastException: Specified cast is not valid.', so want am I missing. Please give a hint. René
> I will try to explain what I'm trying to do, and hopefully some one knows how to do it.
I want a Function to return a Class. With that Class in hand, I should be able to Cast/Convert it, to two other Classes.
You can not return a class, you can return an object.
You convert no class, you cast or convert an object.
Cor
Absolutely, this is what I'm looking for, but as Jay writes, it doesn't work
when it comes to runtime, so what should be done, to get it to work.
I see what Jay means by returning the right object from start, but that's
not the solution, sorry.
The above code is what I want, but how should it be implemented, so it
works?
René
"Carl Tribble" <ca*********@sbcglobal.net> wrote in message
news:OD**************@tk2msftngp13.phx.gbl... I just tried this and it works. Is this what you are looking for? -Carl.
Option Explicit On Option Strict On Public Class A Inherits C End Class Public Class B Inherits C End Class Public Class C End Class Public Class Test Public Function GetClassA() As A Return CType(GetClassC(), A) End Function Public Function GEtClassB() As B Return CType(GetClassC(), B) End Function Public Function GetClassC() As C Return New C End Function End Class
"René Nordby" <re**@bluedot.dk> wrote in message news:%2***************@TK2MSFTNGP12.phx.gbl... OK, thanks everyone for your replies.
I will try to explain what I'm trying to do, and hopefully some one
knows how to do it.
I want a Function to return a Class. With that Class in hand, I should
be able to Cast/Convert it, to two other Classes.
In pseudo code it would look like this
Sub GetClassA myClassA = CType(GetClass, ClassA) End Sub
Sub GetClassB myClassB = CType(GetClass, ClassB) End Sub
Function GetClass As ClassC Return ClassC End Function
Is that possible, and how?
Keep up the good work René
"René Nordby" <re**@bluedot.dk> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than the Inherit statement). Now I want to make a Type Casting on an object of the type A, so that object become a B type. I have tryied it, but gets the error 'System.InvalidCastException: Specified cast is not valid.', so want am I missing. Please give a hint. René
Hi All,
Now I have solved it, but I'm not 100% sure that it is the rigt OO way, so
if anyone have any comments you are mostly welcome.
Option Explicit On
Option Strict On
Public Class A
Inherits B
End Class
Public Class B
End Class
Public Class C
Inherits A
End Class
Public Class Test
Public Function GetClassA() As A
Return CType(GetClassC(), A)
End Function
Public Function GetClassB() As B
Return CType(GetClassC(), B)
End Function
Private Function GetClassC() As C
Return New C
End Function
End Class
"René Nordby" <re**@bluedot.dk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... Absolutely, this is what I'm looking for, but as Jay writes, it doesn't
work when it comes to runtime, so what should be done, to get it to work.
I see what Jay means by returning the right object from start, but that's not the solution, sorry.
The above code is what I want, but how should it be implemented, so it works?
René
"Carl Tribble" <ca*********@sbcglobal.net> wrote in message news:OD**************@tk2msftngp13.phx.gbl... I just tried this and it works. Is this what you are looking
or? -Carl. Option Explicit On Option Strict On Public Class A Inherits C End Class Public Class B Inherits C End Class Public Class C End Class Public Class Test Public Function GetClassA() As A Return CType(GetClassC(), A) End Function Public Function GEtClassB() As B Return CType(GetClassC(), B) End Function Public Function GetClassC() As C Return New C End Function End Class
"René Nordby" <re**@bluedot.dk> wrote in message news:%2***************@TK2MSFTNGP12.phx.gbl... OK, thanks everyone for your replies.
I will try to explain what I'm trying to do, and hopefully some one
knows how to do it.
I want a Function to return a Class. With that Class in hand, I should be able to Cast/Convert it, to two other Classes.
In pseudo code it would look like this
Sub GetClassA myClassA = CType(GetClass, ClassA) End Sub
Sub GetClassB myClassB = CType(GetClass, ClassB) End Sub
Function GetClass As ClassC Return ClassC End Function
Is that possible, and how?
Keep up the good work René
"René Nordby" <re**@bluedot.dk> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... > Hi there, > > > > Is there anyone that knows how to do the following? > > > > I have a class A and a class B, that 100% inherits from class A
(this means > that I don't have other code in class B, than the Inherit
statement). > > > > Now I want to make a Type Casting on an object of the type A, so
that object > become a B type. > > > > I have tryied it, but gets the error 'System.InvalidCastException: Specified > cast is not valid.', so want am I missing. > > > > Please give a hint. > > > > René > > > >
Absolutely, this is what I'm looking for, but as Jay writes, it doesn't work
when it comes to runtime, so what should be done, to get it to work.
I see what Jay means by returning the right object from start, but that's
not the solution, sorry.
The above code is what I want, but how should it be implemented, so it
works?
René
"Carl Tribble" <ca*********@sbcglobal.net> wrote in message
news:OD**************@tk2msftngp13.phx.gbl... I just tried this and it works. Is this what you are looking for? -Carl.
Option Explicit On Option Strict On Public Class A Inherits C End Class Public Class B Inherits C End Class Public Class C End Class Public Class Test Public Function GetClassA() As A Return CType(GetClassC(), A) End Function Public Function GEtClassB() As B Return CType(GetClassC(), B) End Function Public Function GetClassC() As C Return New C End Function End Class
"René Nordby" <re**@bluedot.dk> wrote in message news:%2***************@TK2MSFTNGP12.phx.gbl... OK, thanks everyone for your replies.
I will try to explain what I'm trying to do, and hopefully some one
knows how to do it.
I want a Function to return a Class. With that Class in hand, I should
be able to Cast/Convert it, to two other Classes.
In pseudo code it would look like this
Sub GetClassA myClassA = CType(GetClass, ClassA) End Sub
Sub GetClassB myClassB = CType(GetClass, ClassB) End Sub
Function GetClass As ClassC Return ClassC End Function
Is that possible, and how?
Keep up the good work René
"René Nordby" <re**@bluedot.dk> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than the Inherit statement). Now I want to make a Type Casting on an object of the type A, so that object become a B type. I have tryied it, but gets the error 'System.InvalidCastException: Specified cast is not valid.', so want am I missing. Please give a hint. René
Hi All,
Now I have solved it, but I'm not 100% sure that it is the rigt OO way, so
if anyone have any comments you are mostly welcome.
Option Explicit On
Option Strict On
Public Class A
Inherits B
End Class
Public Class B
End Class
Public Class C
Inherits A
End Class
Public Class Test
Public Function GetClassA() As A
Return CType(GetClassC(), A)
End Function
Public Function GetClassB() As B
Return CType(GetClassC(), B)
End Function
Private Function GetClassC() As C
Return New C
End Function
End Class
"René Nordby" <re**@bluedot.dk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... Absolutely, this is what I'm looking for, but as Jay writes, it doesn't
work when it comes to runtime, so what should be done, to get it to work.
I see what Jay means by returning the right object from start, but that's not the solution, sorry.
The above code is what I want, but how should it be implemented, so it works?
René
"Carl Tribble" <ca*********@sbcglobal.net> wrote in message news:OD**************@tk2msftngp13.phx.gbl... I just tried this and it works. Is this what you are looking
or? -Carl. Option Explicit On Option Strict On Public Class A Inherits C End Class Public Class B Inherits C End Class Public Class C End Class Public Class Test Public Function GetClassA() As A Return CType(GetClassC(), A) End Function Public Function GEtClassB() As B Return CType(GetClassC(), B) End Function Public Function GetClassC() As C Return New C End Function End Class
"René Nordby" <re**@bluedot.dk> wrote in message news:%2***************@TK2MSFTNGP12.phx.gbl... OK, thanks everyone for your replies.
I will try to explain what I'm trying to do, and hopefully some one
knows how to do it.
I want a Function to return a Class. With that Class in hand, I should be able to Cast/Convert it, to two other Classes.
In pseudo code it would look like this
Sub GetClassA myClassA = CType(GetClass, ClassA) End Sub
Sub GetClassB myClassB = CType(GetClass, ClassB) End Sub
Function GetClass As ClassC Return ClassC End Function
Is that possible, and how?
Keep up the good work René
"René Nordby" <re**@bluedot.dk> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... > Hi there, > > > > Is there anyone that knows how to do the following? > > > > I have a class A and a class B, that 100% inherits from class A
(this means > that I don't have other code in class B, than the Inherit
statement). > > > > Now I want to make a Type Casting on an object of the type A, so
that object > become a B type. > > > > I have tryied it, but gets the error 'System.InvalidCastException: Specified > cast is not valid.', so want am I missing. > > > > Please give a hint. > > > > René > > > >
René, Now I have solved it, but I'm not 100% sure that it is the rigt OO way, so if anyone have any comments you are mostly welcome.
No you didn't, as your GetClassB method will fail with a
InvalidCastException! The above code is what I want, but how should it be implemented, so it works?
It "cannot" be implemented in VS.NET 2003, as CType cannot change the type
of a C object into a B or A object. It can however Cast a B or A object in a
C variable to a B or A variable. Only predefined CTYPE conversions are
defined in VS.NET 2003, C to A & C to B are not predefined. Also see my
message to Nice chap about the difference between CType the conversion &
DirectCast the casting.
VS.NET 2005 (aka Whidbey, due out in 2005) you can overload CType such that
it can create a B or A object given a C object, HOWEVER!!! your sample seems
to be an extreme abuse of this ability! If you provide better details on
what you are really trying to accomplish we may be able to offer other
solutions then the two I offered earlier.
The correct way (only way really) to implement this is:
I would have GetClassA & GetClassB create the correct type of object, and
not include GetClass.
Alternatively you would need to pass a parameter to GetClass that indicates
what kind of object to actually create. I would either use an Enum or the
Type & Activator.CreateInstance.
Public Function GetClassA() As A Return DirectCast(GetClassC(gettype(A)), A) End Function
Public Function GetClassB() As B Return DirectCast(GetClassC(GetType(B)), B) End Function
Private Function GetClassC(type As Type) As C Return DirectCast(Activator.CreateInstance(type), C) End Function
Hope this helps
Jay
"René Nordby" <re**@bluedot.dk> wrote in message
news:ex**************@TK2MSFTNGP10.phx.gbl... Hi All,
Now I have solved it, but I'm not 100% sure that it is the rigt OO way, so if anyone have any comments you are mostly welcome.
Option Explicit On Option Strict On
Public Class A Inherits B End Class
Public Class B End Class
Public Class C Inherits A End Class
Public Class Test Public Function GetClassA() As A Return CType(GetClassC(), A) End Function
Public Function GetClassB() As B Return CType(GetClassC(), B) End Function
Private Function GetClassC() As C Return New C End Function End Class
"René Nordby" <re**@bluedot.dk> wrote in message news:%2****************@tk2msftngp13.phx.gbl... Absolutely, this is what I'm looking for, but as Jay writes, it doesn't work when it comes to runtime, so what should be done, to get it to work.
I see what Jay means by returning the right object from start, but
that's not the solution, sorry.
The above code is what I want, but how should it be implemented, so it works?
René
"Carl Tribble" <ca*********@sbcglobal.net> wrote in message news:OD**************@tk2msftngp13.phx.gbl... I just tried this and it works. Is this what you are looking or? -Carl. Option Explicit On Option Strict On Public Class A Inherits C End Class Public Class B Inherits C End Class Public Class C End Class Public Class Test Public Function GetClassA() As A Return CType(GetClassC(), A) End Function Public Function GEtClassB() As B Return CType(GetClassC(), B) End Function Public Function GetClassC() As C Return New C End Function End Class
"René Nordby" <re**@bluedot.dk> wrote in message news:%2***************@TK2MSFTNGP12.phx.gbl... > OK, thanks everyone for your replies. > > I will try to explain what I'm trying to do, and hopefully some one knows > how to do it. > > I want a Function to return a Class. With that Class in hand, I
should be > able to Cast/Convert it, to two other Classes. > > In pseudo code it would look like this > > Sub GetClassA > myClassA = CType(GetClass, ClassA) > End Sub > > Sub GetClassB > myClassB = CType(GetClass, ClassB) > End Sub > > Function GetClass As ClassC > Return ClassC > End Function > > Is that possible, and how? > > Keep up the good work > René > > "René Nordby" <re**@bluedot.dk> wrote in message > news:%2****************@TK2MSFTNGP09.phx.gbl... > > Hi there, > > > > > > > > Is there anyone that knows how to do the following? > > > > > > > > I have a class A and a class B, that 100% inherits from class A (this > means > > that I don't have other code in class B, than the Inherit statement). > > > > > > > > Now I want to make a Type Casting on an object of the type A, so that > object > > become a B type. > > > > > > > > I have tryied it, but gets the error 'System.InvalidCastException: > Specified > > cast is not valid.', so want am I missing. > > > > > > > > Please give a hint. > > > > > > > > René > > > > > > > > > >
René, Now I have solved it, but I'm not 100% sure that it is the rigt OO way, so if anyone have any comments you are mostly welcome.
No you didn't, as your GetClassB method will fail with a
InvalidCastException! The above code is what I want, but how should it be implemented, so it works?
It "cannot" be implemented in VS.NET 2003, as CType cannot change the type
of a C object into a B or A object. It can however Cast a B or A object in a
C variable to a B or A variable. Only predefined CTYPE conversions are
defined in VS.NET 2003, C to A & C to B are not predefined. Also see my
message to Nice chap about the difference between CType the conversion &
DirectCast the casting.
VS.NET 2005 (aka Whidbey, due out in 2005) you can overload CType such that
it can create a B or A object given a C object, HOWEVER!!! your sample seems
to be an extreme abuse of this ability! If you provide better details on
what you are really trying to accomplish we may be able to offer other
solutions then the two I offered earlier.
The correct way (only way really) to implement this is:
I would have GetClassA & GetClassB create the correct type of object, and
not include GetClass.
Alternatively you would need to pass a parameter to GetClass that indicates
what kind of object to actually create. I would either use an Enum or the
Type & Activator.CreateInstance.
Public Function GetClassA() As A Return DirectCast(GetClassC(gettype(A)), A) End Function
Public Function GetClassB() As B Return DirectCast(GetClassC(GetType(B)), B) End Function
Private Function GetClassC(type As Type) As C Return DirectCast(Activator.CreateInstance(type), C) End Function
Hope this helps
Jay
"René Nordby" <re**@bluedot.dk> wrote in message
news:ex**************@TK2MSFTNGP10.phx.gbl... Hi All,
Now I have solved it, but I'm not 100% sure that it is the rigt OO way, so if anyone have any comments you are mostly welcome.
Option Explicit On Option Strict On
Public Class A Inherits B End Class
Public Class B End Class
Public Class C Inherits A End Class
Public Class Test Public Function GetClassA() As A Return CType(GetClassC(), A) End Function
Public Function GetClassB() As B Return CType(GetClassC(), B) End Function
Private Function GetClassC() As C Return New C End Function End Class
"René Nordby" <re**@bluedot.dk> wrote in message news:%2****************@tk2msftngp13.phx.gbl... Absolutely, this is what I'm looking for, but as Jay writes, it doesn't work when it comes to runtime, so what should be done, to get it to work.
I see what Jay means by returning the right object from start, but
that's not the solution, sorry.
The above code is what I want, but how should it be implemented, so it works?
René
"Carl Tribble" <ca*********@sbcglobal.net> wrote in message news:OD**************@tk2msftngp13.phx.gbl... I just tried this and it works. Is this what you are looking or? -Carl. Option Explicit On Option Strict On Public Class A Inherits C End Class Public Class B Inherits C End Class Public Class C End Class Public Class Test Public Function GetClassA() As A Return CType(GetClassC(), A) End Function Public Function GEtClassB() As B Return CType(GetClassC(), B) End Function Public Function GetClassC() As C Return New C End Function End Class
"René Nordby" <re**@bluedot.dk> wrote in message news:%2***************@TK2MSFTNGP12.phx.gbl... > OK, thanks everyone for your replies. > > I will try to explain what I'm trying to do, and hopefully some one knows > how to do it. > > I want a Function to return a Class. With that Class in hand, I
should be > able to Cast/Convert it, to two other Classes. > > In pseudo code it would look like this > > Sub GetClassA > myClassA = CType(GetClass, ClassA) > End Sub > > Sub GetClassB > myClassB = CType(GetClass, ClassB) > End Sub > > Function GetClass As ClassC > Return ClassC > End Function > > Is that possible, and how? > > Keep up the good work > René > > "René Nordby" <re**@bluedot.dk> wrote in message > news:%2****************@TK2MSFTNGP09.phx.gbl... > > Hi there, > > > > > > > > Is there anyone that knows how to do the following? > > > > > > > > I have a class A and a class B, that 100% inherits from class A (this > means > > that I don't have other code in class B, than the Inherit statement). > > > > > > > > Now I want to make a Type Casting on an object of the type A, so that > object > > become a B type. > > > > > > > > I have tryied it, but gets the error 'System.InvalidCastException: > Specified > > cast is not valid.', so want am I missing. > > > > > > > > Please give a hint. > > > > > > > > René > > > > > > > > > >
Thanks to all of you, especially Carl and Jay for your good and detailed
descriptions.
I think I have solved my problem, otherwise I will get back to you.
..Net and OO is a fantastic world :-)
René
"Carl Tribble" <ca*********@sbcglobal.net> wrote in message
news:ur**************@TK2MSFTNGP10.phx.gbl... Renee,
The reason this works is because you are casting a derived object to a
base object. In your original post you stated you were trying to do just the opposite (cast a base object to a derived object), which as Jeff Johnson stated in the first response on this thread is not possible. ironic huh? :-)
You can, in fact, cast a derived object to any one of it's bases. If that is what you wanted in the first place, then you have the solution and I guess we are done. If you still need to do it the other way around, give this a try. I experimented a good bit with this and it does work. Basically instead of converting the object (or casting it to a new
reference or whatever the right term is...) create a new object of the desired type and in its constructor, copy any properties or state information from the object you are converting from. Attached is a complete solution that illustrates what I am talking about. This example uses a base class C and two derived classes A and B. C has a method (or property) named ToA that changes itself into a type A object, and another property named ToB that changes itself into a type B object. So instead
of a procedure like this: CType(GetClassC, A) you do this: objC.ToA
I hope either this helps, or that your own solution has already solved
your problem.
-Carl
"René Nordby" <re**@bluedot.dk> wrote in message news:ex**************@TK2MSFTNGP10.phx.gbl... Hi All,
Now I have solved it, but I'm not 100% sure that it is the rigt OO way,
so if anyone have any comments you are mostly welcome.
Option Explicit On Option Strict On
Public Class A Inherits B End Class
Public Class B End Class
Public Class C Inherits A End Class
Public Class Test Public Function GetClassA() As A Return CType(GetClassC(), A) End Function
Public Function GetClassB() As B Return CType(GetClassC(), B) End Function
Private Function GetClassC() As C Return New C End Function End Class
"René Nordby" <re**@bluedot.dk> wrote in message news:%2****************@tk2msftngp13.phx.gbl... Absolutely, this is what I'm looking for, but as Jay writes, it
doesn't work when it comes to runtime, so what should be done, to get it to work.
I see what Jay means by returning the right object from start, but that's not the solution, sorry.
The above code is what I want, but how should it be implemented, so it works?
René
"Carl Tribble" <ca*********@sbcglobal.net> wrote in message news:OD**************@tk2msftngp13.phx.gbl... > I just tried this and it works. Is this what you are looking
or? -Carl. > > Option Explicit On > Option Strict On > Public Class A > Inherits C > End Class > Public Class B > Inherits C > End Class > Public Class C > End Class > Public Class Test > Public Function GetClassA() As A > Return CType(GetClassC(), A) > End Function > Public Function GEtClassB() As B > Return CType(GetClassC(), B) > End Function > Public Function GetClassC() As C > Return New C > End Function > End Class > > > > > "René Nordby" <re**@bluedot.dk> wrote in message > news:%2***************@TK2MSFTNGP12.phx.gbl... > > OK, thanks everyone for your replies. > > > > I will try to explain what I'm trying to do, and hopefully some
one knows > > how to do it. > > > > I want a Function to return a Class. With that Class in hand, I should be > > able to Cast/Convert it, to two other Classes. > > > > In pseudo code it would look like this > > > > Sub GetClassA > > myClassA = CType(GetClass, ClassA) > > End Sub > > > > Sub GetClassB > > myClassB = CType(GetClass, ClassB) > > End Sub > > > > Function GetClass As ClassC > > Return ClassC > > End Function > > > > Is that possible, and how? > > > > Keep up the good work > > René > > > > "René Nordby" <re**@bluedot.dk> wrote in message > > news:%2****************@TK2MSFTNGP09.phx.gbl... > > > Hi there, > > > > > > > > > > > > Is there anyone that knows how to do the following? > > > > > > > > > > > > I have a class A and a class B, that 100% inherits from class A
(this > > means > > > that I don't have other code in class B, than the Inherit statement). > > > > > > > > > > > > Now I want to make a Type Casting on an object of the type A, so that > > object > > > become a B type. > > > > > > > > > > > > I have tryied it, but gets the error
'System.InvalidCastException: > > Specified > > > cast is not valid.', so want am I missing. > > > > > > > > > > > > Please give a hint. > > > > > > > > > > > > René > > > > > > > > > > > > > > > > > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Suzanne Vogel |
last post by:
** Isn't the 'static_cast' operator the same as traditional type
casting? ie, Aren't the following ways of getting b1, b2 the same?
// 'Derived' is derived from 'Base'
Derived* d = new...
|
by: Jacob Jensen |
last post by:
This question has probably been asked a million time, but here it comes
again. I want to learn the difference between the three type cast operators:
static_cast, reinterpret_cast, dynamic_cast. A...
|
by: Steve Teeples |
last post by:
Can someone explain how to cast an object to a specific type during runtime?
// This line of code tells me the objects type.
System.Type type = System.Type.GetType("string of the type");
//...
|
by: Bob |
last post by:
This has been bugging me for a while now.
GetType isn't availble for variables decalred as interface types, I have to
DirectCast(somevariable, Object).
In example:
Sub SomeSub(ByVal...
|
by: lovecreatesbeauty |
last post by:
Why (type*)pointer isn't equal to *(type**)pointer,
In the code snippet, it shows that:
(int *) == (int **) ,
(int *) != (*(int **)) .
Does type-casting change the address? or...
|
by: crook |
last post by:
I have code below and it works properly but when I'm compiling it with
"--pedantic" flag, GCC(3.4.2) shows such warning: "ISO C forbids
casting nonscalar to the same type". How can I change this...
|
by: shuisheng |
last post by:
Dear All,
Assume I have a class named Obj.
class Obj
{ };
And a class named Shape which is derived from Obj.
class Shape: public Obj
|
by: Frederic Rentsch |
last post by:
Hi all,
If I derive a class from another one because I need a few extra
features, is there a way to promote the base class to the derived one
without having to make copies of all attributes?
...
|
by: Smithers |
last post by:
Are there any important differences between the following two ways to
convert to a type?... where 'important differences' means something more
profound than a simple syntax preference of the...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |