473,729 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4328
"Jan Warning" <ja*********@wa nadoo.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******** ******@TK2MSFTN GP10.phx.gbl...
"Jan Warning" <ja*********@wa nadoo.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*********@wa nadoo.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(send er, 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(send er, 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*********@wa nadoo.nl> wrote in message
news:uB******** ******@TK2MSFTN GP10.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*********@wa nadoo.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
12197
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 on which type of object it gets. Object types to be handled include windows, documents, locations, links, images, layers, and so on. I'm using Mozilla, but I'm looking for a platform-independent solution. I know of four potential approaches to...
3
1922
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) { switch (TypeCode) case 1:
0
1690
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 System.Runtime.InteropServices.COMException (0x80004005): The client
7
5565
by: Chuck Bowling | last post by:
Is there some way to determine what base class an instance of a object was derived from?
2
3643
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
11950
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 "DirectoryEntry" objects within the path that I specify (either using the DirectoryEnrtry.Children or using the DirectorySearcher class) and all started well and dandy! Now the problem; some of the properties of the DirectoryEntry objects being...
9
1633
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
10304
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 number, isNaN returns false, regardless of whether I declared it as "5" or 5.
53
4954
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
1656
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: if hasattr(clr, '__lt__'): However - in Python 2.6 object has grown a default implementation of
0
8913
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
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...
1
9200
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
9142
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
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
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...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2162
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.