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

determining object type

P: n/a
Hi,
how can I determine (at run time) the type of an Object and how can I use
the obtained result to do a type conversion with CType?
I tried something like:
dim myType as type
myType = sender.GetType
CType(sender, myType)
This however generates a compiler error: Type 'myType' is not defined

What is the proper way to do this?

Jan Warning
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Jan Warning" <ja*********@wanadoo.nl> schrieb:
how can I determine (at run time) the type of an Object and how can I use
the obtained result to do a type conversion with CType?
I tried something like:
dim myType as type
myType = sender.GetType
CType(sender, myType)
This however generates a compiler error: Type 'myType' is not defined

What is the proper way to do this?


There is no way to do this, because the type must be known at compile time.
What exactly do you want to archieve?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

P: n/a
I have a form with a large number of custom controls.
For all these controls I want to use the same Click_event handler.
Depending on certain conditions, the actions to be taken for the click event
can differ slightly for the various controls (e.g. retrieving properties).
So I need to be able to determine the type of the control for which the
eventhandler is invoked.

I know that I can do this by means of "If TypeOf.... is " bu that could
require a lot of writing
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Of**************@TK2MSFTNGP10.phx.gbl...
"Jan Warning" <ja*********@wanadoo.nl> schrieb:
how can I determine (at run time) the type of an Object and how can I use
the obtained result to do a type conversion with CType?
I tried something like:
dim myType as type
myType = sender.GetType
CType(sender, myType)
This however generates a compiler error: Type 'myType' is not defined

What is the proper way to do this?


There is no way to do this, because the type must be known at compile
time. What exactly do you want to archieve?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
Jan,

"Jan Warning" <ja*********@wanadoo.nl> schrieb:
I have a form with a large number of custom controls.
For all these controls I want to use the same Click_event handler.
Depending on certain conditions, the actions to be taken for the click
event can differ slightly for the various controls (e.g. retrieving
properties).
So I need to be able to determine the type of the control for which the
eventhandler is invoked.

I know that I can do this by means of "If TypeOf.... is " bu that could
require a lot of writing


Simply cast the 'sender' to 'Control'. This will allow you to access all
properties and methods defined in the 'Control' class:

\\\
Dim SourceControl As Control = DirectCast(sender, Control)
....
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4

P: n/a
Herfried,
That's what I started with, but as you say, I have access to all methods and
properties of the Control class and not to those of my own custom classes.

I am afraid that I have to think about another solution.

Thanks a lot for your support.

Simply cast the 'sender' to 'Control'. This will allow you to access all
properties and methods defined in the 'Control' class:

\\\
Dim SourceControl As Control = DirectCast(sender, Control)
...
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5

P: n/a

"Jan Warning" <ja*********@wanadoo.nl> wrote in message
news:uB**************@TK2MSFTNGP10.phx.gbl...
I have a form with a large number of custom controls.
For all these controls I want to use the same Click_event handler.
Depending on certain conditions, the actions to be taken for the click
event can differ slightly for the various controls (e.g. retrieving
properties).
So I need to be able to determine the type of the control for which the
eventhandler is invoked.

I know that I can do this by means of "If TypeOf.... is " bu that could
require a lot of writing

Thats the only way to do it though. The compiler must do type checking at
compile-time so that it can verify that the properties that you are using
are valid. You can use reflection to grab properties for unknown types at
run-time, though you will still have to know the type and property name.

Nov 21 '05 #6

P: n/a
"Jan Warning" <ja*********@wanadoo.nl> schrieb:
That's what I started with, but as you say, I have access to all methods
and properties of the Control class and not to those of my own custom
classes.

I am afraid that I have to think about another solution.


You'll have to make sure the method exists in order to call it. What you
can do is adding the method to an interface and implement this interface by
all controls. Then you can check the sender's type using 'TypeOf...Is' ('If
TypeOf sender Is IFoo') and then cast to the interface type in order to be
able to call the method. If you don't want a common interface or base
class, you can use 'CallByName' to call a method/property/... by its name.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.