c# - Operator Overloading with different types -


ok. i'm trying figure out how implement overloaded operator. i've searched tutorials there's i'm missing. maybe it's 1 of 'obvious' things i'm thinking hard about. don't know. why i'm here.

here's overloaded operator:

public static bool operator +(hero h, monster m)      {         if (!h.isrunningaway)         {             if(h.attackspeed > m.attackspeed)             {                 m.takesdamage(h.attackvalue());                 if (m.isalive())                 {                     h.takesdamage(m.attackvalue);                 }             }             else if(h.attackspeed < m.attackspeed)             {                 h.takesdamage(m.attackvalue);                 if (h.isalive())                 {                     m.takesdamage(h.attackvalue());                 }             }             else             {                 h.takesdamage(m.attackvalue);                 m.takesdamage(h.attackvalue());             }         }         else         {             if(h.attackspeed <= m.attackspeed)             {                 h.takesdamage(m.attackvalue);             }                       }         h.isrunningaway = false;         return h.isalive();     } 

now, i'm trying implement in button click event in form.

 private void btnattack_click(object sender, routedeventargs e)     {      } 

tutorials i've seen aren't being particularly clear on how it. said, maybe overthinking it. do.

if question can edited clearer, or else needed, let me know. i'm glad edit.

do not use operator overloading here!

operator overloading should used when makes sense. example, - operator can overloaded on datetime. when subtract 1 datetime another, makes sense time interval:

25/4/2016 - 24/4/2016 = 1 day 

but in case, want addition on hero , monster, and want return bool! makes no sense @ all! can tell me mean?

superman + godzilla = true 

even if can, don't it. because others don't know unless need explain code them. maintenance nightmare!

what should instead, write method in hero class.

just reading code, think want hero kill monster when use + operator. , return whether if hero alive after fight. method job:

///<summary> ///blah blah blah ///</summary> ///<returns>whether hero alive</returns> public bool killmonster(monster m) {     if (!this.isrunningaway)     {         if(this.attackspeed > m.attackspeed)         {             m.takesdamage(this.attackvalue());             if (m.isalive())             {                 this.takesdamage(m.attackvalue);             }         }         else if(this.attackspeed < m.attackspeed)         {             this.takesdamage(m.attackvalue);             if (this.isalive())             {                 m.takesdamage(this.attackvalue());             }         }         else         {             this.takesdamage(m.attackvalue);             m.takesdamage(this.attackvalue());         }     }     else     {         if(this.attackspeed <= m.attackspeed)         {             this.takesdamage(m.attackvalue);         }                   }     this.isrunningaway = false;     return this.isalive(); } 

if want go 1 step further, don't need return value! because client code can call isalive() check whether alive, right? if isalive() private, make public.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -