[英]Split First Name and Last name from Combobox
我有以下Edit_Click
函數,它在string lname = Strings.Trim...
行上出錯。 它說名稱Strings
在當前上下文中不存在。 我想念什么?
private void Edit_Click(object sender, EventArgs e)
{
string[] fullname = ComboBox1.Text.Split(',');
string lname = Strings.Trim(fullname[0]);
string fname = Strings.Trim(fullname[1]);
for (int i = 0; i <= CustomerList.count - 1; i++)
{
Customers c = CustomerList(i);
if (c.last == lname & c.first == fname)
{
TextBox1.Text = lname;
TextBox2.Text = fname;
CurrentIndx = i;
CurrentCustomer = c;
break;
}
}
}
正如Loocid和paxdiablo在評論中指出的那樣,如果該方法是靜態的,則它將位於String
,而不是Strings
。
由於Trim()
是實例方法,因此需要在string
對象上調用它:
string lname = fullname[0].Trim();
string fname = fullname[1].Trim();
您的下一個問題是此行(盡管這可能是您寫問題時的錯字):
Customers c = CustomerList(i);
它應如下所示:
Customers c = CustomerList[i];
作為附帶說明,您可能還需要檢查fullname
的Length
屬性。 如果用戶輸入“ Test”,那么您將只有1個條目,因此您的代碼將因IndexOutOfRangeException
而失敗。
錯誤Strings
“當前上下文中不存在”表示您嘗試使用的對象不存在。 您可以在fname
和lname
上使用Trim,因為這是擴展方法。
string lname = fullname[0].Trim();
string fname = fullname[1].Trim();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.