0

I have a base 'Vehicle' class:

public abstract class Vehicle
{
    private string company;
    private string model;

    private static int ID = 0;

    public Vehicle(string company, string model)
    {
        this.company = company;
        this.model = model;
        ID++;
    }

    public override string ToString()
    {
            return "\n\nVehicle Information: \n\t" +
                    "ID: "+ID+"\n\t"+
                    "Car: " + company + "\n\t" +
                    "Model: " + model + "\n\t";
    }
}

Now I have an inherited class 'Ford', inherited from Vehicle:

public class Ford : Vehicle
{
    public Ford(string company, string model) : 
                               base(company, model)
    {

    }        
}

I also have another inherited class 'Honda', inherited from Vehicle:

public class Honda: Vehicle
{
    public Honda(string company, string model) : 
                               base(company, model)
    {

    }        
}

Now in my Main method, I call the derived classes Ford and Honda, and add them to an ArrayList:

class Test
{
    static void Main(string[] args)
    {
        ArrayList vehicleList = new ArrayList();

        Ford firstVehicle = new Ford("Ford", "Fusion");
        vehicleList.Add(firstVehicle);


        vehicleList.Add(new Honda("Honda", "Civic"));


        foreach (Vehicle x in vehicleList)
        {
            Console.WriteLine(x);
        }
    }
}

The problem is that, when I run it, I get the following output:

Vehicle Information:
    ID:2
    Car:Ford
    Model:Fusion
Vehicle Information:
    ID:2
    Car:Honda
    Model:Civic

As you can see, both the objects show the ID column '2' instead of 1 for the first and 2 for the second. When I used the breakpoint to detect whats happening, I see that when the first object is processed, the arrayList shows ID=1 for the first object, but when the second object is processed and added to the arrayList, the ID value of the first object too is changed to 2 from 1. I think this is because it is using 'add by reference'?? any suggestions what can I do to show ID:1 for the first and ID:2 for the second?

2 Answers 2

1

ID is static, therefore a singleton. There is one instance of it for the application (shared by all instances of Vehicle)

Start by changing this:

private static int ID = 0;

To this:

private static intCounter = 0;
private int ID = 0;

Then where your ID is being set replace:

ID++;

...with...

intCounter++;
ID = intCounter;
Sign up to request clarification or add additional context in comments.

5 Comments

so what's my alternative ?
@RajivGanti, don't use static ID...why you need a static id in this case? care to explain?
@davidshen84 I think he's using it to generate unique ID's.
When I don't use 'static' and like I say: private int ID = 0; then in the output, I get: ID:1 for both objects
@RajivGanti try the solution I suggested, but also it is a good idea to understand how static fields work. There is one instance for your application, not per class instance, so calling Honda.ID and Ford.ID will give the same value.
0
private static int ID = 0;
private int instanceID;
public Vehicle(string company, string model)
{
    this.company = company;
    this.model = model;
    instanceID = ID++;
}

...and use instanceID in ToString().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.