[英]C# Overloading Invalid Arguments
我對C#還是很陌生,所以如果我的編碼有點晦澀,對不起! 我的People應用程序不斷出現3個錯誤。
這些錯誤是由於嘗試將int和bool轉換為字符串引起的,我只是不確定我將如何更改它,如果你們能幫幫我,那將非常感謝! 任何提示也將不勝感激! 不斷發生的錯誤是:
錯誤1:“ Lab5b.Person.Person(string,int,string,string,bool,string)”的最佳重載方法匹配具有一些無效的參數
錯誤2:參數2:無法從“字符串”轉換為“ int”
錯誤3:參數5:無法從“方法組”轉換為“布爾”
我的代碼:
public partial class _Default : Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("You have successfully added a Person!");
Person p = new Person(TextBox1.Text, TextBox2.ToString(), TextBox3.Text, TextBox4.Text, DropDownList1.ToString, TextBox5.Text);
}
}
和我的Person類:
class Person
{
private string name;
private int age;
private string dob;
private string telNo;
private bool gender;
private string address;
public Person(string name, int age, string dob, string telNo, bool gender, string address)
{
Name = name;
Age = age;
DOB = dob;
TelNo = telNo;
Gender = gender;
Address = address;
}
public int Age { get; set; }
public string Name { get; set; }
public string DOB {get; set;}
public string TelNo {get; set;}
public bool Gender {get; set;}
public string Address {get; set;}
public string PresentPerson()
{
}
}
您有很多問題。
()
,所以將DropDownList1值作為方法組傳遞 .ToString()
,則可能會將字符串值傳遞給期望布爾值的參數。 TextBox2
而不是文本框的值將給您帶來意外的錯誤結果。 。
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("You have successfully added a Person!");
Person p = new Person(TextBox1.Text, int.Parse(TextBox2.Text.ToString()),
TextBox3.Text, TextBox4.Text, bool.Parse(DropDownList1.SelectedValue.ToString()),
TextBox5.Text);
}
對於Gender,代碼假設您的下拉列表值為True或False,以便可以正確分析它們,而沒有空條目或其他值(例如Male / Female)的條目。
另外,值得注意的是,如果這些解析不能完全正確地進行,則將引發異常。 最好使用TryParse
並驗證您的數據是否符合您的想法。
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("You have successfully added a Person!");
int age;
bool gender;
int.TryParse(TextBox2.Text.ToString(), out age);
bool.TryParse(DropDownList1.SelectedValue, out gender);
Person p = new Person(TextBox1.Text, age, TextBox3.Text, TextBox4.Text, gender,
TextBox5.Text);
}
您在此行中調用構造函數人:
Person p = new Person(TextBox1.Text, TextBox2.ToString(), TextBox3.Text, TextBox4.Text, DropDownList1.ToString, TextBox5.Text);
請注意,TextBox.Text是字符串類型。
您的人構造函數具有以下特征:
public Person(string name, int age, string dob, string telNo, bool gender, string address)
這意味着第二個和第五個參數分別是int和bool。 如果用戶在文本框中正確輸入了數據,並且下拉列表具有正確的值,則可以使用Convert類獲取正確的格式,如下所示:
Person p = new Person(TextBox1.Text, Convert.ToInt32(TextBox2.Text), TextBox3.Text, TextBox4.Text, Convert.ToBoolean(DropDownList1.SelectedValue), TextBox5.Text);
調用TextBox2.ToString()試圖將對象轉換為字符串,而不一定是文本框中的文本。 對於下拉列表類似,根據設置方式,應在其中使用SelectedText或SelectedValue。
同樣,在“人”的類定義中,您也定義了兩次字段。
private string name;
private int age;
private string dob;
private string telNo;
private bool gender;
private string address;
和
public int Age { get; set; }
public string Name { get; set; }
public string DOB {get; set;}
public string TelNo {get; set;}
public bool Gender {get; set;}
public string Address {get; set;}
確保只定義一次字段。 如果您嘗試擁有私有字段和公共屬性,通常的做法是將camelCase作為私有字段名稱,將PascalCase作為屬性。 還應該定義屬性以訪問私有字段:
private string name;
public string Name { get { return name; } set { name = value; } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.