operator (+) operator

  • Thread starter Thread starter enahar
  • Start date Start date
E

enahar

Dear Sir/Madam,
I have 2 lists as following:

ApproverListBE appList1;

ApproverListBE appList2 ;

I want to add the above 2 lists.How do I override the + operator.



Kind Regards,
 
In your ApproverListBE class add

Public static ApproverListBE operator +(ApproverListBE ob1,
ApproverListBE ob2)
{
// Insert code here to add the variables of class ApproverListBE
together

// return instance of ApproverListBE
}


-----Original Message-----
From: enahar [mailto:[email protected]]
Posted At: Tuesday, 19 April 2005 8:16 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: operator (+) operator
Subject: operator (+) operator

Dear Sir/Madam,
I have 2 lists as following:

ApproverListBE appList1;

ApproverListBE appList2 ;

I want to add the above 2 lists.How do I override the + operator.



Kind Regards,
 
That said, are you really "adding" these two lists? That is, are you
doing a mathematical "+" operation on each item in each list?

Or are you joining the lists together by creating a list that contains
all of the items in both lists?

If I were you I would be very, very leery of overriding the "+"
operator for collections like your lists. In my experience that has
meaning only in vector mathematics.

If you are combining two lists to make a third list that contains
everything in the first two lists (or even folding the contents of a
second list into the first list), then you want to define a static
method like Append(ApproverListBE a, ApproverListBE b), or an instance
method like Append(ApproverListBE otherList) to do the job. Don't use
the "+" operator.
 
I have 2 lists as following:

ApproverListBE appList1;

ApproverListBE appList2 ;

I want to add the above 2 lists.How do I override the + operator.

As Bruce has said, adding lists is not a simple concept. There are three
different set operations available:

Union
The result set contains all the items from both operand sets

Intersection
The result set contains those items that are common to both operand sets

Difference
The result set contains the inverse of an Intersection; those items that are
only found in one or other of the operand sets.

You should also consider whether you want the 'addition' to append one list
to another or to merge the two lists in some implicit order.

For the sanity of those who might have to maintain your code in later years,
I would suggest you do not use operator overloading, but instead use Append
and Merge instance methods; and possibly Union, Intersection and Difference
static methods that take two lists as arguments and return a resulting list.

Joanna
 
Back
Top