Overloading VB & Concatenate operator in C # library

So, I have a DLL that was implemented in VB and I ported it to C #. One of the features of my library was that I had a class that I tried to give the same look and feel as the string. I have overloaded string concatenation operators.

C # uses the + operator to concatenate strings, while VB uses the and operator. Therefore, in the old version of the VB library, I overloaded both operators. Then VB and C # library clients could use it just fine.

When I port the class to C #, I still keep overloading the and operator. However, VB client reports that the operator and the operator are not implemented. It won't compile.

Here is my C # library:

public class Document
{
    private readonly string _body;

    public Document(string body)
    {
        _body = body;
    }

    public static Document operator &(Document lhs, string rhs)
    {
        return new Document(string.Concat(lhs.ToString(), rhs));
    }

    public override string ToString()
    {
        return _body;
    }
}

      

And here is the VB client code that doesn't compile. The error message "Operator" & is not defined for types "Document" and "String".

<TestMethod()>
Public Sub TestMethod1()

    Dim doc = New Document("Hello")

    doc = doc & ", world!"

End Sub

      

Does anyone know how I support the VB operator and concatenation from the C # library?

+3


source to share





All Articles