473,569 Members | 3,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check if an object is a type object

Hi!

I receive an object as a System.Object argument to a method. I need to
check if the object is a Type object or not (that is, not a specific
type, but if the object is a type object in general). How can I do
this? Is there a way of checking if my object inherits from the
System.Type class?

sub myMethod(obj as Object)

if ("obj is a type object")

...do my stuff...

I could not find any solution in any thread in the google groups. I
only find threads about how to determine the Type of an object, but I
need to determine if my object is a Type object!

As far as i understand, I am not able to use the Type.IsInstance OfType
method since the Type class is abstract, i.e.

Type typeObj
typeObj.IsInsta nceOfType(GetTy pe(obj))

does not work.

Thank you for your time!

/Fredrik

Feb 28 '06 #1
13 25838
Hi

This might work

test(GetType(Da taColumn))

Private Sub test(ByVal obj As Object)

MsgBox(obj.GetT ype.ToString)
End Sub

Feb 28 '06 #2
jvb
I would recommend

If TypeOf obj is <Type> then
..
ElseIf TypeOf obj is <Type> then.
..
End If

Feb 28 '06 #3


If you have a routine

Sub fx(obj as object)
end sub

and call it with a type as a parameter

e.g

fx(GetType(Inte ger))

or,

dim str as string = ""
fx(str.GetType( ))
Then the incoming parameter will have a type of

System.RuntimeT ype
The nice way to check would be to compare with that type
if (obj.GetType() is GetType(System. RuntimeType)) then
....
end if

but System.RuntimeT ype is private and not accessible - that's what the
error message says ?

More clumsy, but will work, is to compare with the name

if (obj.GetType(). ToString() = "System.Runtime Type") then
...
end if

will allow you to discern which of the incoming objects are types.
hth,
Alan.

Feb 28 '06 #4
I may missinterpret your answers but,

parez - I cannot use a msgBox to determine manually if the obejct is a
type object, it has to be done automatically.

jvb - If I understand you correctly <Type> should be replaced with the
types I am looking for, but I am looking for all types. If the method
call to my method is:

myMethod(GetTyp e(anyobject))

then I want to be able to determine that it is a type object that I
receive, in contrast to

Dim obj as MyClass

myMethod(obj)

where the obj (probably) is not a type object.

I do not however have any idea of which types the objects may be!

/Fredrik

Feb 28 '06 #5
"AlanT" <al*******@user s.com> ha scritto nel messaggio
news:11******** **************@ p10g2000cwp.goo glegroups.com.. .
More clumsy, but will work, is to compare with the name

if (obj.GetType(). ToString() = "System.Runtime Type") then
...
end if

will allow you to discern which of the incoming objects are types.


Never be esoteric in your code!

What about the

obj.GetType.Equ als(GetType(obj ))

?

or

obj.GetType().I sSubClassOf(Get Type(obj)))

--

Free .Net Reporting Tool - http://www.neodatatype.net
Feb 28 '06 #6

-- In general it is probably better not too open an answer with a
criticism, especially when said criticism is dubious.

GetType() works on Types not objects - GetType(obj) doesn't compile
Unless I totally misunderstood the question, the issue is that objects
will be passed into the function

sub fx(obj as Object)
some of these will be actual objects - ints, strings, user defined
classes.
some of these will be Types

e.g.
fx(GetType(Stri ng))

dim i as integer
fx(i.GetType()) ' FYI, you this is how you find the type or an
object
The OP wanted to ascertain which of the incoming value were Types, not
the type of the incoming parameter.

My answer was (as I said) a touch clumsy, but did the job.

If there is another way to obtain the same result (one that actually
compiles), I would be interested in learning it.
Alan.

Feb 28 '06 #7

"Fredrik Strandberg" <be***********@ hotmail.com> wrote in message
news:11******** *************@u 72g2000cwu.goog legroups.com...
Hi!

I receive an object as a System.Object argument to a method. I need to
check if the object is a Type object or not (that is, not a specific
type, but if the object is a type object in general).


You'd be better off looking into Overloading and writing methods for
/each/ object Type that you want to process but, if you /really/ want
to do it the Hard Way, try this:

Dim thing as New SomeReferenceTy pe

If thing.GetType() .IsSubclassOf( GetType( System.Object ) ) Then
' Reference Type
Else
' Value Type
End If

Can't think why you'd want to though ... ;-)

HTH,
Phill W.
Feb 28 '06 #8
>but System.RuntimeT ype is private and not accessible - that's what the
error message says ?

More clumsy, but will work, is to compare with the name

if (obj.GetType(). ToString() = "System.Runtime Type") then

What's wrong with

If TypeOf obj Is System.Type Then
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 28 '06 #9

Fredrik Strandberg wrote:
Hi!

I receive an object as a System.Object argument to a method. I need to
check if the object is a Type object or not (that is, not a specific
type, but if the object is a type object in general). How can I do
this? Is there a way of checking if my object inherits from the
System.Type class?


The replies to this thread have astonished me.

Does this meet your requirements?

Public Sub whatisit(ByVal o As Object)
If TypeOf o Is Type Then
MsgBox("it's a type")
Else
MsgBox("it's not a type")
End If
End Sub

--
Larry Lard
Replies to group please

Feb 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2002
by: Torsten Mohr | last post by:
Hi, i write a set of extension modules for python in C. I also use some new types that i defined there. At the moment i write some methods that expect some of these types as parameters. I now want to check that the right types were given as parameters. How can i do that?
6
9482
by: Damon | last post by:
Hi, I created a template class and stored its instances in a map object that is like std::map<std::string, void* >. But i just made a nasty discovery that if i fail to use a C-style cast properly with the void pointer to return it to the proper class it belongs to, I get very bad and random segmentation fault. So question to all the...
1
2455
by: Ping | last post by:
Hi, i am trying to : dim db As database and i have Access 2002. The object type "database" is not in the list and error message is "type mismatching " when run. i am wondering if the "database" object type is replaced by some other object or i did not install the office XP correctly.
2
5173
by: MFRASER | last post by:
How can I tell what object type is dropped onto my control? Here is a snippet of code. private void lvwTasks_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { //I want to know if e.Data is of type MyClass1 or MyClass2
2
2117
by: Just D. | last post by:
All, Do we have a simple way to Create an object on the fly knowing just an object type? The usual design-time way is to write a code something like this: CObjectType obj = new CObjectType(); That's simple. But to create any object knowing its object type on the fly is looking like a problem. I'll try to explain the idea.
5
1828
by: Don | last post by:
I have an array of System.type and I need to go through the array and perform different logic depending on the type stored in the array. I want to do: if (typeof typeCollection(i) is String) then 'some logic elseif (typeof typeCollection(i) is int32) then 'some logic .....
6
2197
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time and resource consuming Try... Catch, which in iterative processing is totally unacceptable. -tom
5
24247
bvdet
by: bvdet | last post by:
Python 2.3 Windows XP We have a builtin module 'point'. I am trying to determine a better way to check the type of a list of 'point' objects. A point object is created:from point import Point pt1 = Point(x_coord, y_coord, z_coord) print type(pt1) <type 'point'> print dir(pt1) Original code: def chk_type(p_list): ...
6
1970
by: Deckarep | last post by:
I want to be able to pass in a function a string say: "TextBox" Then I need a way to convert that string representation into a Type object so i can search through some controls and check their type. Then I can do my check: If ( control is type) { //continue } I'm doing this inside of a recursive function which goes through the...
0
7703
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...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
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. ...
0
8138
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...
1
7681
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...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5228
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...
0
3662
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...
1
2118
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

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.