Type conversion

  • Thread starter Thread starter fred
  • Start date Start date
F

fred

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
 
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

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

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
 
Hi Ken,

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

Cor
 
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
 
Back
Top