Вопрос:

Accessing variable in class by value of string

c#

42 просмотра

2 ответа

8 Репутация автора

I am making a text based adventure and want to have a function in my player class that increases a variable in the class by accepting a string(for the variable name ) and int (for the amount)

class player
{
  private int stat1 = 1;
  private int stat2 = 2; 
  public void statincrease(string name, int amount)
  {
    //variable called by string// += amount  
  }
  private void main()
  {
   player.statincrease(stat2,5);
  }
}

sorry if i have not explained my self very well, but any help on this would much appreciated

Автор: Ojardan Источник Размещён: 08.04.2017 05:18

Ответы (2)


2 плюса

4626 Репутация автора

Решение

I can tell you're new at this so I'll be easy on you.

There are several ways to go about something like what you're asking.

My first suggestion is to make a method for each name you need, so instead of string name you have shieldStatIncrease(int amount); swordStatIncrease(int amount); etc.

If you don't want to do that then you can use if or switch statements

public void statincrease(string name, int amount)
{
    switch(name)
    {
         case "stat1":
            //Increase stat1
            break;
         case "stat2":
            //Increase stat2
            break; //etc...
    }
}

If this isn't what you're talking about then I apologize... Let me know and I'll adjust my answer.

Now if you're trying to get real technical you can use reflection to find the property name and adjust the variable for you... But I don't recommend that; especially not for a game because reflection has a little more overhead.

Автор: Michael Puckett II Размещён: 08.04.2017 05:25

1 плюс

22501 Репутация автора

This would be the recommended approach, which is not what you're asking for: create public properties that users can access and adjust. You can include some data validation in the property setter if you want to enforce that the value stays within some particular bounds:

class Player
{
    public int Stat1 { get; set; }
    public int Stat2 { get; set; }

    public Player()
    {
        Stat1 = 1;
        Stat2 = 2;
    }
}

Then the user of this class would do something like:

var player = new Player();
player.Stat2 += 5; // Increase Stat2 by 5

Alternatively, you could keep the members private and create a method that changes the values, though I think this is less common:

class Player
{
    private int stat1 = 1;
    private int stat2 = 2;

    public void AdjustStat2(int adjustmentIncrement)
    {
        stat2 += adjustmentIncrement;
    }
}

Finally, to answer your original question, you can have the user type in a string and do matching on the variable name, but that is a painful process for users of your class because they can't just type Player. and get an intellisense list of the properties they have access to:

class Player
{
    private int stat1 = 1;
    private int stat2 = 2;

    public void AdjustProperty(string propName, int adjustmentIncrement)
    {
        if (propName.Equals("stat1", StringComparison.OrdinalIgnoreCase))
        {
            stat1 += adjustmentIncrement;
        }
        else if (propName.Equals("stat2", StringComparison.OrdinalIgnoreCase))
        {
            stat2 += adjustmentIncrement;
        }            
    }
}
Автор: Rufus L Размещён: 08.04.2017 05:38
32x32