变量作为函数参数?

时间:2016-07-15 13:42:39

标签: c# variables parameters

我正在做一些小RPG(角色扮演游戏),在对话中有决定影响玩家的某些变量。我想知道你是否知道比我更有效的方式:

public string Name, Nickname;
public int Age;

public void Decision(string Var, string Input)
{
    if (Var == "Name")
        Name = Input;
    else if (Var == "Nickname")
        Nickname = Input;
    else if (Var == "Age")
        Age = Convert.ToInt32(Input);
}

正如你所看到的,只有3个变量很长,但还有更多...有没有办法更快地完成它?像这样:

public void Decision(Variable Var, string Input)
{
    Player.Var = input;
}

编辑:我将使用两个答案的混合:

    public string Nom, Prenom;
    public int Age;

    public void Decision(InfoType type, string Input)
    {
        switch (type)
        {
            case InfoType.Name:
                Nom = Input;
                break;
            case InfoType.Prenom:
                Prenom = Input;
                break;
        }
    }
    public void Decision(InfoType type, int Input)
    {
        switch (type)
        {
            case InfoType.Age:
                Age = Convert.ToInt32(Input);
                break;
        }

    }

3 个答案:

答案 0 :(得分:0)

您可以使用switch语句使事情更简洁,并使用枚举来实现类型安全。 e.g。

public enum InfoType {
    Name,
    Nickname,
    Age
}

public void Decision(InfoType type, string input)
{
    switch (type)
    {
        case InfoType.Name:
            Name = input;
            break;
        case InfoType.NickName:
            NickName = input;
            break;
    }
}

等等。

答案 1 :(得分:0)

方法重载怎么样?

SQL> with tbl(str) as (
     select 'ABCDEF:PmId12345RmLn1VlId0,ABCDEF:PmId12345RmLn1VlId0,ABCDEF:PmId12345RmLn1VlId0,ABCDEF:PmId12345RmLn1VlId0,ABCDEF:PmId12345RmLn1VlId0,ABCDEF:PmI
d12345RmLn1VlId0,ABCDEF:PmId12345RmLn1VlId0,ABCDEF:PmId12345RmLn1VlId0,ABCDEF:PmId12345RmLn1VlId0,ABCDEF:PmId12345RmLn1VlId0,ABCDEF:PmId12345RmLn1VlId0,ABCDEF:P
mId12345RmLn1VlId0,ABCDEF:PmId12345RmLn1VlId0,ABCDEF:PmId12345RmLn1VlId0,ABCDEF:PmId12345RmLn1VlId0,ABCDEF:PmId12345RmLn1VlId0'
       from dual
   )
   select regexp_substr(str, '(.*?)(,|$)', 1, level, NULL, 1) column_name
   from tbl
   connect by regexp_substr(str, '(.*?)(,|$)', 1, level) is not null;

COLUMN_NAME
--------------------------------------------------------------------------------
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0
ABCDEF:PmId12345RmLn1VlId0

16 rows selected.

SQL>

答案 2 :(得分:0)

你可以使用反射。这样做的缺点是您必须对复杂类型进行额外检查,但对于简单对象,它可以像这样工作。

public class MyObject {
    public string Name { get; set; }
    public string Nickname { get; set; }
    public int Age { get; set; }
}

public class TestReflection {
    public void Test() {
        var obj = new MyObject();

        UpdateObject(obj, "Name", "THis is a name");
        UpdateObject(obj, "Age", 99);
        UpdateObject(obj, "Nickname", "This is a nickname");

        Console.WriteLine("{0} {1} {2}", obj.Name, obj.Age, obj.Nickname);
        Console.ReadLine();
    }

    private void UpdateObject(MyObject obj, string varName, object varValue) {
        //Get Properties of the object
        var properties = obj.GetType().GetProperties();
        //Search for the property via the name passed over to the method.
        var prop = properties.FirstOrDefault(_ => _.Name.Equals(varName, StringComparison.InvariantCultureIgnoreCase));
        //Check prop exists and set to the object.
        if(prop != null) {
            prop.SetValue(obj, varValue);
        }
    }
}