I am making an application to store tickets for boarding a place for a imaginary airline. I have created a Ticket class see below
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)
{
}
I have two ticket classes, first and economy. The plane can only hold 10 seats. So my structure is having two arrays of ticket objects, one containing 4 ticket objects "first class" and one containing 6 ticket objects "economy class". See below
ticket[] ticketFirst = new ticket[4];
ticket[] ticketEcon = new ticket[6];
I have to assign the seats randomly within their respective ticket arrays. I am calling my get method (example below) to check for duplicates, that is, that the randomly assigned seat is already filled or not.
public String getLastName()
{
return this.lastName;
}
When I run my program I get the Error WindowsFormsApplication1.ticket' is a 'type' but is used like a 'variable' (relevant code below)
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");
}
}
What my intention was is to assign the new ticket object a random seat (position) in the array, and most of my experience is with java. I think this is a syntax error from me using java-like syntax. Any pointers to get this to work properly?
Full Program Below
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;
}
}
}
To shed more light on the "PlayerMovement is a type but is used like a variable" error: This implies that you have a "PlayerMovement" class somewhere in your code. Otherwise you'd get a completely different error. It's generally OK to have classes, functions, and members sharing the same name.
An object variable is always a reference-type. Classes and string are reference type. Struct and enum are kind of value types.
The typeof is an operator keyword which is used to get a type at the compile-time. Or in other words, this operator is used to get the System. Type object for a type.
The object type is an alias for System. Object in . NET. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from System.
Your ticket[rand]
needs to be either ticketFirst[rand]
or ticketEcon[rand]
. They also need to be defined outside of the Form1 constructor to be able to access them elsewhere in the form.
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");
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With