473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

CType to a dynamicaly defined Type

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
Jul 21 '05 #1
4 6544
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

--
Rob Windsor
G6 Consulting
Toronto, Canada
"Joeri" <jo*********@pandora.be> wrote in message
news:0c****************************@phx.gbl...
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

Jul 21 '05 #2
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.

-----Original Message-----
Joeri,

I think I know what you want. You need to pass in the type you want toconvert 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

--
Rob Windsor
G6 Consulting
Toronto, Canada


Jul 21 '05 #3
Joeri <jo*********@pandora.be> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
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 End Sub
Hope this helps
Jay

"Joeri" <jo*********@pandora.be> wrote in message
news:0c****************************@phx.gbl... 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

Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Paul | last post by:
I am trying to get a form to dynamicaly add hidden elements. Below is the function I've created, basicaly it loops thru an array and attempts to add those values to a newly created hidden input...
3
by: Daniel | last post by:
How do i dynamicaly load and unload a C# dll at runtime
6
by: Ot | last post by:
I apparently have a bit to learn about Casting and Conversion. I have been thinking of them as the same but a discussion in another thread leads me to believe that this is wrong thinking. I...
3
by: FB's .NET Dev PC | last post by:
I'm trying to create a general-purpose routine to update a value in a data location on a remote server. The client-server protocol is OPC, but that isn't immediately important to my question. The...
4
by: Joeri | last post by:
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...
1
by: Bert Tuijl \(thuis\) | last post by:
Can someone explain this new-to-visualbasic programmer why my statement throws an exception? I have defined a class: Class myDataSet Inherits System.Data.DataSet Public Sub fun(ByVal someparm...
6
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a...
19
by: Taras_96 | last post by:
Hi all, A poster at http://bytes.com/forum/thread60652.html implies that using strtoupper in transform doesn't work because ctype.h may define strtoupper as a macro: "The problem is that most...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.