Home All Groups Group Topic Archive Search About

Problem using '=' with System.Type

Author
23 Mar 2010 1:27 AM
John
Hi

I have the following code;

ByVal MyType As Type
Dim frame As StackFrame
If frame.GetMethod().DeclaringType = MyType Then

Problem is I am getting the "Operator '=' is not defined for types
'System.Type' and 'System.Type'." error on the last line. How can I fix
this?

Thanks

Regards

Author
23 Mar 2010 1:38 AM
Armin Zingler
Am 23.03.2010 02:27, schrieb John:
> Hi
>
> I have the following code;
>
> ByVal MyType As Type
> Dim frame As StackFrame
> If frame.GetMethod().DeclaringType = MyType Then
>
> Problem is I am getting the "Operator '=' is not defined for types
> 'System.Type' and 'System.Type'." error on the last line. How can I fix
> this?

To compare references, use the 'Is' operator:

  If frame.GetMethod().DeclaringType Is MyType


--
Armin
Author
23 Mar 2010 8:34 AM
Göran_Andersson
Armin Zingler wrote:
Show quoteHide quote
> Am 23.03.2010 02:27, schrieb John:
>> Hi
>>
>> I have the following code;
>>
>> ByVal MyType As Type
>> Dim frame As StackFrame
>> If frame.GetMethod().DeclaringType = MyType Then
>>
>> Problem is I am getting the "Operator '=' is not defined for types
>> 'System.Type' and 'System.Type'." error on the last line. How can I fix
>> this?
>
> To compare references, use the 'Is' operator:
>
>   If frame.GetMethod().DeclaringType Is MyType
>

The Is operator works for some purposes, but it doesn't do an equality
comparison. For example, (String Is Object) = True, while (Object Is
String) = False.

--
Göran Andersson
_____
http://www.guffa.com
Author
23 Mar 2010 11:27 AM
Armin Zingler
Am 23.03.2010 09:34, schrieb Göran Andersson:
Show quoteHide quote
> Armin Zingler wrote:
>> Am 23.03.2010 02:27, schrieb John:
>>> Hi
>>>
>>> I have the following code;
>>>
>>> ByVal MyType As Type
>>> Dim frame As StackFrame
>>> If frame.GetMethod().DeclaringType = MyType Then
>>>
>>> Problem is I am getting the "Operator '=' is not defined for types
>>> 'System.Type' and 'System.Type'." error on the last line. How can I fix
>>> this?
>>
>> To compare references, use the 'Is' operator:
>>
>>   If frame.GetMethod().DeclaringType Is MyType
>>
>
> The Is operator works for some purposes, but it doesn't do an equality
> comparison. For example, (String Is Object) = True, while (Object Is
> String) = False.

Help me understanding it. :) John wants to check if it's the same System.Type
object, right?  So your example with String and Object would be:

   dim t1 = gettype(string)
   dim t2 = gettype(object)

   msgbox (t1 is t2) 'False
   msgbox (t2 is t1) 'False

He does not want to check if the type of an object is of a
certain type or derived from that type. But maybe I got it wrong.



--
Armin
Author
29 Mar 2010 11:36 AM
Göran_Andersson
Armin Zingler wrote:
Show quoteHide quote
> Am 23.03.2010 09:34, schrieb Göran Andersson:
>> Armin Zingler wrote:
>>> Am 23.03.2010 02:27, schrieb John:
>>>> Hi
>>>>
>>>> I have the following code;
>>>>
>>>> ByVal MyType As Type
>>>> Dim frame As StackFrame
>>>> If frame.GetMethod().DeclaringType = MyType Then
>>>>
>>>> Problem is I am getting the "Operator '=' is not defined for types
>>>> 'System.Type' and 'System.Type'." error on the last line. How can I fix
>>>> this?
>>> To compare references, use the 'Is' operator:
>>>
>>>   If frame.GetMethod().DeclaringType Is MyType
>>>
>> The Is operator works for some purposes, but it doesn't do an equality
>> comparison. For example, (String Is Object) = True, while (Object Is
>> String) = False.
>
> Help me understanding it. :) John wants to check if it's the same System.Type
> object, right?  So your example with String and Object would be:
>
>    dim t1 = gettype(string)
>    dim t2 = gettype(object)
>
>    msgbox (t1 is t2) 'False
>    msgbox (t2 is t1) 'False
>
> He does not want to check if the type of an object is of a
> certain type or derived from that type. But maybe I got it wrong.
>

No, you are right. I was thinking that the Is operator is comparing the
types, but it is comparing the Type objects.

--
Göran Andersson
_____
http://www.guffa.com
Author
24 Mar 2010 12:27 AM
Mark Hurd
"Göran Andersson" <gu***@guffa.com> wrote in message
news:OMZtZOmyKHA.3884@TK2MSFTNGP06.phx.gbl...
<snip>
> The Is operator works for some purposes, but it doesn't do an equality
> comparison. For example, (String Is Object) = True, while (Object Is
> String) = False.

Could you be thinking of the C# Is operator?

Show quoteHide quote
> --
> Göran Andersson

--
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)
Author
23 Mar 2010 8:31 AM
Göran_Andersson
John wrote:
Show quoteHide quote
> Hi
>
> I have the following code;
>
> ByVal MyType As Type
> Dim frame As StackFrame
> If frame.GetMethod().DeclaringType = MyType Then
>
> Problem is I am getting the "Operator '=' is not defined for types
> 'System.Type' and 'System.Type'." error on the last line. How can I fix
> this?
>
> Thanks
>
> Regards
>

VB has it's own implementation of the = operator, so it doesn't always
work as you might expect.

Use the Equals method to use it's own built in comparison to compare the
types:

   If frame.GetMethod().DeclaringType.Equals(MyType) Then

--
Göran Andersson
_____
http://www.guffa.com