Operator Overloading.....C# can do it....


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Operator Overloading.....C# can do it....

  1. #1
    KraKheD Guest

    Operator Overloading.....C# can do it....


    Can VB.Net overload operators?

    I know it can override the equals, tostring, etc. functions, but what about
    operators?


    Thanks in advance.


  2. #2
    Rob Teixeira Guest

    Re: Operator Overloading.....C# can do it....



    You can't create overloaded operators in VB.NET. You can however, use the
    defined operators created in C# (for example, they appear as op_equaility,
    op_inequality, etc. methods).

    I've heard conflicting reports about VB.NET having this feature in the next
    release. Some articles say it will, others report it won't. No definite info
    at this time. I honestly don't think it's necessary for work with VB.NET,
    but there's no technical reason stopping it either.

    -Rob


    "KraKheD" <S@S.com> wrote:
    >
    >Can VB.Net overload operators?
    >
    >I know it can override the equals, tostring, etc. functions, but what about
    >operators?
    >
    >
    >Thanks in advance.
    >



  3. #3
    Phil Weber Guest

    Re: Operator Overloading.....C# can do it....

    > Can VB.Net overload operators?

    No.
    ---
    Phil Weber



  4. #4
    Jonathan Allen Guest

    Re: Operator Overloading.....C# can do it....

    It was on the original published specs for VB7, and some MS people told me
    that they just ran out of time. Since I have not heard otherwise since then,
    I assume it is still on the feature list.

    --
    Jonathan Allen



    "Rob Teixeira" <RobTeixeira@@msn.com> wrote in message
    news:3be96a3c$1@147.208.176.211...
    >
    >
    > You can't create overloaded operators in VB.NET. You can however, use the
    > defined operators created in C# (for example, they appear as op_equaility,
    > op_inequality, etc. methods).
    >
    > I've heard conflicting reports about VB.NET having this feature in the

    next
    > release. Some articles say it will, others report it won't. No definite

    info
    > at this time. I honestly don't think it's necessary for work with VB.NET,
    > but there's no technical reason stopping it either.
    >
    > -Rob
    >
    >
    > "KraKheD" <S@S.com> wrote:
    > >
    > >Can VB.Net overload operators?
    > >
    > >I know it can override the equals, tostring, etc. functions, but what

    about
    > >operators?
    > >
    > >
    > >Thanks in advance.
    > >

    >



  5. #5
    Rob Teixeira Guest

    Re: Operator Overloading.....C# can do it....



    The standards are clearly defined in the SDK. I doubt they would change,
    since in essence, i believe C# would internally look for these as well. However,
    nothing I've read so far indicates that one is "forced" to use these names.

    For anyone how's interested, here's a list of overloadable ops:
    op_Implicit
    op_explicit
    op_Addition
    op_Subtraction
    op_Multiply
    op_Division
    op_Modulus
    op_ExclusiveOr
    op_BitwiseAnd
    op_BitwiseOr
    op_LogicalAnd
    op_LogicalOr
    op_Assign
    op_LeftShift
    op_RightShift
    op_SignedRightShift
    op_UnsignedRightShift
    op_Equality
    op_GreaterThan
    op_LessThan
    op_Inequality
    op_GreaterThanOrEqual
    op_LessThanOrEqual
    op_MultiplicationAssignment
    op_SubtractionAssignment
    op_ExclusiveOrAssignment
    op_LeftShiftAssignment
    op_ModulusAssignment
    op_AdditionAssignment
    op_BitwiseAndAssignment
    op_BitwiseOrAssignment
    op_Comma
    op_DivisionAssignment
    op_Decrement
    op_Increment
    op_UnaryNegation
    op_UnaryPlus
    op_OnesComplement


    >Rob,
    >
    >Do you know if that's a feature of the C# compiler (writing out the
    >op_xxx methods)? If so, couldn't we run into a language that does *not*


    >automatically output the op_xxx methods -- thus limiting the overloaded


    >operators to only languages that can consume overloaded operators?
    >
    >--
    >Patrick Steele
    >Microsoft .NET MVP



  6. #6
    Patrick Steele Guest

    Re: Operator Overloading.....C# can do it....

    In article <3be96a3c$1@147.208.176.211> (from "Rob Teixeira"
    <RobTeixeira@@msn.com> <"Rob Teixeira" <RobTeixeira@@msn.com>>),
    > You can't create overloaded operators in VB.NET. You can however, use the
    > defined operators created in C# (for example, they appear as op_equaility,
    > op_inequality, etc. methods).


    Rob,

    Do you know if that's a feature of the C# compiler (writing out the
    op_xxx methods)? If so, couldn't we run into a language that does *not*
    automatically output the op_xxx methods -- thus limiting the overloaded
    operators to only languages that can consume overloaded operators?

    --
    Patrick Steele
    Microsoft .NET MVP

  7. #7
    Rob Teixeira Guest

    Re: Operator Overloading.....C# can do it....



    If you look at the IL generated by, oh, let's say op_Addition...
    You'll notice that there are a couple of other method attributes: HideBySig
    and SpecialName.
    In particular, SpecialName implies that the name of the method means something.
    You can, for example create an op_Addition method in a VB class/structure,
    but without that attribute, C# won't know what to do with it. So, it's a
    little more than just the name itself.

    Ironically, attributes like SpecialName are not attributes you can use in
    code. The compiler emits it for you. Too bad, because if you could use them
    in code, that would bring VB one step closer to having operator overloading,
    without doing anything new to it (C# would "see" and be able to use operator
    overload methods created in VB, even though you'd still not be able to use
    them with operator symbols in VB).

    -Rob

    "Jonathan Allen" <greywolf@cts.com> wrote:
    >> Do you know if that's a feature of the C# compiler (writing out the
    >> op_xxx methods)?

    >
    >I think operators, like properties, are based in part on method names. The
    >compiler sees something like "get_Item" and thinks "that is a property".

    Or
    >it sees "op_add" and thinks "that is an operator overload".
    >
    >> If so, couldn't we run into a language that does *not*
    >> automatically output the op_xxx methods -- thus limiting the overloaded
    >> operators to only languages that can consume overloaded operators?

    >
    >I do not think you can create a method that does not have a name, which
    >means the overloads would always be available.
    >
    >
    >--
    >Jonathan Allen
    >
    >



  8. #8
    Jonathan Allen Guest

    Re: Operator Overloading.....C# can do it....

    > Do you know if that's a feature of the C# compiler (writing out the
    > op_xxx methods)?


    I think operators, like properties, are based in part on method names. The
    compiler sees something like "get_Item" and thinks "that is a property". Or
    it sees "op_add" and thinks "that is an operator overload".

    > If so, couldn't we run into a language that does *not*
    > automatically output the op_xxx methods -- thus limiting the overloaded
    > operators to only languages that can consume overloaded operators?


    I do not think you can create a method that does not have a name, which
    means the overloads would always be available.


    --
    Jonathan Allen



    "Patrick Steele" <psteele@ipdsolution.com_> wrote in message
    news:MPG.165369a81a8b6f5b98987b@news.devx.com...
    > In article <3be96a3c$1@147.208.176.211> (from "Rob Teixeira"
    > <RobTeixeira@@msn.com> <"Rob Teixeira" <RobTeixeira@@msn.com>>),
    > > You can't create overloaded operators in VB.NET. You can however, use

    the
    > > defined operators created in C# (for example, they appear as

    op_equaility,
    > > op_inequality, etc. methods).

    >
    > Rob,
    >
    > Do you know if that's a feature of the C# compiler (writing out the
    > op_xxx methods)? If so, couldn't we run into a language that does *not*
    > automatically output the op_xxx methods -- thus limiting the overloaded
    > operators to only languages that can consume overloaded operators?
    >
    > --
    > Patrick Steele
    > Microsoft .NET MVP



  9. #9
    Mattias Sjögren Guest

    Re: Operator Overloading.....C# can do it....

    >The standards are clearly defined in the SDK. I doubt they would change,
    >since in essence, i believe C# would internally look for these as well. However,
    >nothing I've read so far indicates that one is "forced" to use these names.
    >
    >For anyone how's interested, here's a list of overloadable ops:



    Some additions - see CLI Partition 1, section 10.3.

    op_UnsignedRightShiftAssignment
    op_RightShiftAssignment
    op_MemberSelection
    op_PointerToMemberSelection
    op_LogicalNot
    op_True
    op_False
    op_AddressOf
    op_PointerDereference


    Mattias

    ===
    Mattias Sjögren (VB MVP)
    mattias @ mvps.org
    http://www.msjogren.net/dotnet/

  10. #10
    Jonathan Allen Guest

    Re: Operator Overloading.....C# can do it....

    > Ironically, attributes like SpecialName are not attributes you can use in
    > code.


    Why, what happens if you try?

    --
    Jonathan Allen



    "Rob Teixeira" <RobTeixeira@@msn.com> wrote in message
    news:3be9a4bf$1@147.208.176.211...
    >
    >
    > If you look at the IL generated by, oh, let's say op_Addition...
    > You'll notice that there are a couple of other method attributes:

    HideBySig
    > and SpecialName.
    > In particular, SpecialName implies that the name of the method means

    something.
    > You can, for example create an op_Addition method in a VB class/structure,
    > but without that attribute, C# won't know what to do with it. So, it's a
    > little more than just the name itself.
    >
    > Ironically, attributes like SpecialName are not attributes you can use in
    > code. The compiler emits it for you. Too bad, because if you could use

    them
    > in code, that would bring VB one step closer to having operator

    overloading,
    > without doing anything new to it (C# would "see" and be able to use

    operator
    > overload methods created in VB, even though you'd still not be able to use
    > them with operator symbols in VB).
    >
    > -Rob
    >
    > "Jonathan Allen" <greywolf@cts.com> wrote:
    > >> Do you know if that's a feature of the C# compiler (writing out the
    > >> op_xxx methods)?

    > >
    > >I think operators, like properties, are based in part on method names.

    The
    > >compiler sees something like "get_Item" and thinks "that is a property".

    > Or
    > >it sees "op_add" and thinks "that is an operator overload".
    > >
    > >> If so, couldn't we run into a language that does *not*
    > >> automatically output the op_xxx methods -- thus limiting the overloaded
    > >> operators to only languages that can consume overloaded operators?

    > >
    > >I do not think you can create a method that does not have a name, which
    > >means the overloads would always be available.
    > >
    > >
    > >--
    > >Jonathan Allen
    > >
    > >

    >



  11. #11
    Rob Teixeira Guest

    Re: Operator Overloading.....C# can do it....



    As far as I can tell, you simply can't. There's no exposed attribute that
    you could use in code, something like:

    <SpecialName()> Public Shared Function op_Addition(. . .

    The only way i've seen to attach these attributes to a method is by using
    the IL and metadata emitting classes (the stuff that compilers use to write
    IL).

    -Rob

    "Jonathan Allen" <greywolf@cts.com> wrote:
    >> Ironically, attributes like SpecialName are not attributes you can use

    in
    >> code.

    >
    >Why, what happens if you try?
    >
    >--
    >Jonathan Allen
    >
    >
    >



  12. #12
    Jonathan Allen Guest

    Re: Operator Overloading.....C# can do it....

    From what I can tell, it is not an attribute but rather a modifier. Still,
    they should have created an Attribute that tells the compiler to export the
    modifier.

    Speaking of modifiers, I heard that C# does not support Friend. Is that
    true, and if so why?

    --
    Jonathan Allen



    "Rob Teixeira" <RobTeixeira@@msn.com> wrote in message
    news:3be9ecdf$1@147.208.176.211...
    >
    >
    > As far as I can tell, you simply can't. There's no exposed attribute that
    > you could use in code, something like:
    >
    > <SpecialName()> Public Shared Function op_Addition(. . .
    >
    > The only way i've seen to attach these attributes to a method is by using
    > the IL and metadata emitting classes (the stuff that compilers use to

    write
    > IL).
    >
    > -Rob
    >
    > "Jonathan Allen" <greywolf@cts.com> wrote:
    > >> Ironically, attributes like SpecialName are not attributes you can use

    > in
    > >> code.

    > >
    > >Why, what happens if you try?
    > >
    > >--
    > >Jonathan Allen
    > >
    > >
    > >

    >



  13. #13
    Willy Van den Driessche Guest

    Re: Operator Overloading.....C# can do it....


    Mattias Sjögren <mattias.dont.want.spam@mvps.org> wrote:
    >>The standards are clearly defined in the SDK. I doubt they would change,
    >>since in essence, i believe C# would internally look for these as well.

    However,
    >>nothing I've read so far indicates that one is "forced" to use these names.
    >>
    >>For anyone how's interested, here's a list of overloadable ops:

    >
    >
    >Some additions - see CLI Partition 1, section 10.3.
    >
    >op_UnsignedRightShiftAssignment
    >op_RightShiftAssignment
    >op_MemberSelection
    >op_PointerToMemberSelection
    >op_LogicalNot
    >op_True
    >op_False
    >op_AddressOf
    >op_PointerDereference
    >
    >
    >Mattias
    >
    >===
    >Mattias Sjögren (VB MVP)
    >mattias @ mvps.org
    >http://www.msjogren.net/dotnet/


    Probably an oversight on my part, but isn't there an operator "()" ?

    Willy.

  14. #14
    Eric Gunnerson Guest

    Re: Operator Overloading.....C# can do it....

    The current runtime doesn't have any concept of friend accessibility.

    "Jonathan Allen" <greywolf@cts.com> wrote in message
    news:3bea1f41@147.208.176.211...
    > From what I can tell, it is not an attribute but rather a modifier. Still,
    > they should have created an Attribute that tells the compiler to export

    the
    > modifier.
    >
    > Speaking of modifiers, I heard that C# does not support Friend. Is that
    > true, and if so why?
    >
    > --
    > Jonathan Allen
    >
    >
    >
    > "Rob Teixeira" <RobTeixeira@@msn.com> wrote in message
    > news:3be9ecdf$1@147.208.176.211...
    > >
    > >
    > > As far as I can tell, you simply can't. There's no exposed attribute

    that
    > > you could use in code, something like:
    > >
    > > <SpecialName()> Public Shared Function op_Addition(. . .
    > >
    > > The only way i've seen to attach these attributes to a method is by

    using
    > > the IL and metadata emitting classes (the stuff that compilers use to

    > write
    > > IL).
    > >
    > > -Rob
    > >
    > > "Jonathan Allen" <greywolf@cts.com> wrote:
    > > >> Ironically, attributes like SpecialName are not attributes you can

    use
    > > in
    > > >> code.
    > > >
    > > >Why, what happens if you try?
    > > >
    > > >--
    > > >Jonathan Allen
    > > >
    > > >
    > > >

    > >

    >




  15. #15
    David Rothgery Guest

    Re: Operator Overloading.....C# can do it....

    Well, it doesn't have any concept of C++-style friend accessibility.
    I thought Friend in vb.NET was pretty much equivalent to internal in C#.

    "Eric Gunnerson" <ericgu_nospam@microsoft.nospam.com> wrote in message
    news:3beabbe2$1@147.208.176.211...
    > The current runtime doesn't have any concept of friend accessibility.
    >
    > "Jonathan Allen" <greywolf@cts.com> wrote in message
    > news:3bea1f41@147.208.176.211...
    > > From what I can tell, it is not an attribute but rather a modifier.

    Still,
    > > they should have created an Attribute that tells the compiler to export

    > the
    > > modifier.
    > >
    > > Speaking of modifiers, I heard that C# does not support Friend. Is that
    > > true, and if so why?
    > >
    > > --
    > > Jonathan Allen
    > >
    > >
    > >
    > > "Rob Teixeira" <RobTeixeira@@msn.com> wrote in message
    > > news:3be9ecdf$1@147.208.176.211...
    > > >
    > > >
    > > > As far as I can tell, you simply can't. There's no exposed attribute

    > that
    > > > you could use in code, something like:
    > > >
    > > > <SpecialName()> Public Shared Function op_Addition(. . .
    > > >
    > > > The only way i've seen to attach these attributes to a method is by

    > using
    > > > the IL and metadata emitting classes (the stuff that compilers use to

    > > write
    > > > IL).
    > > >
    > > > -Rob
    > > >
    > > > "Jonathan Allen" <greywolf@cts.com> wrote:
    > > > >> Ironically, attributes like SpecialName are not attributes you can

    > use
    > > > in
    > > > >> code.
    > > > >
    > > > >Why, what happens if you try?
    > > > >
    > > > >--
    > > > >Jonathan Allen
    > > > >
    > > > >
    > > > >
    > > >

    > >

    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center