473,698 Members | 2,222 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 4324
"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
12194
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
1919
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
1685
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
5563
by: Chuck Bowling | last post by:
Is there some way to determine what base class an instance of a object was derived from?
2
3640
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
11942
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
1630
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
10302
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
4946
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
1652
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
8674
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
9028
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...
0
8861
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...
1
6518
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
5860
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
4369
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...
1
3046
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
2
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
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.