By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

Type conversion

P: n/a
I know I can convert an instance of a class to an instance of any class type
from which the original class is derived.
So is it possible to convert the other way. E.g.

Let Class Ford be derived from Class Car.

Dim F as New Ford(...)
Dim C as Car

C = CType(F, Car)
F = Nothing
'This conversion works OK and C.GetType.Name is Ford so now how can I
convert C back to a Ford.
F = CType(C, Ford) 'This does not work at runtime although the compiler
thinks its OK.

Since C.GetType is a Ford type why can't I convert to a Ford Class?

Thanks for any help
Fred
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

This works.

Dim f As New Ford

Dim c As car

f.Make = "Mustang"

f.Color = "Blue"

c = CType(f, car)

Debug.WriteLine(c.GetType.Name)

f = CType(c, Ford)

Debug.WriteLine(f.GetType.Name)

The classes

Public Class car

Dim strMake As String

Public Property Make() As String

Get

Return strMake

End Get

Set(ByVal Value As String)

strMake = Value

End Set

End Property

End Class

Public Class Ford

Inherits car

Dim mstrColor As String

Public Property Color() As String

Get

Return mstrColor

End Get

Set(ByVal Value As String)

mstrColor = Value

End Set

End Property

End Class

Ken

--------------------------

"fred" <fr**@NoSpam.com> wrote in message
news:ek****************@TK2MSFTNGP12.phx.gbl...
I know I can convert an instance of a class to an instance of any class type
from which the original class is derived.
So is it possible to convert the other way. E.g.

Let Class Ford be derived from Class Car.

Dim F as New Ford(...)
Dim C as Car

C = CType(F, Car)
F = Nothing
'This conversion works OK and C.GetType.Name is Ford so now how can I
convert C back to a Ford.
F = CType(C, Ford) 'This does not work at runtime although the compiler
thinks its OK.

Since C.GetType is a Ford type why can't I convert to a Ford Class?

Thanks for any help
Fred

Nov 20 '05 #2

P: n/a
Hi Ken,

I have an old bicycle, can you convert it to a Mercedes for me?
They both have wheels.

Cor
Nov 20 '05 #3

P: n/a
Fred,
Given:

Public Class Car

End Class

Public Class Ford : Inherits Car

End Class

Your code as posted, works, no exceptions!

Dim F as New Ford
Dim C as Car
C = F
F = Nothing
F = DirectCast(C, Ford)

Note you do not need to use CType when you are going from a derived type to
a base type, you do however need to use CType or I prefer DirectCast when
you are going from a base type to a derived type.

The following will not work:

Dim F as New Ford
Dim C as Car
C = F
F = Nothing
C = New Car
F = DirectCast(C, Ford)

As the C variable now contains a Car object, not a Ford object!

Remember that CType is convert type, while DirectCast is cast type. CType
will attempt to convert an object of one type (Integer) into a object of a
different type (String), while DirectCast will only allow casting up & down
the Class inherits & Interface implementation chain. Within VB.NET 2005 (aka
Whidbey, due out in 2005) we will be able to define overloaded operators,
one of which is CType which means that we will be able to define how CType
is going to behave with our class. For example you could define how to
convert any old Car into a new Ford.

Hope this helps
Jay
"fred" <fr**@NoSpam.com> wrote in message
news:ek****************@TK2MSFTNGP12.phx.gbl...
I know I can convert an instance of a class to an instance of any class type from which the original class is derived.
So is it possible to convert the other way. E.g.

Let Class Ford be derived from Class Car.

Dim F as New Ford(...)
Dim C as Car

C = CType(F, Car)
F = Nothing
'This conversion works OK and C.GetType.Name is Ford so now how can I
convert C back to a Ford.
F = CType(C, Ford) 'This does not work at runtime although the compiler
thinks its OK.

Since C.GetType is a Ford type why can't I convert to a Ford Class?

Thanks for any help
Fred

Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.