NeuralJack Posted November 13, 2006 Posted November 13, 2006 Lets say I want to find out the Type of an Object - I often use tags. In order to do this do I really have to instantiate another new object to compare the tag object to? for instance 'Lets say obj is a Tag object imported into the function Private Function IsObjMyClass(obj as Object) as boolean Dim MC1 As New MyClass1 'Do I really need to create an object here? If Object.ReferenceEquals(obj.GetType(), MC1.GetType()) Then Return True Else Return False End If End Function Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
MrPaul Posted November 13, 2006 Posted November 13, 2006 TypeOf operator Use the TypeOf operator: 'Lets say obj is a Tag object imported into the function Private Function IsObjMyClass(obj as Object) as boolean If TypeOf obj Is MyClass1 Then Return True Else Return False End If End Function Or simpler: 'Lets say obj is a Tag object imported into the function Private Function IsObjMyClass(obj as Object) as boolean Return (TypeOf obj Is MyClass1) End Function Good luck :cool: Quote Never trouble another for what you can do for yourself.
NeuralJack Posted November 13, 2006 Author Posted November 13, 2006 Thanks a lot, that's exactly what i'm looking for! Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Leaders snarfblam Posted November 13, 2006 Leaders Posted November 13, 2006 There is one difference between the two methods: If Object.ReferenceEquals(obj.GetType(), MC1.GetType()) Then 'obj is an instance of MyClass1 End If If TypeOf obj Is MyClass1 Then 'obj is instance of MyClass1 or a derived class End If Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.