C#应用程序将对象从表单传递到另一个表单

时间:2015-12-06 19:40:33

标签: c#

在我的应用程序中主要加载表单1,在表单1中创建一个对象。表单1上有一个下一个按钮,当单击下一个按钮时,隐藏表单1并加载表单2。我试图将表单1中创建的对象传递给表单2,但是对象中的数据没有被拉到表单2,我做错了什么?

Class1有

for()

Form1有

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab15
{
public class Reservation
{
    //set private instance variables
    private int checkInDateDay;
    private int checkInDateMonth;
    private int checkInDateYear;
    private int numberOfNights;
    private int numberofGuests;
    private string name;
    private string addressStreet;
    private string addressCity;
    private string addressState;
    private int addressZipCode;
    private string creditCardNumber;
    private decimal dailyRate;
    private decimal subTotal;
    private decimal tax;
    private decimal total;
    private string phone;

    // CheckInDateDay property
    public int CheckInDateDay
    {
        get { return checkInDateDay; }
        set { checkInDateDay = value; }
    } // end CheckInDateDay property

    // CheckInDateMonth property
    public int CheckInDateMonth
    {
        get { return checkInDateMonth; }
        set { checkInDateMonth = value; }
    } // end CheckInDateMonth property

    // CheckInDateYear property
    public int CheckInDateYear
    {
        get { return checkInDateYear; }
        set { checkInDateYear = value; }
    } // end CheckInDateYear property

    // NumberOfNights property
    public int NumberOfNights
    {
        get { return numberOfNights; }
        set { numberOfNights = value; }
    } // end NumberOfNights property

    // NumberofGuests property
    public int NumberofGuests
    {
        get { return numberofGuests; }
        set { numberofGuests = value; }
    } // end NumberofGuests property

    // Name property
    public string Name
    {
        get { return name; }
        set { name = value; }
    } // end Name property

    // AddressStreet property
    public string AddressStreet
    {
        get { return addressStreet; }
        set { addressStreet = value; }
    } // end AddressStreet property

    // AddressCity property
    public string AddressCity
    {
        get { return addressCity; }
        set { addressCity = value; }
    } // end AddressCity property

    // AddressState property
    public string AddressState
    {
        get { return addressState; }
        set { addressState = value; }
    } // end AddressState property

    // AddressZipCode property
    public int AddressZipCode
    {
        get { return addressZipCode; }
        set { addressZipCode = value; }
    } // end AddressZipCode property

    // CreditCardNumber property
    public string CreditCardNumber
    {
        get { return creditCardNumber; }
        set { creditCardNumber = value; }
    } // end CreditCardNumber property

    // DailyRate property
    public decimal DailyRate
    {
        get { return dailyRate; }
        set { dailyRate = value; }
    } // end DailyRate property

    // SubTotal property
    public decimal SubTotal
    {
        get { return subTotal; }
        set { subTotal = value; }
    } // end SubTotal property

    // Tax property
    public decimal Tax
    {
        get { return tax; }
        set { tax = value; }
    } // end Tax property

    // Total property
    public decimal Total
    {
        get { return total; }
        set { total = value; }
    } // end Total property

    // Phone property
    public string Phone
    {
        get { return phone; }
        set { phone = value; }
    } // end Phone property

    // Reservation Constructor
    public Reservation(int checkInDateday, int checkInDateMonth, int checkInDateYear, 
                       int numberOfNights, int numberofGuests, string name, 
                       string addressStreet, string addressCity, string addressState, 
                       int addressZipCode, string creditCardNumber, decimal dailyRate, 
                       decimal subTotal, decimal tax, decimal total, string phone)
    {
        CheckInDateDay = checkInDateDay;
        CheckInDateMonth = checkInDateMonth;
        CheckInDateYear = checkInDateYear;
        NumberOfNights = numberOfNights;
        NumberofGuests = numberofGuests;
        Name = name;
        AddressStreet = addressStreet;
        AddressCity =addressCity;
        AddressState = addressState;
        AddressZipCode = addressZipCode;
        CreditCardNumber = creditCardNumber;
        DailyRate = dailyRate;
        SubTotal = subTotal;
        Tax = tax;
        Total = total;
        Phone = phone;
    } // end Reservation Constructor

}
}

Form2有

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lab15
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void nextButton_Click(object sender, EventArgs e)
    {
        int checkInDateDay = checkInDateDateTimePicker.Value.Day;
        int checkInDateMonth = checkInDateDateTimePicker.Value.Month;
        int checkInDateYear = checkInDateDateTimePicker.Value.Year;
        int numberOfNights = Convert.ToInt32(numberOfNightsNumericUpDown.Value);
        int numberofGuests = Convert.ToInt32(numberOfGuestsNumericUpDown.Value);
        string name = "";
        string addressStreet = "";
        string addressCity = "";
        string addressState = "";
        int addressZipCode = 0;
        string creditCardNumber = "";
        decimal dailyRate = 0.00m;
        decimal subTotal = 0.00m;
        decimal tax = 0.00m;
        decimal total = 0.00m;
        string phone = "";

        Reservation newRes = new Reservation(checkInDateDay, checkInDateMonth, checkInDateYear, numberOfNights,
                       numberofGuests, name, addressStreet, addressCity, addressState,
                       addressZipCode, creditCardNumber, dailyRate, subTotal, tax,
                       total, phone);

        this.Hide();
        Form2 form2 = new Form2(newRes);
        form2.Show();
    }


}
}

在表单2中,我使用下一个按钮来填充标签,而不是仅仅为了检查值而应该做什么。

1 个答案:

答案 0 :(得分:0)

查看Reservation类的构造函数。您正在设置CheckInDateDay = checkInDateDay。但是,构造函数的参数是checkInDateday(注意" day"中的小写d;)。由于Reservation课程中的字段为checkInDateDay,因此专栏CheckInDateDay = checkInDateDay会将字段/属性设置为自身(而不是将CheckInDateDay设置为您的值checkInDateDay。像你期望的那样重新进入构造函数)。将构造函数中的参数更改为public class Reservation { private int _checkInDateDay; public int CheckInDateDay { get { return _checkInDateDay; } set { _checkInDateDay = value; } } } (大写字母D),它应该可以正常工作。

为了防止变量和具有相同或相似名称的字段存在这些问题,这些问题允许您的代码在没有错误的情况下进行编译但是没有按照您的预期进行编译,因此通常的做法是使字段以下划线开头。例如

public class Reservation
{
   public int CheckInDateDay { get; set; }
}

此外,没有理由在您的情况下拥有字段。只需让您的属性自动实现,就像这样。

     query = "SELECT test From Result result " +
                    "JOIN result.test test JOIN result.user user " +
                    "WHERE test.id = :testId AND user.id = :userId " +
                    "AND result.permission.id = :permissionId"),