Reputation: 463
I have been trying to create a program that allows the user to edit things such as the supply of mp3 players which have a base value stored in the program:
public struct MP3_Players
{
public int ID { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public int MBsize { get; set; }
public decimal Price { get; set; }
public int Supply { get; set; }
}
MP3_Players player1 = new MP3_Players
{
ID = 1,
Make = "GET Technologies .inc",
Model = "HF 410",
MBsize = 4096,
Price = 129.95m,
Supply = 500
};
MP3_Players player2 = new MP3_Players
{
ID = 2,
Make = "Microsoft",
Model = "SOFT1",
MBsize = 1028,
Price = 432.99m,
Supply = 500
};
MP3_Players player3 = new MP3_Players
{
ID = 3,
Make = "PEAR",
Model = "APPLE3",
MBsize = 2056,
Price = 130.00m,
Supply = 500
};
MP3_Players player4 = new MP3_Players
{
ID = 4,
Make = "logitech",
Model = "DEF0",
MBsize = 2056,
Price = 53.35m,
Supply = 500
};
MP3_Players player5 = new MP3_Players
{
ID = 5,
Make = "Sennheiser",
Model = "HEIS1",
MBsize = 2056,
Price = 399.95m,
Supply = 500
};
Now making a method with a list isn't really an issue, I imagine it goes as follows:
public static void mutate()
{
List<MP3_Players> PlayerList = new List<MP3_Players>;
Playerlist.Add(player1);
Playerlist.Add(player2);
Playerlist.Add(player3);
Playerlist.Add(player4);
Playerlist.Add(player5);
}
My first question is simply a matter of improvement of code, but I suppose many people reading this will understand the problems I'm running into, primarily that player1 is not static, does this mean I will have to individually make every player static or is there an easier way?
The primary issue however lies with my attempts to create a small program to change the Supply, The idea is for a user to enter an ID and then allow them to edit the supply, this went well for me when I was using an array but I am having issues understanding how to use data in a list<>
and change it(basically trying to reach the values stored in the list from other methods if possible at all).
Upvotes: 1
Views: 112
Reputation: 2289
In C#, Structs are Immutable. This means that they can't be edited. If you wish to make a change to an item, then it needs to be replaced by a totally new item. You can avoid this by using a class.
One problem that I have ran into editing collections in an enumeration is that the collection was modified and the enumeration can no longer continue. This was happening because I was using a foreach loop that evaluates each item in the collection. I avoided it by using for loops and fetching the element from the list using its index and performing my edit there.
Upvotes: 0
Reputation: 17605
An easier way would be to initialize the list by
List<MP3_Players> PlayerList = new MP3Players[]{
new MP3_Players()
{
ID = 1,
Make = "GET Technologies .inc",
Model = "HF 410",
MBsize = 4096,
Price = 129.95m,
Supply = 500
}, <-- include more instances of MP3_Players here
}.ToList();
and furthermore, the indexer []
can be used to access the elements in a list very similar to an array.
Upvotes: 1
Reputation: 18127
One way is to create dictionary like other user explain if the ID of the players are Unique, if they are not doesn't start this approach. This is requirement of dictionary !
The other way using List.
List<MP3_Players> newList = PlayerList.FindAll(x=>x.Model == Apple3);
newList.ToList().ForEach(x => x.Storage = 30);
This will return every Player with Model Apple3 and second row will change all the values for Storage.
Upvotes: 0
Reputation: 179
Have you considered using collection initializer for inlining the creation of the PlayerList?
Where static is concerned, static method cannot access non static instance members.
Upvotes: 0
Reputation: 23103
To solve your "list problem" you could use classes and a dictionary:
public class MP3_Players
{
/* ... */
}
public static void mutate()
{
var PlayerList = new Dictionary<int, MP3_Players>;
PlayerList.Add(player1.ID, player1);
PlayerList.Add(player2.ID, player2);
PlayerList.Add(player3.ID, player3);
PlayerList.Add(player4.ID, player4);
PlayerList.Add(player5.ID, player5);
}
Then you can access them via their ID and change them:
var player = PlayerList[ID];
player.Supply--;
player.Price -= 10;
BUT to solve your bigger problem of "persistency" you should think of a "data-store" to persist you data, e.g. Entity Framework and a DB.
Upvotes: 0