Reputation: 37
I am busy with a really small console application for c#. I am just new into c# and i'm only familiar with Java. My school gave the assignment to create a console application that simulates a car dealer. I already wrote a bunch of code to add cars by console. Things like brand and type and max speed are already implemented. The only thing i still need to realize is the auto creation and incremental of an ID for a car. It has the be unique of course.
My first approach was making the id field static and increment it in the constructor so that each time the object gets created the id gets ++;
I saw allot of people on stackoverflow doing allot of things but the solutions did not work or the where to big.
This is my code;
class Car : Vehicle
{
public string brand { get; set; }
public string type { get; set; }
public int maxSpeed { get; set; }
public double price { get; set; }
public static int carID { get; set; }
public Car(string _brand, string _type, int _maxspeed, double _price)
{
this.brand = _brand;
this.type = _type;
this.maxSpeed = _maxspeed;
this.price = _price;
this.carID++;
}
}
I added 3 cars to my list but the result is that all cars have ID 3;
Maybe someone could help me, thanks in advance.
Upvotes: 2
Views: 25399
Reputation: 216343
A static variable is shared between all instances of the class.
Incrementing it in the constructor makes all instances of that class 'see' the last value assigned to the static variable.
You should create a normal instance carID
variable (not static) and use a protected static variable defined in the base class to grab the current value at constructor time, assign to your carID instance variable and then increment the base class value.
class Vehicle
{
protected static int FakeID = 1;
}
class Car : Vehicle
{
public string brand { get; set; }
public string type { get; set; }
public int maxSpeed { get; set; }
public double price { get; set; }
public int carID { get; set; }
public Car(string _brand, string _type, int _maxspeed, double _price)
{
this.brand = _brand;
this.type = _type;
this.maxSpeed = _maxspeed;
this.price = _price;
this.carID = base.FakeID++;;
}
}
void Main()
{
Car a = new Car("xyz", "Auto", 120, 12000);
Car b = new Car("kwx", "Moto", 180, 8000);
Console.WriteLine(a.carID);
Console.WriteLine(b.carID);
}
Keep in mind that this will work correctly if your code doesn't use multithreaded access to the constructor. In case of multithreading you need to look at Interlocked.Increment
Upvotes: 0
Reputation: 7856
Just to remove static and add lock section for avoiding duplicate of Ids
class Car : Vehicle
{
private static object sync = new object();
private static int _globalCount;
public string brand { get; set; }
public string type { get; set; }
public int maxSpeed { get; set; }
public double price { get; set; }
public int carID { get; set; }
public Car(string _brand, string _type, int _maxspeed, double _price)
{
this.brand = _brand;
this.type = _type;
this.maxSpeed = _maxspeed;
this.price = _price;
lock (sync)
{
this.carID = ++globalCount;
}
}
}
Upvotes: 1
Reputation: 5667
class Car : Vehicle
{
public string brand { get; set; }
public string type { get; set; }
public int maxSpeed { get; set; }
public double price { get; set; }
public int carID { get; private set; }
public static int globalCarID;
public Car(string _brand, string _type, int _maxspeed, double _price)
{
this.brand = _brand;
this.type = _type;
this.maxSpeed = _maxspeed;
this.price = _price;
this.carID = Interlocked.Increment(ref globalCarID);
}
}
This maintains a global ID counter, and increments it atomically to make it thread-safe.
Note that the first ID assigned this way would be 1. You can initialize globalCarID
with -1 to start at 0.
Upvotes: 4