[英]Replace TextBox with int
我有以下內容,並且有效:
我的球員班:
public Player(string Str, string SP)
{
Strength = Str;
StatPoints = SP;
}
public string StatPoints
{
get;
set;
}
public string Strength
{
get;
set;
}
現在在我的form1上,我有一個文本框和一個按鈕。 只要SP文本框中的值大於0,該按鈕就會將文本框中的值增加1。 問題是,我要聲明六個變量以管理兩個值,因為我必須將字符串轉換為ints以及所有這些廢話。 有沒有辦法用固有的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());
*/
}
還是有一種更簡單的方法來完成此操作,而我卻完全忽略了它。 經過大量的反復試驗后,我終於很高興能最終使這一點起作用,但我願意重做。 但是,我寧願只替換文本框,這樣就不會在各處轉換變量。
這是快速的方法,而不是在裝有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.TryParse
int.Parse
替換為int.TryParse
,后者在轉換前檢查是否可以進行轉換。 只是一些想法,助您一臂之力!
-更新-
您可以做的另一件事是在Player
類中增加邏輯。 這是更好,因為它含有保持在一個地方的邏輯,所以你可以看到一個Player
可以做 ,而不是搜索整個程序:
新玩家課程:
// 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!");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.