Can't pass objects as parameters in method? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

Can't pass objects as parameters in method?

Here's the code: public class Fighter { public string Name; public int Health, DamagePerAttack; public Fighter(string name, int health, int damagePerAttack) { this.Name = name; this.Health = health; this.DamagePerAttack = damagePerAttack; } class Fight { public Fight(Fighter fighter1, Fighter fighter2) { while (fighter1.Health != 0 || fighter2.Health != 0) { fighter1.Health -= fighter2.DamagePerAttack; fighter2.Health -= fighter1.DamagePerAttack; if (fighter1.Health == 0) { Console.WriteLine(fighter2 + " is the winner!"); } else if (fighter2.Health == 0) { Console.WriteLine(fighter1 + " is the winner"); } else { Console.WriteLine("It's a draw!"); } } } } Fighter fighter01 = new Fighter("Robert", 100, 20); Fighter fighter02 = new Fighter("James", 200, 10); Fight FinalFight = new Fight(fighter01, fighter02); } } Error: A field initializer cannot reference the non-static field, method, or property 'Program.Fighter.fighter01'

3rd Jun 2017, 7:45 PM
Unknown Unknown
Unknown Unknown - avatar
13 Respostas
+ 6
That's pretty good for that little time, great choice picking up C#.
3rd Jun 2017, 8:30 PM
Dao
Dao - avatar
+ 5
I don't really know, your code sample is very messy, but you didn't override the ToString method in the Fighter class, so you might wanna use "fighterX.Name" rather than just "fighterX" in the Fight class.
3rd Jun 2017, 7:59 PM
Dao
Dao - avatar
+ 5
Also, in the while loop, you should change to && instead of ||, and != to > or it will continue until both have exactly 0 health (and they might even keep fighting with negative health).
3rd Jun 2017, 8:05 PM
Dao
Dao - avatar
+ 5
@sneeze props to you for noticing that despite the mess.
3rd Jun 2017, 8:07 PM
Dao
Dao - avatar
+ 2
Are you missing a } to close the fighter class. Are the last 3 sentences part of the fighter class or should they be in some sort of button_click of main method.
3rd Jun 2017, 7:57 PM
sneeze
sneeze - avatar
+ 2
disregard my last post, hah read something wrong. When you create the objects is that supposed to be in the main method?
3rd Jun 2017, 8:09 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
I was on a website that does coding challenges and when I copy and pasted my code to Visual Studio it didn't have the Main method. I was a little confused and then I thought you didn't need it, but I guess you do. Alright thanks again for the help. I added the Main method and the code works now.
3rd Jun 2017, 8:07 PM
Unknown Unknown
Unknown Unknown - avatar
+ 1
Looks good. You are practising and asking question, that good. Still have the idea that the positioning of your {} (brackets ?) is not ideal. The Fight class and main are part of the fighter class, is that correct ? I still think. this.DamagePerAttack = damagePerAttack; } class Fight A "}" should be placed between these two lines And you should remove a "}" at the end
3rd Jun 2017, 9:26 PM
sneeze
sneeze - avatar
0
I took your guy's advice and changed a bit of my code. Here it is now: public class Fighter { public string Name; public int Health, DamagePerAttack; Fighter(string name, int health, int damagePerAttack) { this.Name = name; this.Health = health; this.DamagePerAttack = damagePerAttack; } class Fight { public Fight(Fighter fighter1, Fighter fighter2) { while (fighter1.Health > 0 && fighter2.Health > 0) { fighter1.Health -= fighter2.DamagePerAttack; fighter2.Health -= fighter1.DamagePerAttack; if (fighter1.Health <= 0) { Console.WriteLine(fighter2.Name + " is the winner!"); } else if (fighter2.Health <= 0) { Console.WriteLine(fighter1.Name + " is the winner"); } else if(fighter1.Health <= 0 && fighter2.Health <= 0 ) { Console.WriteLine("It's a draw!"); } } } } static void Main(string[] args) { Fighter fighter01 = new Fighter("Robert", 100, 20); Fighter fighter02 = new Fighter("James", 200, 10); Fight FinalFight = new Fight(fighter01, fighter02); Console.ReadKey(); } } Besides it being messy(I just started programming about a week ago), how does it look?
3rd Jun 2017, 8:22 PM
Unknown Unknown
Unknown Unknown - avatar
0
Thanks man.
3rd Jun 2017, 8:32 PM
Unknown Unknown
Unknown Unknown - avatar
0
I was doing this as a challenge on another website and when I copy and pasted it, it messed up a bit. I will look at the brackets again. Thanks for the advice.
4th Jun 2017, 2:31 AM
Unknown Unknown
Unknown Unknown - avatar
0
please do start in thinking in start and end. It is not a trick you are learning it is a skill. class ClassA () { // begin of class //the code }// end of class every begin { should have a} end
4th Jun 2017, 8:21 AM
sneeze
sneeze - avatar
0
Mine does have brackets though.
4th Jun 2017, 4:20 PM
Unknown Unknown
Unknown Unknown - avatar