473,327 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Operator Overload question for VB 2005

I am creating a class with operator overloads. It makes references to another
class I've created, but do not wish to modify. I try to overload an operator
having arguments having the other class type, but none of the new class type.
However it does return a type of the new class. The compiler tells me that at
least one parameter must be of the containing type. Is there a way to do
this? The other class hos no reference to this new class. Is there a way to
overload operators in the other class within the definition for the new
class? The overload I'm trying to create does not conflict with existing
overloads in the other class.
Jul 21 '05 #1
5 2032
bsaucer
It sounds like you created two classes, correct?

And you want to create an overloaded operator that expects one class &
returns the second, correct?

Have you tried:

' VB.NET 2005 syntax

Public Class FirstClass
End Class

Public Class OtherClass
Public Shared Operator +(ByVal otherClass1 As OtherClass, ByVal
otherClass2 As OtherClass) As FirstClass
End Operator
End Class

Note the overloaded operator is in the class that the parameters are, not
the class of the return type.

Hope this helps
Jay

"bsaucer" <bs*****@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
I am creating a class with operator overloads. It makes references to
another
class I've created, but do not wish to modify. I try to overload an
operator
having arguments having the other class type, but none of the new class
type.
However it does return a type of the new class. The compiler tells me that
at
least one parameter must be of the containing type. Is there a way to do
this? The other class hos no reference to this new class. Is there a way
to
overload operators in the other class within the definition for the new
class? The overload I'm trying to create does not conflict with existing
overloads in the other class.

Jul 21 '05 #2
The problem is, the "other class" (written first) has no "knowledge" of the
new class. The new class does have knowledge of the older class.
Theoretically, the old class is "off limits", as if it were written by
someone else while I develop the new class.

I have found a workaround: I used a "widening ctype" operator in my new
class to allow the calling routine to use arguments of the other class, so I
no longer need to to overload all my arithmetic operators specifying the
other arguments.

"Jay B. Harlow [MVP - Outlook]" wrote:
bsaucer
It sounds like you created two classes, correct?

And you want to create an overloaded operator that expects one class &
returns the second, correct?

Have you tried:

' VB.NET 2005 syntax

Public Class FirstClass
End Class

Public Class OtherClass
Public Shared Operator +(ByVal otherClass1 As OtherClass, ByVal
otherClass2 As OtherClass) As FirstClass
End Operator
End Class

Note the overloaded operator is in the class that the parameters are, not
the class of the return type.

Hope this helps
Jay

"bsaucer" <bs*****@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
I am creating a class with operator overloads. It makes references to
another
class I've created, but do not wish to modify. I try to overload an
operator
having arguments having the other class type, but none of the new class
type.
However it does return a type of the new class. The compiler tells me that
at
least one parameter must be of the containing type. Is there a way to do
this? The other class hos no reference to this new class. Is there a way
to
overload operators in the other class within the definition for the new
class? The overload I'm trying to create does not conflict with existing
overloads in the other class.


Jul 21 '05 #3
bsaucer,
The problem is, the "other class" (written first) has no "knowledge" of
the
new class. Ah! there's the rub!
I have found a workaround: I used a "widening ctype" operator in my new Just be aware that a "widening ctype" operator may cause implicit
conversions when you do least expect them! (for example during an assignment
or being passed as a parameter to another method (other then one of your
overloaded operators).

If possible I would modify the "other class"! to avoid the implicit
conversions!

For example:

The DateTime operator - returns a TimeSpan, TimeSpan does not have an
implicit ("widening ctype") from DateTime. As a "widening ctype" on TimeSpan
from DateTime could cause lose of the Date part of the DateTime. TimeSpan
could have a narrowing ctype as TimeSpan represents less data then
DateTime...

NOTE: There are times when implicit conversions do make sense, such as an
implicit conversion from Double to Complex (where Complex is a type that
represents a complex number).

Hope this helps
Jay

"bsaucer" <bs*****@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com... The problem is, the "other class" (written first) has no "knowledge" of
the
new class. The new class does have knowledge of the older class.
Theoretically, the old class is "off limits", as if it were written by
someone else while I develop the new class.

I have found a workaround: I used a "widening ctype" operator in my new
class to allow the calling routine to use arguments of the other class, so
I
no longer need to to overload all my arithmetic operators specifying the
other arguments.

"Jay B. Harlow [MVP - Outlook]" wrote:
bsaucer
It sounds like you created two classes, correct?

And you want to create an overloaded operator that expects one class &
returns the second, correct?

Have you tried:

' VB.NET 2005 syntax

Public Class FirstClass
End Class

Public Class OtherClass
Public Shared Operator +(ByVal otherClass1 As OtherClass, ByVal
otherClass2 As OtherClass) As FirstClass
End Operator
End Class

Note the overloaded operator is in the class that the parameters are, not
the class of the return type.

Hope this helps
Jay

"bsaucer" <bs*****@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
>I am creating a class with operator overloads. It makes references to
>another
> class I've created, but do not wish to modify. I try to overload an
> operator
> having arguments having the other class type, but none of the new class
> type.
> However it does return a type of the new class. The compiler tells me
> that
> at
> least one parameter must be of the containing type. Is there a way to
> do
> this? The other class hos no reference to this new class. Is there a
> way
> to
> overload operators in the other class within the definition for the new
> class? The overload I'm trying to create does not conflict with
> existing
> overloads in the other class.


Jul 21 '05 #4
Your note describes the situation, where an object of the original class CAN
be "upgraded" to the new class. I was going to use that anyway. I was just
surprised that it enabled me to eliminate several "redundant" operator
overloads!
NOTE: There are times when implicit conversions do make sense, such as an
implicit conversion from Double to Complex (where Complex is a type that
represents a complex number).


Note that the double class has no "knowledge" of complex numbers, and
therefore makes no references to the complex class. However, the complex
class would make heavy use of doubles. You shouldn't have to modify the class
of doubles in any way to accomodate complex numbers.
Jul 21 '05 #5
bsaucer,
Your note describes the situation, where an object of the original class
CAN
be "upgraded" to the new class. I don't follow what you mean by "CAN be "upgraded"", do you mean an implicit
conversion is "well defined" between the two classes?
Note that the double class has no "knowledge" of complex numbers, and Correct, the double class has *NO* knowledge of complex numbers! You add or
subtract (or any other operator) two double numbers together and you still
have a double!

However defining an implicit conversion from Double to Complex makes sense
as its "well defined".

Where as in the case of DateTime if you subtract two DateTimes you have a
TimeSpan, its doesn't completely make sense to define the result of
subtracting two DateTimes in the terms of a DateTime (as you actually have a
duration & not an actual DateTime, hence the TimeSpan type)...

Likewise implicitly converting a DateTime to or from a TimeSpan does not
really make sense either.

NOTE: I'm not saying its wrong to have the implicit conversion, I'm just
saying make sure the implicit conversion makes sense in all cases... Those
cases are, but not limited to, assignments & parameter passing.

Hope this helps
Jay
"bsaucer" <bs*****@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com... Your note describes the situation, where an object of the original class
CAN
be "upgraded" to the new class. I was going to use that anyway. I was just
surprised that it enabled me to eliminate several "redundant" operator
overloads!
NOTE: There are times when implicit conversions do make sense, such as an
implicit conversion from Double to Complex (where Complex is a type that
represents a complex number).


Note that the double class has no "knowledge" of complex numbers, and
therefore makes no references to the complex class. However, the complex
class would make heavy use of doubles. You shouldn't have to modify the
class
of doubles in any way to accomodate complex numbers.

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Shaun | last post by:
Hi, I'm trying to overload the divide operator in python for basic arithmetic. eg. 10/2 ... no classes involved. I am attempting to redefine operator.__div__ as follows: # my divide...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
5
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
16
by: Norman Diamond | last post by:
In an antique obsolete version of MFC, a CString expression could be subscripted in order to retrieve one element. Visual Studio 2005 defines CSimpleStringT::operator. At first glance it looks...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
5
by: phiefer3 | last post by:
I'm currently a student, but this problem isn't directly related to what I have to do on an assignment. It's just a problem I've had with some supporting features. First of all, I'm using MSVS...
2
by: rn5a | last post by:
I use VB.NET to create ASP.NET apps. If I am not wrong, there is something called method overloading in VB.NET (like in C#) which is one of the features in OOP (polymorphism) but does VB.NET also...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.