Best way to declaring multiple objects
-
I'm new to C# and have been reading up on Classes. In my small app, I want to instantiate a class called 'backupJob'. There are two operationType members: local to remote server and syncing both ways. I want to create an object for one op type and then create another object for the other type. In the demos I have seen they always show this: Car myCar1 = new Car(); Car myCar2 = new Car(); I may end up with more than two backup jobs so I'm thinking there must be a better way to manage the object names besides myCar1 and myCar2, etc. I will want to delete an object as well as create more objects so is it best to keep track of these objects in an Array or possibly a Collection? Thanks...
-
I'm new to C# and have been reading up on Classes. In my small app, I want to instantiate a class called 'backupJob'. There are two operationType members: local to remote server and syncing both ways. I want to create an object for one op type and then create another object for the other type. In the demos I have seen they always show this: Car myCar1 = new Car(); Car myCar2 = new Car(); I may end up with more than two backup jobs so I'm thinking there must be a better way to manage the object names besides myCar1 and myCar2, etc. I will want to delete an object as well as create more objects so is it best to keep track of these objects in an Array or possibly a Collection? Thanks...
Create and maintain objects in a collection (list maybe). You can add and remove objects as required from within the list.
Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial
-
I'm new to C# and have been reading up on Classes. In my small app, I want to instantiate a class called 'backupJob'. There are two operationType members: local to remote server and syncing both ways. I want to create an object for one op type and then create another object for the other type. In the demos I have seen they always show this: Car myCar1 = new Car(); Car myCar2 = new Car(); I may end up with more than two backup jobs so I'm thinking there must be a better way to manage the object names besides myCar1 and myCar2, etc. I will want to delete an object as well as create more objects so is it best to keep track of these objects in an Array or possibly a Collection? Thanks...
As Abhinav suggests, a collection is the easiest way to go: declare it as class level as a private List and you can do what you want with it:
private List<Car> myCars = new List<Car>();
...
Car car = new Car();
car.Color = blue;
car.Fuel = Fuels.Diesel;
myCars.Add(car);
...
myCars.Remove(car);
...
foreach (Car car in myCars)
{
car.Respray(Color.Red);
}This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre. Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes - in this case, please bury them in your back garden and water frequently.
-
As Abhinav suggests, a collection is the easiest way to go: declare it as class level as a private List and you can do what you want with it:
private List<Car> myCars = new List<Car>();
...
Car car = new Car();
car.Color = blue;
car.Fuel = Fuels.Diesel;
myCars.Add(car);
...
myCars.Remove(car);
...
foreach (Car car in myCars)
{
car.Respray(Color.Red);
}This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre. Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes - in this case, please bury them in your back garden and water frequently.
-
You're welcome!
This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre. Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes - in this case, please bury them in your back garden and water frequently.