CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: a better way?

  1. #1
    Join Date
    Jun 2008
    Posts
    154

    a better way?

    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.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: a better way?

    Well, why not putting your stuff like:

    newAgility1
    newAgility2
    newAgility3
    ...

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

  3. #3
    Join Date
    Jun 2008
    Posts
    154

    Re: a better way?

    time to learn arrays then!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured