Home All Groups Group Topic Archive Search About

Operators do not work in VB.NET with a C# referenced assembly

Author
30 Mar 2006 1:23 PM
mkeggen
Unfortunately due to project constraints I have to code in VB.NET of
which I have little experience with.  I have a C# assembly referenced
which contains an object where the + and - operators have been
successfully overridden and in my other C# projects the following code
(changed for posting) compiles and works:

Field TheField = this.GetField(1);
TheField += this.GetField(2);
Etc...

When I attempt to do something similar in VB.NET I get the following
compiler error "Operator '+' is not defined for types 'Field' and
'Field'"
I've tried different variations of the code with no joy - can
anyone assist and tell me what I am doing wrong?

Dim TheField As Field = Me.GetField(1)
TheField += Me.GetField(2)
TheField = (TheField + me.GetField(3))

Thanks in advance

Author
30 Mar 2006 1:46 PM
Marina Levit [MVP]
VB.NET (at least in 1.1, not sure about 2.0)  doesn't support operator
overloading.

So, to use it, you would have to call the methods that are generated that
represent the operators.  If you use intellisense, you should be able to
find them. There is a naming convention for them (which unfortunately I
don't remember), but it would have the operator name in it. But in the end,
you would have to call a method, you wouldn't be able to use the operators.

<mkeg***@hotmail.com> wrote in message
Show quoteHide quote
news:1143724994.930248.6860@e56g2000cwe.googlegroups.com...
> Unfortunately due to project constraints I have to code in VB.NET of
> which I have little experience with.  I have a C# assembly referenced
> which contains an object where the + and - operators have been
> successfully overridden and in my other C# projects the following code
> (changed for posting) compiles and works:
>
> Field TheField = this.GetField(1);
> TheField += this.GetField(2);
> Etc...
>
> When I attempt to do something similar in VB.NET I get the following
> compiler error "Operator '+' is not defined for types 'Field' and
> 'Field'"
> I've tried different variations of the code with no joy - can
> anyone assist and tell me what I am doing wrong?
>
> Dim TheField As Field = Me.GetField(1)
> TheField += Me.GetField(2)
> TheField = (TheField + me.GetField(3))
>
> Thanks in advance
>
Author
30 Mar 2006 1:48 PM
Patrice
Actually behing the scene .NET creates a method named "opp_add" (if I
remember). In VB.NET 2003, you have to call explicitely this method (is this
what you are using, I believe VB 2005 does this for you automatically when
the operator is used).

--
Patrice

<mkeg***@hotmail.com> a écrit dans le message de news:
1143724994.930248.6***@e56g2000cwe.googlegroups.com...
Show quoteHide quote
> Unfortunately due to project constraints I have to code in VB.NET of
> which I have little experience with.  I have a C# assembly referenced
> which contains an object where the + and - operators have been
> successfully overridden and in my other C# projects the following code
> (changed for posting) compiles and works:
>
> Field TheField = this.GetField(1);
> TheField += this.GetField(2);
> Etc...
>
> When I attempt to do something similar in VB.NET I get the following
> compiler error "Operator '+' is not defined for types 'Field' and
> 'Field'"
> I've tried different variations of the code with no joy - can
> anyone assist and tell me what I am doing wrong?
>
> Dim TheField As Field = Me.GetField(1)
> TheField += Me.GetField(2)
> TheField = (TheField + me.GetField(3))
>
> Thanks in advance
>
Author
30 Mar 2006 2:54 PM
Claes Bergefall
The generated method is called op_Addition.

In 2003 you must use this method:
Field1 = Field.op_Addition(Field2, Field3)

In 2005 you can use + just as in C#.:
Field1 = Field2 + Field3


  /claes


Show quoteHide quote
"Patrice" <a@bc.c> wrote in message
news:uPnuYCAVGHA.2156@tk2msftngp13.phx.gbl...
> Actually behing the scene .NET creates a method named "opp_add" (if I
> remember). In VB.NET 2003, you have to call explicitely this method (is
> this what you are using, I believe VB 2005 does this for you automatically
> when the operator is used).
>
> --
> Patrice
>
> <mkeg***@hotmail.com> a écrit dans le message de news:
> 1143724994.930248.6***@e56g2000cwe.googlegroups.com...
>> Unfortunately due to project constraints I have to code in VB.NET of
>> which I have little experience with.  I have a C# assembly referenced
>> which contains an object where the + and - operators have been
>> successfully overridden and in my other C# projects the following code
>> (changed for posting) compiles and works:
>>
>> Field TheField = this.GetField(1);
>> TheField += this.GetField(2);
>> Etc...
>>
>> When I attempt to do something similar in VB.NET I get the following
>> compiler error "Operator '+' is not defined for types 'Field' and
>> 'Field'"
>> I've tried different variations of the code with no joy - can
>> anyone assist and tell me what I am doing wrong?
>>
>> Dim TheField As Field = Me.GetField(1)
>> TheField += Me.GetField(2)
>> TheField = (TheField + me.GetField(3))
>>
>> Thanks in advance
>>
>
>