[英]calling on a method PresentPerson() from another class Person in a button click event
so i have a simple web application which allows a user to input name, age, date, gender and telephone number 所以我有一个简单的Web应用程序,允许用户输入姓名,年龄,日期,性别和电话号码
i am using a constructer to build a person called p 我正在使用构造函数来建立一个叫做p的人
i am then trying to present the person using PresentPerson() method and assign the return string to a label 然后,我尝试使用PresentPerson()方法演示此人,并将返回字符串分配给标签
can you help me? 你能帮助我吗? i have an error with PresentPerson and im having trouble figuring it out, the coffee doesn't seem to be working
我在PresentPerson中遇到错误,无法及时解决,咖啡似乎不起作用
My web page code: 我的网页代码:
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;
}
}
My Person.cs code: 我的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;
}
}
You need to call PresentPerson() on the object p. 您需要在对象p上调用PresentPerson()。 So p.PresentPerson() will return the information.
所以p.PresentPerson()将返回信息。
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);
Label2.Text = p.PresentPerson();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.