0

How can I pass self parameters to a C# methods?

Example: I have two instances of a class, each one with atk, def, hp etc... I wanna do the python equivalent monster 1.attack(monster2) but I don't know how to pass self.atk where self is monster 1. I can do it with Monster1.attack(Monster Monster1, Monster Monster2) but I think is not as useful as in Python. Any suggestion?

14th Oct 2018, 1:27 PM
Stefano Marzo
Stefano Marzo - avatar
4 odpowiedzi
+ 2
In C# and in most OOP languages you don't have to add a "self" parameter that is the object itself, they do it for you which is usually called "this" e.g. https://code.sololearn.com/ceZs0vcFUHsf/?ref=app
14th Oct 2018, 1:34 PM
TurtleShell
TurtleShell - avatar
0
yr saying Monster1.attack(Monster monster2) and then this.hp refers to monster 1 hps?
14th Oct 2018, 1:41 PM
Stefano Marzo
Stefano Marzo - avatar
0
Stefano Marzo you don't have to pass monster1 at all, C# does it for you in the form of "this"
14th Oct 2018, 1:54 PM
TurtleShell
TurtleShell - avatar
0
really good example. Thanks. I'll work on it and let you know, posting my code. Again really useful.
14th Oct 2018, 1:59 PM
Stefano Marzo
Stefano Marzo - avatar