Click to See Complete Forum and Search --> : a better way?


bixel
March 3rd, 2009, 11:16 PM
I got a host of semi-complex functions that you feed in some data and then spit it out. Problem is I need to make sure the data is correctly saved separately if a different object is using the function

so for instance
public void ChangeLevel(int newlevel, int levelid, int strength, int vitality, int agility, decimal classtype)

we are going to give the function some data to work correctly

newlevel = whatever the new level is 1,2,3,4etc...
levelid = which object are we leveling?? object 1 object 2, etc...
that particular objects stats, object 1 strength, object 1 vitality, etc
and finally that objects class (fighter, mage)

lately I have been doing this the obvious and nasty way

I check which object it is

if (levelid == 1)

if so, then I use these sets of variables
newStrength1 = strength;
newVitality1 = vitality;
newAgility1 = agility;

as you can quickly see, if I have 5+ more objects, the code can get pretty long and nasty

if (levelid == 2)
newStrength2 = strength;
newVitality2 = vitality;
newAgility2 = agility;
if (levelid == 3)
newStrength3 = strength;
newVitality3 = vitality;
newAgility3 = agility;

I was wondering perhaps to shorten the code and make useful for any number of objects. I was thinking of a way to give ownership to the variables depending on which object is being used.

one of the ways I was thinking of was to append the universal variable's (name) with the object id.
string owner = Convert.ToString(levelid);

newStrength+owner; // the name has been appended
newStrength+owner = strength; //the value has changed

but this doesnt work cause newStrength is an int, I dont want to change its value, I just want the reference to belong to the object (levelid). Bah.

MNovy
March 3rd, 2009, 11:33 PM
Well, why not putting your stuff like:

newAgility1
newAgility2
newAgility3
...

into an array? This is the way I would go.

bixel
March 3rd, 2009, 11:42 PM
time to learn arrays then!