Overloading vb.net & operator in C # class

I have an incredibly unique problem. Our business application is built using C # and vb.net. We tried to get closer to the standard and trim fat for some of our main, already duplicated objects. We become very close, but when trying to consolidate a duplicated object in C #, our vb.net code now starts throwing an "Operator" error & is not defined for the "CSType" and "String" types when I try to concatenate a vb.net string using ampersand (&). The funny thing is that if I use '&' in C # with CSType (after the correct overload) I get the string concatenation that I expect.

Here are my main overloads in 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 '&' statement in C # with CSType and a string, I get the expected results, when I try to execute this in vb.net, the code will not compile, giving me an error that:

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

CSType is also implicitly converted to most data types, so I thought there might have been a problem with '&' suggesting that it was a bitwise operator, but I would assume that this would not work if I mixed up the execution and not the compilation error .

Anyway, I do half the mind to put this class in C ++, where I know that I can get what I need, but two languages ​​are not enough.

+4
source share
1

& # . , ,

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

VB.Net And:

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

, VB.Net & VB.Net(, #), op_Concatenate SpecialName:

[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();
}

:

Dim a = New CSType("Foo")
Dim b = "Bar"
Dim c = a & b
+4

Source: https://habr.com/ru/post/1544244/


All Articles