gpt4 book ai didi

c# - 用作变量错误的类型

转载 作者:太空狗 更新时间:2023-10-29 22:22:41 28 4
gpt4 key购买 nike

我正在申请存储一张假想航空公司登机地点的机票。我创建了一个 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)字符串 getLastName()
{
返回这个。姓氏;
}

当我运行我的程序时,出现错误 WindowsFormsApplication1.ticket' is a 'type' but is used like a 'variable'(下面的相关代码)

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;
}
}
}

最佳答案

您的 ticket[rand] 需要是 ticketFirst[rand]ticketEcon[rand]。它们还需要在 Form1 构造函数之外定义,以便能够在表单的其他位置访问它们。

例如

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");
}
}
}

关于c# - 用作变量错误的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20410727/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com