CType to a dynamicaly defined Type

  • Thread starter Thread starter Joeri
  • Start date Start date
J

Joeri

Hi,

I'm struggling with this issue, maybe someone has already
solved ik before. Here's the problem
I Have :
clsA witch is a base class
clsB inherits from clsA
clsC inherits from clsA
clsD inherits from clsA

At a certain point i have an instance of clsA : objA
What i can do is CType(objA, clsB).
But i would like to put it in a procedure and be able to
pass the resulting type as a parameter
Ex :
public sub Convert(DestinationType as ????)
CType (objA, DestinationType)
End Sub
Where destinationType could be clsB,clsC or clsD
I already tried a lot of possibilities but can't get it
to work.
Maybe anyone of you has an idea?

Thanks in advance
Best regards
Joeri
 
Joeri,

I think I know what you want. You need to pass in the type you want to
convert to.

Public Sub Convert(ByVal destinationType As System.Type)
CType(objA, destinationType)
End Sub

Public Sub Test()
Dim objA As New clsA
Convert(objA, GetType(clsB))
Convert(objA, GetType(clsC))
Convert(objA, GetType(clsD))
End Sub
 
Hi rob,
Thanks for your answer,

Indeed, that's wat i thought, But when i try it i get a
compiler error telling me :
Type 'DesinationType' is not defined.
 
Joeri said:
I'm struggling with this issue, maybe someone has already
solved ik before. Here's the problem
I Have :
clsA witch is a base class
clsB inherits from clsA
clsC inherits from clsA
clsD inherits from clsA

At a certain point i have an instance of clsA : objA
What i can do is CType(objA, clsB).
But i would like to put it in a procedure and be able to
pass the resulting type as a parameter
Ex :
public sub Convert(DestinationType as ????)
CType (objA, DestinationType)
End Sub
Where destinationType could be clsB,clsC or clsD
I already tried a lot of possibilities but can't get it
to work.
Maybe anyone of you has an idea?

What would you expect to be able to do afterwards? The point of typing
is that you can tell the compiler which type something is, so it has
more knowledge of what methods, fields etc are avaiable. If you don't
*actually* know the type at compile-time, you can't give the compiler
any more information.
 
Joeri,
As Jon asked, what are you attempting to do?
At a certain point i have an instance of clsA : objA
What i can do is CType(objA, clsB).

If you have an instance of clsA, you cannot CType it to clsB no matter how
hard you try, as it is an instance of clsA. If you have a clsA variable
which has an instance of clsB you can Ctype that to clsB as the actual
object is clsB. Note for reference types DirectCast is generally better than
CType.

Dim objA As clsA = New clsB
Dim objB As clsB = DirectCast(objA, clsB)
public sub Convert(DestinationType as ????)
CType (objA, DestinationType)
End Sub

Is not possible, CType & DirectCast are keywords, they require the actual
type name be supplied, variables are not supported!

The closest you can come is an if/elseif
public sub Convert(DestinationType as Type)
if DestinationType is GetType(clsB) Then
CType (objA, clsB)
elseif DestinationType is GetType(clsC) Then
CType (objA, clsC)
elseif DestinationType is GetType(clsD) Then
CType (objA, clsD)
end if

Hope this helps
Jay
 
Back
Top