Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Understanding operator overloading
Message
General information
Forum:
Visual FoxPro
Category:
Visual FoxPro and .NET
Miscellaneous
Thread ID:
01064520
Message ID:
01065117
Views:
19
I'm glad you got it figured out. If I had had more time, I might have messed around a little with it myself and been able to give you a better answer. =)

~~Bonnie



>Greetings Bonnie,
>
>Thanks for your reply. I had seen that example in the help, but hadn't been able to figure out how to adapt my situation to it. A co-worker helped me figure it out. hc is the Hand and dc is the card returned drom the Deck.DealCard()
>
>public static Hand operator +(Hand hc, Card dc)
>{
> hc.Cards[hc.cardcount] = dc
> hc.cardcount++;
> return (hc);
>}
>
>>There's a .NET forum here too, where this post might have gotten a quicker response. Anyway, have you read the Help topics on operator overloading? How have they explained it to you in the C# class that you're taking? I've not actually done any operator overloading, but it seems pretty straightforward. I found some examples in the Help, here's an example of an overloaded + operator in a struct (could be used in a class as well, like your Hand class):
>>
>>public struct Complex
>>{
>>   public int real;
>>   public int imaginary;
>>
>>   public Complex(int real, int imaginary)
>>   {
>>      this.real = real;
>>      this.imaginary = imaginary;
>>   }
>>
>>   // Declare which operator to overload (+), the types
>>   // that can be added (two Complex objects), and the
>>   // return type (Complex):
>>   public static Complex operator +(Complex c1, Complex c2)
>>   {
>>      return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
>>   }
>>   // Override the ToString method to display an complex number in the suitable format:
>>   public override string ToString()
>>   {
>>      return(String.Format("{0} + {1}i", real, imaginary));
>>   }
>>
>>This should maybe get you started.
>>
>>~~Bonnie
>>
>>
>>>This is more a VFPer learning .Net instead of VFP and .Net.
>>>
>>>I've started a C# class and I'm having some difficulty with operator overloading.
>>>
>>>It's a card game (great real-world example, huh) with a Deck class and a Hand class. Both have card arrays that contain card objects. I'm trying to overload the + operator of the Hand class so that I can get a card from the deck array into the hand array. This is a requirement of the lab, not something I decided to do on my own.
>>>
>>>Hand h1 = new Hand();
>>>Deck d = new Deck();
>>>
>>>//Should add a card object to h1.Cards array
>>>h1+= d.DealCard();
>>>
>>>Anybody? Anybody? Bueller?
>>>
>>>Thanks,
>>>
>>>E.R.
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform