Type used as a variable error

Viewed 136

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

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

Your ticket arrays only exist in the form constructor. In order to use it in events, methods... you must create a field on the class...

public partial class Form1 : Form
{
    private Ticket[] tickets = new Ticket[5];

    ...
}

In C# you should:

  • Use Properties rather than methods for gets and sets.
  • Use TitleCase for class names (instead of ticket, name as Ticket)

In .NET, the convention is to give types TitleCase names, but your class ticket is lowercased, which causes the compiler to be confused between a variable named ticket and your class, also named ticket.

Change the name of the class to the title-cased Ticket and it will work.

Related