• 14
• 12
• 9
• 10
• 9

# Variables + OOP/OOD

This topic is 2573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a question for you guys i have a bunch of variables, that i want to be able to be accessed by any class(in C#).These will be ones such as health, mana, gold, exp, attakpwr, defense. How would i go about making these "global variables" ?

##### Share on other sites
You can make them private and make public methods for accesing them
For example
 private: int health; public: int getHealth() { return health; } void setHealth(int h) { health = h; } 
it is always better to do this way, although I'm using C++, but i think C# has it's own methods to quickly create accesor functions

##### Share on other sites
Ohhh... .Epic fail... it looks I misunderstood you. Does these variables don't belong to any class?

##### Share on other sites

I have a question for you guys i have a bunch of variables, that i want to be able to be accessed by any class(in C#).These will be ones such as health, mana, gold, exp, attakpwr, defense. How would i go about making these "global variables" ?

2. Make them public. Then other classes can access that instance's variables. Again, generally frowned upon.
3. Make them static. Then you don't need to create an instance of the class for the variables to exist. Generally, a little frowned upon, and decidedly un-OOP (especially for this scenario).

##### Share on other sites

[quote name='mchristensen' timestamp='1299418106' post='4782397']
I have a question for you guys i have a bunch of variables, that i want to be able to be accessed by any class(in C#).These will be ones such as health, mana, gold, exp, attakpwr, defense. How would i go about making these "global variables" ?

2. Make them public. Then other classes can access that instance's variables. Again, generally frowned upon.
3. Make them static. Then you don't need to create an instance of the class for the variables to exist. Generally, a little frowned upon, and decidedly un-OOP (especially for this scenario).
[/quote]

could you give a small example? Im still sort of new to the whole proper way to use methods and such.

##### Share on other sites

[quote name='Telastyn' timestamp='1299421219' post='4782410']
[quote name='mchristensen' timestamp='1299418106' post='4782397']
I have a question for you guys i have a bunch of variables, that i want to be able to be accessed by any class(in C#).These will be ones such as health, mana, gold, exp, attakpwr, defense. How would i go about making these "global variables" ?

2. Make them public. Then other classes can access that instance's variables. Again, generally frowned upon.
3. Make them static. Then you don't need to create an instance of the class for the variables to exist. Generally, a little frowned upon, and decidedly un-OOP (especially for this scenario).
[/quote]

could you give a small example? Im still sort of new to the whole proper way to use methods and such.

[/quote]

OOP groups data and their related methods into encapsulating classes. For example, you create a class Player that has member variables health, exp, attakpwr, defense, and so on. You could then either
1)make the member variables public (i.e. declared as "public int health") but this is one of those "generally frowned upon" ideas in OOP as it breaks the encapsulation, or
2)make the data private but create public methods that modify it. As an example:

 //in class Player ... private int health, defense; ... //simple method to cause the player to take damage, mitigated by their current defense stat. public void takeHit(int dmg) { health -= (dmg - defense); if(health <= 0) die(); } 

##### Share on other sites

[quote name='mchristensen' timestamp='1299430962' post='4782463']
[quote name='Telastyn' timestamp='1299421219' post='4782410']
[quote name='mchristensen' timestamp='1299418106' post='4782397']
I have a question for you guys i have a bunch of variables, that i want to be able to be accessed by any class(in C#).These will be ones such as health, mana, gold, exp, attakpwr, defense. How would i go about making these "global variables" ?

2. Make them public. Then other classes can access that instance's variables. Again, generally frowned upon.
3. Make them static. Then you don't need to create an instance of the class for the variables to exist. Generally, a little frowned upon, and decidedly un-OOP (especially for this scenario).
[/quote]

could you give a small example? Im still sort of new to the whole proper way to use methods and such.

[/quote]

OOP groups data and their related methods into encapsulating classes. For example, you create a class Player that has member variables health, exp, attakpwr, defense, and so on. You could then either
1)make the member variables public (i.e. declared as "public int health") but this is one of those "generally frowned upon" ideas in OOP as it breaks the encapsulation, or
2)make the data private but create public methods that modify it. As an example:

 //in class Player ... private int health, defense; ... //simple method to cause the player to take damage, mitigated by their current defense stat. public void takeHit(int dmg) { health -= (dmg - defense); if(health <= 0) die(); } 
[/quote]

So if i understand correctly by making the variable private it keeps it central to the class it is contained in, but a public method within the same as the variable class can change the value, but not a method outside that class? That seems slightly counter productive because, wouldn't it be easier to access the variables data as needed by any class/method that needs it? or am i mis understanding something?

##### Share on other sites
What I was trying to say to you... And you didn't reply...

##### Share on other sites

So if i understand correctly by making the variable private it keeps it central to the class it is contained in, but a public method within the same as the variable class can change the value, but not a method outside that class? That seems slightly counter productive because, wouldn't it be easier to access the variables data as needed by any class/method that needs it? or am i mis understanding something?

The point is to control access to the data. It's not counter-productive to restrict access, especially when all you have to do is make a call to "get" or "set" a value; it's not much more to do than to directly assign a value or to pass one as a parameter. But you don't need to worry about accidentally changing the values when you don't mean to, it's easier to check a value at any given time, and so on. There's nothing inherently wrong with global variables, that can be manipulated by anything in the program at any time, but you have more options and better control by taking an object oriented approach rather than a procedural one.

It's really not harder to use get and set functions for a private variable compared with passing the value in question directly.

##### Share on other sites

So if i understand correctly by making the variable private it keeps it central to the class it is contained in, but a public method within the same as the variable class can change the value, but not a method outside that class? That seems slightly counter productive because, wouldn't it be easier to access the variables data as needed by any class/method that needs it? or am i mis understanding something?

You want to keep things private and locked away in most classes. A class is, ideally, a thing that has a single purpose exposed as a set of actions. You don't want to have "health + getters/setters", you want more specific terms to your object like "recieve_damage" and "is_dead". This does two major things:
First off, it makes sure you can impose limits on the variables. In the case of health, you'd want to know when/if it goes below 0, because that killed you. You'd also probably check that healing you didn't take you past full health. Sure, other items could manage those limits, but the first time another programmer comes along to edit health they have to know all the esoteric rules to editing health. That instantly means that you have bugs to work out of the code. Keeping it in a method function of the class owning the health means it is a lot harder to misuse the variable.
Secondly, it localizes the place to change code by abstracting the implementation away from other classes. It is easy now to implement "health = health - damage" everywhere, but what happens when you want to add armor to that? Elemental resistances? etc? When you have code all over the place touching the health variable, you don't know who is doing what with it. It makes for a lot of code changes for something that should be a one line change to "health = health - max( (damage - armor), 0 )". By making health private, you can just change the "damage()" function, and know that your new armor damage reduction works everywhere.