473,386 Members | 1,801 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,386 software developers and data experts.

determining object type

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
6 4300
"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
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
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
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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: James Marshall | last post by:
I need to detect the type of an object, more than just "object" as typeof gives us. I'm writing a general handler that accepts a variety of objects and properties, and acts accordingly depending...
3
by: Steve Brown | last post by:
Hello all, Is there a way to determine a variable's type at run-time? The reason I'm asking is that i have code that looks like this: template <class T> Object::Object(int TypeCode, T* data)...
0
by: CTDev Team | last post by:
Hi, We are using Exchange Server 5.5, and have applications written in VB6 and C# that read and process emails. We are experiencing intermittent errors similar to C# Application ...
7
by: Chuck Bowling | last post by:
Is there some way to determine what base class an instance of a object was derived from?
2
by: Garrett | last post by:
Need any help in determining which groups have been given security access to a folder. Searched DirectoryServices to no avail... Any Help?
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
9
by: Clay_Culver | last post by:
I need to find out if an object is a class. Using new style classes this is very easy: class Test(object): pass obj = Test or obj = Test() if type(obj) == type:
4
by: mouseit | last post by:
If I have a variable of some sort, say x, how can I find out what variable type javascript thinks it is? For example, if I've declared x as 5 earlier, how do I know now whether it's a string or a...
53
by: Aaron Gray | last post by:
Due to M$'s stupidity in not making DOMElements first class citizens the following will not work :- function isElement( o) { return o instanceof Element } It works for FF, Opera and Safari.
11
by: Fuzzyman | last post by:
Hello all, I may well be being dumb (it has happened before), but I'm struggling to fix some code breakage with Python 2.6. I have some code that looks for the '__lt__' method on a class: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...

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.