0

私は以下を持っています、そしてそれは動作します:

私のプレーヤークラス:

    public Player(string Str, string SP)
    {
        Strength = Str;
        StatPoints = SP;
    }
    public string StatPoints
    {
        get;
        set;
    }
    public string Strength
    {
        get;
        set;
    }

私のフォーム1には、テキストボックスとボタンがあります。ボタンは、SP テキストボックスに 0 より大きい値がある限り、テキストボックスの値を 1 ずつ増やします。問題は、文字列を int に変換する必要があるため、2 つの値を管理するために 6 つの変数を宣言していることです。テキスト ボックスを本質的に int のものに置き換える方法はありますか? これまでの私のキャラクターシートコードは次のとおりです。

    private void AddButton_Click(object sender, EventArgs e)
    {

        Player PCStats = new Player(StrBox.Text, SPBox.Text);
        int IntPCSP = Convert.ToInt16(PCStats.StatPoints);
        int IntPCStr = Convert.ToInt16(PCStats.Strength);

        if (IntPCSP >= 1 && IntPCStr <= 7)
        {
            IntPCStr++;
            IntPCSP--;
            PCStats.Strength = IntPCStr.ToString();
            PCStats.StatPoints = IntPCSP.ToString();
            StrBox.Text = PCStats.Strength;
            SPBox.Text = PCStats.StatPoints;
        }
        else
        {
            MessageBox.Show("Earn more experience!");
        }
        /*
        MessageBox.Show("PCStats.StatPoints equals" + PCStats.StatPoints);
        MessageBox.Show("PCStats,Strength equals" + PCStats.Strength);
        MessageBox.Show("IntPCSP Equals" + IntPCSP.ToString());
        MessageBox.Show("IntPCStr Equals" + IntPCStr.ToString());
        */
    }

または、私が完全に見落としていたこれを行うためのさらに簡単な方法があります。多くの試行錯誤の末、ようやくこのビットが機能するようになったことに非常に興奮しましたが、やり直すことはできます。ただし、テキストボックスを置き換えるだけなので、変数をあちこちに変換することはありません。

4

1 に答える 1

0

これは、Visual Studio がインストールされたコンピューターではありませんが、すぐに始められるはずです。また、変数などにもう少し意味のある名前を付けてみてください。また、これはあなたが持っているものをそのまま修正することですがPlayerロジックをクラスに移動することについての私の更新/提案をさらに見てください...

私のプレーヤークラス:

public Player(int strength, int statPoints)
{
    this.Strength = strength;
    this.StatPoints = statPoints;
}

public int StatPoints { get; set; }

public int Strength { get; set; }

私のフォーム:

private void AddButton_Click(object sender, EventArgs e)
{
    Player thePlayer = new Player(int.Parse(StrBox.Text), int.Parse(SPBox.Text));

    if (thePlayer.StatPoints > 0 && thePlayer.Strength < 8)
    {
        thePlayer.Strength++;
        thePlayer.StatPoints--;
        StrBox.Text = thePlayer.Strength.ToString();
        SPBox.Text = thePlayer.StatPoints.ToString();
    }
    else
    {
        MessageBox.Show("Earn more experience!");
    }
}

明らかに、テキスト ボックスの値が整数であることを確認する必要があります。別のコントロールを使用したり、テキストボックスなどをマスクしたり、変換前に可能なチェックでコードを置き換えint.Parseたりすることができます。int.TryParseあなたを動かすためのほんのいくつかのアイデア!

- アップデート -

もう 1 つできることは、Playerクラスにロジックを追加することです。これは、プログラム全体を検索するのではなく、何がPlayerできるを確認できるようにロジックを 1 か所に保持するため、優れています。

新しいプレーヤー クラス:

// The Player class
public class Player
{
    // Constructor
    public Player(int strength, int statPoints)
    {
        this.Strength = strength;
        this.StatPoints = statPoints;
    }

    // Method to gain strength if enough StatPoints
    public bool GainStrength()
    {        
        bool playerHasEnoughStatPoints = true;

        if (this.StatPoints < 1)
        {
            playerHasEnoughStatPoints = false;
        }
        else if (this.Strength < 8)
        {
            this.Strength++;
            this.StatPoints--;
        }

        return playerHasEnoughStatPoints;
    }

    // Property for StatPoints
    public int StatPoints { get; set; }

    // Property for Strength
    public int Strength { get; set; }
}

新しいフォーム:

// The Form or similar
public class MyFormOrSimilar
{   
    // When button pressed try and add strength to the player
    protected void AddButton_Click(object sender, EventArgs e)
    {
        // Create new INSTANCE of the player and try to give them strength
        Player thePlayer = new Player(int.Parse(StrBox.Text), int.Parse(SPBox.Text));
        if (thePlayer.GainStrength())
        {
            StrBox.Text = thePlayer.Strength.ToString();
            SPBox.Text = thePlayer.StatPoints.ToString();
        }
        else
        {
            MessageBox.Show("Earn more experience!");
        }
    }
}
于 2013-04-17T09:23:42.983 に答える