Operator Overrides 
Author Message
 Operator Overrides

I was just curious, and cannot seem to find this in the doc anywhere...

With VB.NET now, is it possible to override/overload the operators for a
particular class? How in C++, if you have a class called person, you could
maybe override the "+" operator so that a person + another person object
yields a crowd object, or something similar, and perhaps a crowd object - a
person object yields a new crowd object. Dumb examples, i know, but couldnt
think of anything else right now. But anyway, is this possible in VB.NET, or
is that still something thats in the realm of C++?

Im aware of course that the same thing could be done with a method, but
operators create more readable code than methods for the same effect.

Thanks in advance,

Aaron.



Sat, 23 Apr 2005 22:57:04 GMT  
 Operator Overrides


Quote:
> I was just curious, and cannot seem to find this in the doc
anywhere...

> With VB.NET now, is it possible to override/overload the
operators for a
> particular class? How in C++, if you have a class called
person, you could
> maybe override the "+" operator so that a person + another
person object
> yields a crowd object, or something similar, and perhaps a
crowd object - a
> person object yields a new crowd object. Dumb examples, i
know, but couldnt
> think of anything else right now. But anyway, is this

possible in VB.NET, or

Quote:
> is that still something thats in the realm of C++?

It's never been a part of VB (nor any Basic that I'm aware
of.) -- sorry.

Quote:
> Im aware of course that the same thing could be done with a
method, but
> operators create more readable code than methods for the same

effect.

This sounds like a great point of debate.



Sat, 23 Apr 2005 23:03:09 GMT  
 Operator Overrides

Quote:
> It's never been a part of VB (nor any Basic that I'm aware
> of.) -- sorry.

Yah, i know it wasnt in the past... i just thought maybe with the whole
ground-up rewrite and addition of true OO ability, they might have added
that in too. Ah well....


Quote:



> > I was just curious, and cannot seem to find this in the doc
> anywhere...

> > With VB.NET now, is it possible to override/overload the
> operators for a
> > particular class? How in C++, if you have a class called
> person, you could
> > maybe override the "+" operator so that a person + another
> person object
> > yields a crowd object, or something similar, and perhaps a
> crowd object - a
> > person object yields a new crowd object. Dumb examples, i
> know, but couldnt
> > think of anything else right now. But anyway, is this
> possible in VB.NET, or
> > is that still something thats in the realm of C++?

> It's never been a part of VB (nor any Basic that I'm aware
> of.) -- sorry.

> > Im aware of course that the same thing could be done with a
> method, but
> > operators create more readable code than methods for the same
> effect.

> This sounds like a great point of debate.



Sat, 23 Apr 2005 23:11:01 GMT  
 Operator Overrides


Quote:
> > It's never been a part of VB (nor any Basic that I'm aware
> > of.) -- sorry.

> Yah, i know it wasnt in the past... i just thought maybe with
the whole
> ground-up rewrite and addition of true OO ability, they might
have added
> that in too. Ah well....

On the contrary, it could be argued (perhaps not well) that
there's back-sliding on operator overriding.  There's been
_some_ dispute about C# not implementing this as completely as
C++.

C# Programmer's Reference > Overloadable Operators
C# allows user-defined types to overload operators by defining
static member functions using the operator keyword. Not all
operators can be overloaded, however, and others have
restrictions, as listed in this table:

Operators  Overloadability
+, -, !, ~, ++, --, true, false    These unary operators can be
overloaded.
+, -, *, /, %, &, |, ^, <<, >>  These binary operators can be
overloaded.
==, !=, <, >, <=, >=       The comparison operators can be
overloaded (but see note below).
&&, ||                      The conditional logical operators
cannot be overloaded, but they are evaluated using & and |,
which can be overloaded; see 7.11.2 User-defined conditional
logical operators.
[]           The array indexing operator cannot be overloaded,
but you can define indexers.
()          The cast operator cannot be overloaded, but you can
define new conversion operators (see explicit and implicit).
+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=        Assignment
operators cannot be overloaded, but +=, for example, is
evaluated using +, which can be overloaded.
=, ., ?:, ->, new, is, sizeof, typeof         These operators
cannot be overloaded.

Note   The comparison operators, if overloaded, must be
overloaded in pairs; that is, if == is overloaded, != must also
be overloaded. The reverse is also true, and similar for < and

Quote:
>, and for <= and >=.



Sat, 23 Apr 2005 23:34:17 GMT  
 Operator Overrides
Almost everybody here is using VB6 or lower. While you may get a stray
answer to VB.NET questions here, you should ask them in newsgroups
devoted exclusively to .NET programming. Look for newsgroups with the
word "dotnet" in their name. For the microsoft news server, try these
newsgroups...

microsoft.public.dotnet.general
microsoft.public.dotnet.languages.vb

For the news.devx.com news server, try these

vb.dotnet.discussion
vb.dotnet.technical

There are some others, but these should get you started.

Rick - MVP


Quote:
> I was just curious, and cannot seem to find this in the doc
anywhere...

> With VB.NET now, is it possible to override/overload the operators for
a
> particular class? How in C++, if you have a class called person, you
could
> maybe override the "+" operator so that a person + another person
object
> yields a crowd object, or something similar, and perhaps a crowd
object - a
> person object yields a new crowd object. Dumb examples, i know, but
couldnt
> think of anything else right now. But anyway, is this possible in
VB.NET, or
> is that still something thats in the realm of C++?

> Im aware of course that the same thing could be done with a method,
but
> operators create more readable code than methods for the same effect.

> Thanks in advance,

> Aaron.



Sat, 23 Apr 2005 23:20:40 GMT  
 Operator Overrides

Quote:
> Almost everybody here is using VB6 or lower. While you may get a stray
> answer to VB.NET questions here,

<cut>

it is being cross-posted to a dotnet group, it just needs to have the VB
group removed from the list.



Sat, 23 Apr 2005 23:42:12 GMT  
 Operator Overrides


Quote:

message

> > Almost everybody here is using VB6 or lower. While you may
get a stray
> > answer to VB.NET questions here,
> <cut>

> it is being cross-posted to a dotnet group, it just needs to
have the VB
> group removed from the list.

Is it bad form for me to answer him here?


Sat, 23 Apr 2005 23:43:13 GMT  
 Operator Overrides

Quote:




>>> Almost everybody here is using VB6 or lower. While you may get a
>>> stray answer to VB.NET questions here, <cut>

>> it is being cross-posted to a dotnet group, it just needs to have
>> the VB group removed from the list.

> Is it bad form for me to answer him here?

define "here"
There are two groups in the list; the topic is appropriate for the first but
not the second


Sun, 24 Apr 2005 00:24:48 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. operator override question

2. operator override question

3. Overriding Arithmetic Operators (help!)

4. Overriding 'OnPaintBackground' doesn't work! - Overrides-OnPaintBackground.png [0/1]

5. Override field's caption set in table def

6. Override Built in Menu Action?

7. Is it possible to temporarily override an autonumber.

8. Override Regional Settings

9. Override ByRef with ByVal

10. Override Right mouse button?

11. overriding a startup macro

12. Overriding native processing of S/MIME

 

 
Powered by phpBB® Forum Software