473,804 Members | 3,446 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Destina tionType 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 6575
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*********@pa ndora.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(Destina tionType 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*********@pa ndora.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(Destina tionType 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.co m>
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(Destina tionType 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(Destina tionType 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*********@pa ndora.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(Destina tionType 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
11834
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 field which it then appends to the form. I works in IE, but in NN7 nothing is passed. function processAssortment(){ // get the form Element var formElement=document.getElementById("candyBoxForm"); //loop thru the candy box contents, add them to...
3
3187
by: Daniel | last post by:
How do i dynamicaly load and unload a C# dll at runtime
6
10834
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 found this in the VB Language reference: <quote> The DirectCast keyword introduces a type conversion operation. You use it the same way you use the CType keyword.... Both keywords take an expression to be converted as the first argument, and
3
2175
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 part that is important is that the value passed to the server has to be the correct data type. I am trying to convert the input (as an object) to the correct data type, said type being read from the server immediately prior to update. The...
4
260
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 inherits from clsA
1
7869
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 As String) dosomething() End Sub End Class
6
3555
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 base class that is common to many other classes. public class Base .... end class I have 2 seperate classes that inherit from base
19
3256
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 implementations of the standard C <ctype.h> header define functions like toupper/tolower/etc as macros. To make it work in STL algorithms, you have to include <cctypeheader instead of <ctype.h>. At least on my PC (Debian/gcc 3.3),...
0
9585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.