我正在申请存储为假想航空公司登机的门票。 我创建了一个Ticket类,见下面的
public class ticket
{
String lastName;
String firstName;
String origin;
String destination;
String flightNumber;
String seatNumber;
String date;
public ticket()
{
}
public ticket(String lastname, String firstName, String origin, String destination,
String flightNumber, String seatNumber, String date)
{
}
我有两个门票类,第一个是经济舱。这架飞机只能容纳10个座位。所以我的结构有两个票对象数组,一个包含4个票对象“第一类”,另一个包含6个票证对象“经济类”。见下文
ticket[] ticketFirst = new ticket[4];
ticket[] ticketEcon = new ticket[6];
我必须在各自的票证阵列中随机分配座位。我正在调用我的get方法(下面的示例)来检查重复项,即随机分配的席位是否已填充。
public String getLastName()
{
return this.lastName;
}
当我运行我的程序时,我得到错误WindowsFormsApplication1.ticket'是'type'但是像'变量'一样使用(下面的相关代码)
private void btnSubmit_Click(object sender, EventArgs e)
{
Random random = new Random();
int rand = random.Next(0, 4);
if (ticket[rand].getLastName = null)
{
ticket[rand].setLastName = txbLastName.Text;
ticket[rand].setFirstName = txbFirstName.Text;
ticket[rand].setOrigin = txbOrigin.Text;
ticket[rand].setDestination = txbDestination.Text;
ticket[rand].setFlightNumber = txbFlightNumber.Text;
ticket[rand].setSeatNumber = txbSeatNumber.Text;
ticket[rand].setDate = txbDate.Text;
}
else
{
MessageBox.Show("Seat Assignment Failed, try again.", "Seat Assignment");
}
}
我的意图是将新的票证对象分配给数组中的随机座位(位置),我的大部分经验都是使用java。我认为这是我使用类似java语法的语法错误。是否可以使其正常工作?
下面的完整程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ticket[] ticketFirst = new ticket[4];
ticket[] ticketEcon = new ticket[6];
}
private void btnSubmit_Click(object sender, EventArgs e)
{
Random random = new Random();
int rand = random.Next(0, 4);
if (ticket[rand].getLastName = null)
{
ticket[rand].setLastName = txbLastName.Text;
ticket[rand].setFirstName = txbFirstName.Text;
ticket[rand].setOrigin = txbOrigin.Text;
ticket[rand].setDestination = txbDestination.Text;
ticket[rand].setFlightNumber = txbFlightNumber.Text;
ticket[rand].setSeatNumber = txbSeatNumber.Text;
ticket[rand].setDate = txbDate.Text;
}
else
{
MessageBox.Show("Seat Assignment Failed, try again.", "Seat Assignment");
}
}
}
public class ticket
{
String lastName;
String firstName;
String origin;
String destination;
String flightNumber;
String seatNumber;
String date;
public ticket()
{
}
public ticket(String lastname, String firstName, String origin, String destination,
String flightNumber, String seatNumber, String date)
{
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getLastName()
{
return this.lastName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getFirstName()
{
return this.firstName;
}
public void setOrigin(String origin)
{
this.origin = origin;
}
public String getOrigin()
{
return this.origin;
}
public void setDestination(String destination)
{
this.destination = destination;
}
public String getDestination()
{
return this.destination;
}
public void setFlightNumber(String flightNumber)
{
this.flightNumber = flightNumber;
}
public String getFlightNumber()
{
return this.flightNumber;
}
public void setSeatNumber(String seatNumber)
{
this.seatNumber = seatNumber;
}
public String getSeatNumber()
{
return this.seatNumber;
}
public void setDate(String date)
{
this.date = date;
}
public String getDate()
{
return this.date;
}
}
}
答案 0 :(得分:8)
您的ticket[rand]
必须是ticketFirst[rand]
或ticketEcon[rand]
。它们还需要在Form1构造函数之外定义,以便能够在表单的其他位置访问它们。
e.g。
public partial class Form1 : Form
{
ticket[] ticketFirst = new ticket[4];
ticket[] ticketEcon = new ticket[6];
public Form1()
{
InitializeComponent();
ticketFirst = new ticket[4];
ticketEcon = new ticket[6];
}
private void btnSubmit_Click(object sender, EventArgs e)
{
Random random = new Random();
int rand = random.Next(0, 4);
if (ticketFirst[rand].getLastName = null)
{
ticketFirst[rand].setLastName = txbLastName.Text;
ticketFirst[rand].setFirstName = txbFirstName.Text;
ticketFirst[rand].setOrigin = txbOrigin.Text;
ticketFirst[rand].setDestination = txbDestination.Text;
ticketFirst[rand].setFlightNumber = txbFlightNumber.Text;
ticketFirst[rand].setSeatNumber = txbSeatNumber.Text;
ticketFirst[rand].setDate = txbDate.Text;
}
else
{
MessageBox.Show("Seat Assignment Failed, try again.", "Seat Assignment");
}
}
}
答案 1 :(得分:3)
您的票证数组仅存在于表单构造函数中。为了在事件,方法中使用它......你必须在类上创建一个字段......
public partial class Form1 : Form
{
private Ticket[] tickets = new Ticket[5];
...
}
在C#中你应该:
TitleCase
作为班级名称(而不是ticket
,名称为Ticket
)答案 2 :(得分:0)
在.NET中,约定是提供类型TitleCase
的名称,但是您的类ticket
是小写的,这会导致编译器在名为ticket
的变量与您的类之间混淆,也称为ticket
。
将类的名称更改为标题为Ticket
,它将起作用。