转换值

时间:2015-06-03 22:03:24

标签: c# asp.net .net

我收到了错误

  

System.FormatException

在输入人员详细信息后点击Button1

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, Convert.ToInt32(TextBox2.Text), 
        TextBox3.Text, TextBox4.Text, Convert.ToBoolean(DropDownList1.SelectedValue), TextBox5.Text);    
        Label1.Text = (p.PresentPerson());
    }
}

人员类:

class Person
{
    public int Age { get { return Age; } set { Age = value; } }
    public string Name { get { return Name; } set { Name = value; } }
    public string DOB { get { return DOB; } set { DOB = value; } }
    public string TelNo { get { return TelNo; } set { TelNo = value; } }
    public bool Gender { get { return Gender; } set { Gender = value; } }
    public string Address { get { return Address; } set { Address = value; } }
    public string enterPerson;

    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 string PresentPerson()
    {
        return enterPerson = "Name: " + Name + "\n" + "Age: " + Age + "\n" +  "Date of Birth: "
        + DOB + "\n" + "Telephone Number: " + TelNo + "\n" + "Gender: " + "\n" + "Address: "
        + Address;
    }
}

2 个答案:

答案 0 :(得分:0)

简单的解决方案是检查性别的值,然后设置正确的bool值:

public partial class _Default : Page
{

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("You have successfully added a Person!");
        bool gender = (DropDownList1.SelectedValue == "Male") ? true:false;
        Person p = new Person(TextBox1.Text, Convert.ToInt32(TextBox2.Text), 
        TextBox3.Text, TextBox4.Text, gender, TextBox5.Text);

        Label1.Text = (p.PresentPerson());
    }
}

仅当值为男性时,才会将性别设置为true。

更高级的是使用枚举https://msdn.microsoft.com/en-us/library/sbbt4032.aspx

class Person
{
...
enum Genders {Male, Female, NotSet};
public Genders Gender { get { return Gender; } set { Gender = value; } }
public Person(string name, int age, string dob, string telNo, Person.Genders gender, string address){...}
...
}

也许像这样的开关声明,你可以轻松地处理其他过程:

public partial class _Default : Page
    {

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write("You have successfully added a Person!");
            Person.Genders gender = Person.Genders.NotSet;
            switch(DropDownList1.SelectedValue)
            {
               case "Male": gender = Person.Genders.Male; break;
               case "Female": gender = Person.Genders.Female; break;
            }
            Person p = new Person(TextBox1.Text, Convert.ToInt32(TextBox2.Text), 
            TextBox3.Text, TextBox4.Text, gender, TextBox5.Text);

            Label1.Text = (p.PresentPerson());
        }
    }

答案 1 :(得分:0)

为避免错误,请在文本框中使用验证器,例如regularexpression或custom。 例如:\d+代表您的年龄文本框。 这有助于您将错误降至最低。由于您对男性和女性的表示是布尔值,因此您可以执行以下操作:

bool gender = DropDownList1.SelectedValue == "Male"

并将其传递给构造函数。