5

I have an incredibly unique problem here. Our business application has been built using c# and vb.net. We have been trying to approach a standard and trim the fat for some of our core, already duplicated, objects. We are getting really close, but when trying to consolidate a duplicate object into c# our vb.net code now starts throwing the error "Operator '&' is not defined for type 'CSType' and 'String', when I try to do vb.net string concatenation using the ampersand(&). The funny thing is that if I use the '&' in c# with CSType (after properly overloading it) i get the string concatenation I expect.

Here are my basic overloads on CSType:

public static string operator &(CSType c1, string s2)
{
    return c1.ToString() + s2;
}
public static string operator &(string s1, CSType c2) 
{
    return s1 + c2.ToString();
}

When I run the '&' operator in c# with a CSType and a string, I get the expected results, when I attempt to execute that in vb.net the code will not compile giving me an error that:

"Operator '&' is not defined for types 'CSType' and 'String'"

CSType also implicitly converts to most data types, so I was thinking that there may have been some issue with the '&' assuming that it was a bitwise operator, but I would guess that would fails by giving me messed up execution, not a compile error.

Anyway, I'm of half a mind to place this class in c++ where I know I can get what I need out of it, but isn't 2 languages enough already.

3
  • 8
    & in C# overloads bitwise AND. Commented Jun 11, 2014 at 23:40
  • 1
    (I think the implication here is that you'll need to provide a cast operator overload for string and use CType in VB.NET.) Commented Jun 12, 2014 at 0:28
  • Don't use operator overloading for business applications. :) Commented Jun 12, 2014 at 0:52

1 Answer 1

5

The & operator in C# is the bitwise AND operator. So when you overload it like

public static string operator &(CSType c1, string s2)
{
    return c1.ToString() + s2;
}
public static string operator &(string s1, CSType c2) 
{
    return s1 + c2.ToString();
}

you can use it in VB.Net with the And operator:

Dim a = New CSType("Foo")
Dim b = "Bar"
Dim c = a And b

However, to overload VB.Net's & operator outside of VB.Net (e.g. C#), you have to create a method named op_Concatenate and use the SpecialName attribute:

[SpecialName]
public static string op_Concatenate(CSType c1, string s2)
{
    return c1.ToString() + s2;
}

[SpecialName]
public static string op_Concatenate(string s1, CSType c2)
{
    return s1 + c2.ToString();
}

Then the following code will work:

Dim a = New CSType("Foo")
Dim b = "Bar"
Dim c = a & b
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this was incredibly helpful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.