Consuming operator overloaded c#

N

Nick

I have the following wrapper classed defined in a C# assembley namely:

public class wrapInt32
{
private Int32 m_Int32;
//additional object composition
public wrapInt32(){m_Int32 = 0;}
public integer Value()
{
return m_Int32;
}
//other functions
public static wrapInt32 operator +(wrapInt32 x, wrapInt32 y)
{
x.value + y.value;
return x;
}
//other overloads
}

I can consume this no problem in a parent c# application in fx version 1.1
with:

private void Form1_Load(object sender, System.EventArgs e)
{
wrapInt32 a, b, c;
a = new wrapInt32();
b = new wrapInt32();
c = new wrapInt32();
a.Value = 1;
b.Value = 2;
c = a + b;
}

BUT not in VB.NET!

Private Sub myMain()
Dim a, b, c As wrapInt32
a = New wrapInt32
b = New wrapInt32
c = New wrapInt32

a.Value = 1
b.Value = 2
c = a + b
End Sub


This produces the following error:
Operator '+' is not defined for types 'wrapInt32' and 'wrapInt32'.

Both projects reference the same assembley (first thing to check!) and
version. As far as I was aware, VB.NET could consume overloaded operators
but not create them. Am I right 'in theory' or do I have an implementation
bug to track down somewhere?

Regards
 
H

Herfried K. Wagner [MVP]

Nick said:
Private Sub myMain()
Dim a, b, c As wrapInt32
a = New wrapInt32
b = New wrapInt32
c = New wrapInt32

a.Value = 1
b.Value = 2
c = a + b
End Sub


This produces the following error:
Operator '+' is not defined for types 'wrapInt32' and 'wrapInt32'.

VB 2002/2003:

\\\
c = wrapInt32.op_Addition(a, b)
///

VB 2005 will support operator overloading, so the code you posted will work
then without using 'op_Addition'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top