简体   繁体   English

通过函数传递参数

[英]passing parameters via function

I'm making a C# airlines reservation project, taking the user details via form1, say I have a class PassengerDetails with class variables. 我正在做一个C#航空公司预订项目,通过form1获取用户详细信息,比如说我有一个带有类变量的PassengerDetails类。 Now, on click of the button, I need to assign all those TextBox values to the class variables 现在,单击按钮,我需要将所有这些TextBox值分配给类变量

private void btnSubmit_Click(object sender, EventArgs e)
{
    string fn = txtFname.Text;
    string ln = txtLname.Text;
    string add = txtAddress.Text;
    int age =   int.Parse(txtAge.Text);
    submit(fn, ln, add, age);
}

I need to pass these to the function. 我需要将这些传递给函数。 How should I declare it? 我应该如何申报?

Create a class for passenger data and pass this as a parameter. 为乘客数据创建一个类,并将其作为参数传递。

class Passenger
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public int Age { get; set; }
}

private void AddButton_Click(object sender, EventArgs e)
{
    var passenger = new Passenger
                        {
                            FirstName = txtFname.Text,
                            LastName = txtLname.Text,
                            Address = txtAdd.Text,
                            Age = int.Parse(txtAge.Text)
                        };
    AddPassenger(passenger);
}

Can't you just make a simple assignment? 您不能只做一个简单的作业吗?

public class MyClass
{
  public string fn;
  public string ln;
 public string add;
  public int age;
}

the submit function is 提交功能是

submit(string fn, string ln, string add, int age)
{
  MyClass myclass = new MyClass();
  myclass.fn = fn;
  myclass.ln = ln;
  myclass.add=add;
  mycalss.age = age;
}

You would have a form-level variable that stores an instance of your passenger details class can call that: 您将有一个表单级变量来存储您的乘客详细信息类的实例,可以调用该变量:

Note: pseudo code! 注意:伪代码! Do not copy and paste! 不要复制和粘贴!

class form1()
{
    private passenger_details = new PassengerDetail()

    private void button_click()
    {
        passenger_details.age = int(nameField.Text);
    }
}

You could also overload your constructor: 您还可以重载构造函数:

class Passenger
{
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public string Address { get; set; }
     public int Age { get; set; }

     void Passenger() {}

     void Passenger(string _firstName, string _lastName, string _address, int _age) 
     {
          this.FirstName = _firstName;
          this.LastName = _lastName;
          this.Address = _address;
          this.Age = _age; 
     }
}

Then your event would just assign them at creation. 然后,您的事件将在创建时分配给他们。 (following code assumes you have a global variable called MyPassenger) (以下代码假定您有一个名为MyPassenger的全局变量)

private void btnSubmit_Click(object sender, EventArgs e)  
{  
    this.MyPassenger = new Passenger(txtFname.Text, txtLname.Text, txtAddress.Text, int.Parse(txtAge.Text));     

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM