Hi, I'm working on creating my first game to sharpen my programing skills and as a great learning experience for programming in general.
I'm using private ints for the stats and was working on get and set methods for retrieving them and changing them and I was wondering if anyone could tell me how to go about this without having to create a if statement for each variable in the class (since there are many)

Condensed version of code for example:
class Character
{
private int str;
private int HP;
private int spd;

public int getStat(String stat)
{
//here is where I want to return the value of the
//variable above with the matching name to stat
}

public int setStat(String stat, int x)
{
//here is where I want to change the variable above
//with the matching name of stat to x
}
}

How would I go about it?

Recommended Answers

All 3 Replies

Normally you would 3 setters and 3 getters. Is there any special reason why you want to do it that way?

yeah, that was a small example, I actually have around 70 stats right now, with plans for more.

OK, think about a Hashtable - use the names as keys and the stats as values. There's lots of examples on the web.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.