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

  • Thread starter Thread starter mkeggen
  • Start date Start date
M

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
 
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.
 
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).
 
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
 
Back
Top