爲什麼在下面更改卡片?讓我感到困惑..明白通過裁判哪些工作正常..但傳遞陣列是不符合我的期望。下.NET3.5SP1C#調用方法和變量範圍
非常感謝
void btnCalculate_Click(object sender, EventArgs e)
{
string[] cards = new string[3];
cards[0] = "old0";
cards[1] = "old1";
cards[2] = "old2";
int betResult = 5;
int position = 5;
clsRules myRules = new clsRules();
myRules.DealHand(cards, betResult, ref position); // why is this changing cards!
for (int i = 0; i < 3; i++)
textBox1.Text += cards[i] + "\r\n"; // these are all new[i] .. not expected!
textBox1.Text += "betresult " + betResult.ToString() + "\r\n"; // this is 5 as expected
textBox1.Text += "position " + position.ToString() + "\r\n"; // this is 6 as expected
}
public class clsRules
{
public void DealHand(string[] cardsInternal, int betResultInternal, ref int position1Internal)
{
cardsInternal[0] = "new0";
cardsInternal[1] = "new1";
cardsInternal[2] = "new2";
betResultInternal = 6;
position1Internal = 6;
}
}
nzscott here aka Dave。謝謝大家。我發現http://rapidapplicationdevelopment.blogspot.com/2007/01/parameter-passing-in-c.html文章非常出色。 – 2009-12-14 10:54:55