在按钮单击事件中从另一个类Person调用方法PresentPerson()

时间:2018-12-08 16:55:13

标签: c# visual-studio

所以我有一个简单的Web应用程序,允许用户输入姓名,年龄,日期,性别和电话号码

我正在使用构造函数来建立一个叫做p

的人

然后我尝试使用PresentPerson()方法演示此人,并将返回字符串分配给标签

你能帮我吗?我在PresentPerson中遇到错误,无法及时解决,咖啡似乎无法正常工作

application web form

my error

我的网页代码:

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Person p = new Person(Convert.ToInt32(DropDownList1.SelectedValue), TextBox1.Text, Calendar1.SelectedDate, Convert.ToInt32(TextBox2.Text), DropDownList2.Text);
        string s = PresentPerson();

        Label2.Text = "" + s;

    }

}

我的Person.cs代码:

public class Person
{
    int age;
    string name;
    int telNo;
    string gender;
    DateTime dateOfBirth;

    public int Age { get => age; set => age = value; }
    public string Name { get => name; set => name = value; }
    public int TelNo { get => telNo; set => telNo = value; }
    public string Gender { get => gender; set => gender = value; }
    public DateTime DateOfBirth { get => dateOfBirth; set => dateOfBirth = value; }

    public Person(int age, string name, DateTime dateOfBirth, int telNo, string gender)
    {
        this.age = age;
        this.name = name;
        this.DateOfBirth = dateOfBirth;
        this.telNo = telNo;
        this.gender = gender;
    }

    public string PresentPerson()
    {
        //PresentPerson();
        string s = "";
        s = name + ", age: " + age + ", telephone number: " + telNo + ", gender: " + gender + ", date of birth: " + DateOfBirth;

        return s;
    }
}

1 个答案:

答案 0 :(得分:2)

您需要在对象p上调用PresentPerson()。 因此p.PresentPerson()将返回信息。

cancel