determining object type

J

Jan Warning

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
 
H

Herfried K. Wagner [MVP]

Jan Warning said:
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?
 
J

Jan Warning

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
 
H

Herfried K. Wagner [MVP]

Jan,

Jan Warning said:
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)
....
///
 
J

Jan Warning

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

Rick Mogstad

Jan Warning said:
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.
 
H

Herfried K. Wagner [MVP]

Jan Warning said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top