Jump to content

Problems while using an array of classes

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Graham-lol

Graham-lol

    Newbie

  • Members
  • Pip
  • 8 posts
Hey guys,

I'm currently working on my college project and having a problem while using an array of classes. The problem is that when I try to call a list of the data held in the classes it shows a repeated list of the last item in the array. I'll post a few parts of the program and attach a copy of the code. It's no where near complete and comments are patchy.

This is the class
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]flight
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] flightNo; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//holds unique number to identify flights
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] departureAirport; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//hold the departure airport
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] arrivalAirport; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//holds the arrival airport
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] flightStatus; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//holds status of flight
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//diplays the details held about the flight
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] displayFlightDetails()
{
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Flight Number: \t\t\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + flightNo);
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Departure Airport \t\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + departureAirport);
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Arrival Airport \t\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + arrivalAirport);
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Flight Status \t\t\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + flightStatus);
}
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//creates new flight
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] CreateNewFlight([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] counter)
{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].flightNo = counter; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//defines the unique flight number
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Flight Number: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].flightNo);
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Write([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Please Enter Departure Airport: - "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].departureAirport = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ReadLine();
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Write([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Please enter Arrival Airport: - "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].arrivalAirport = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ReadLine();
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].flightStatus = 1;
}
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] checkNumber()
{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].flightNo;
}
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] checkStatus()
{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]this[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].flightStatus;
}
}
[/SIZE]

My decleration of the class:
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]
flight[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][] flights = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]flight[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][20];
[/SIZE]


This is where the problem arises
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//booking seat
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Clear();
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Booking A Seat"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine();
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Avaliable Flights"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Number\tDeparts from\tArrives at\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][] avaliableFlights = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][20];
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] avaliableFlightsCounter = 0;
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i = 0; i < flightCounter; i++)
{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (flights[i].checkStatus() == 1)
{
 
 
//This is where I try to output the data
 
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine(flights[i].flightNo + [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + flights[i].departureAirport + [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + flights[i].arrivalAirport);
avaliableFlights[avaliableFlightsCounter] = flightCounter;
 
 
 
 
}
}
[/SIZE]


Hope to hear back from someone soon.

Attached Files



#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
OK, yes, I can explain this, but I think you're terminology is off somewhat. I'll first explain my issue with that, and then explain the flawed logic.

When you say Array of Classes, you should say, Array of Objects. A class is a definition, an object is an actual instance of that class.

Now, when you are creating a new flight, you are using the array, and correctly choosing the element within that array, but you are never actually creating a new object.

At the beginning of your Main Method, you have flight MyFlight = new flight();, which is instantiating the new flight.

And then each time you create a new flight (by the menu you created), you are simply referencing that instance of the same flight. So when you then ask to set the properties to that flight, the departure and arrival, you are modifying that one object, which is being referenced by each element of the array. So in fact, every element is affected.

This might be easier, if did it like this.

eg

switch (selection) {
    case 1:
        //creating new flight

        Console.Clear(); //clears the screen
        Console.WriteLine("Create New Flight");
        Console.WriteLine();
        [COLOR="red"]flight new_flight = new flight();[/COLOR]
        flights[flightCounter] = [COLOR="red"]new_flight;[/COLOR]
        seats[flightCounter] = MySeat; //initialise array instance
        flights[flightCounter].CreateNewFlight(flightCounter); //calls the create new flight method
        Console.Clear();
        flights[flightCounter].displayFlightDetails(); //calls display flight details method
        Console.ReadLine();
        flightCounter++;
        break;


I hope that helps

#3
Graham-lol

Graham-lol

    Newbie

  • Members
  • Pip
  • 8 posts
Thanks a lot, sorry about bad terminology I've only been doing OOP for a few months and still get things mixed up.

That's me sorted it :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users