CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2006
    Posts
    18

    How to write an operator in VB-style?

    Hi,

    is there any possibility to write an operator member function for a ref class in C++/CLI which can be called directly from Visual Basic using the common Visual Basic syntax? Of course this can be done with an additional (VB-)wrapper class - but without?

    In detail:
    Assume we have a ref class written in C++/CLI, let's call it MyClass.
    In MyClass we have a member function: static bool operator==(const MyClass^ left, const MyClass^ right).
    Now, we make a dll of this project and import it into a VB-programm. Then we can call this operator function from within VB using the following syntax: MyClass.op_Equality(Left, Right) where Left and Right are objects of type MyClass.
    But this it not VB-style syntax.

    Of course we can declare a wrapper class, e.g. VBClass with an operator function:
    Public Shared Shadows Operator =(ByVal Left As VBClass, ByVal Right As VBClass) As Boolean
    in which we call .op_Equality(). Then we can use this wrapper class and call the operator function in VB-syntax style (e.g. If Obj1 == Obj2 Then ...).

    BUT: I don't want to have an additional wrapper class (in my project I already have 3 nested wrapper classes and I don't want to have a fourth one!).

    THEREFORE: Is there any possibility to write an operator member function within C++/CLI in such a way it will be understood from within VB (with it's syntax)?


    Many thanks in advance,

    Physicus.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to write an operator in VB-style?

    You may try to define an operator like the one you want in VB, e.g.

    Code:
    Public Shared Shadows Operator =(ByVal Left As VBClass, ByVal Right As VBClass) As Boolean
    and then use ILDasm to see what signature VB has generated for the operator definition. Once you know that you can try to mimic it in C++/CLI.

    For instance, the signature generated by C++/CLI for

    Code:
    static bool operator==(const MyClass ^left, const MyClass ^right)
    is

    Code:
    .method assembly static bool 
            marshal( unsigned int8) 
            '?A0xb221482d.=='(class MyClass modopt([mscorlib]System.Runtime.CompilerServices.IsConst) left,
                              class MyClass modopt([mscorlib]System.Runtime.CompilerServices.IsConst) right) cil managed
    . But as you said, this one can't be called as an operator from VB. (Hint: It would probably have been easier to find if it was a MyClass member instead of a free function.)

    There may be, however, a much easier way to achieve your goal that I don't know of because I know close to nothing about VB.NET.

    HTH
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Jul 2002
    Posts
    2,543

    Re: How to write an operator in VB-style?

    With MyClass.op_Equality (operator==) method, when you write Left = Right in VB program, does VB call this operator or just compares references? Looking at String.op_Equality Method http://msdn.microsoft.com/en-us/libr...s.80).aspx#Y15, I see that string class just defines operator==, and this is enough for VB and C# compilers to convert = to this operator call.

  4. #4
    Join Date
    Oct 2006
    Posts
    18

    Thumbs up Re: How to write an operator in VB-style?

    @Alex:

    wow!!! You are quite right - it works!
    The reason why I didn't find out this was: I first wrote the operator functions in C++/CLI as object member functions (not static). VB couldn't access them. Later I formulated the member functions as static functions - but then I didn't tried not any longer to use them directly like you suggested.

    Thank you very much! Problem is solved!


    Best regards,

    Physicus.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to write an operator in VB-style?

    Wow! I considered the operator might be a non-static class member (in that case with only one parameter) or a free function (in which case it actually shouldn't be static if it is to be called from another module, on second thought)... but not a static class member, a variant that I've never seen for an operator like this in native C++. (And apparently it doesn't work there anyway; just quick-checked it.)
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured