Best way to set up this table in a custom list/array?
-
I have this table (with the price for sending a letter to different countries):
WEIGHT Denmark Europe Other Countries
50 g | 6,00 10,50 13,50
100 g | 8,25 15,00 21,00
250 g | 13,50 24,00 36,50
500 g | 22,50 40,00 59,00
1000 g | 30,00 66,00 100,00I need to be able to type which country i want to send a letter to in a console, 1 = Denmark for instance. Then specify the weight of the letter, 120 for instance, and then the program will output 13,50. What is the best way to deal with this if i also want to be able to edit, delete or add prices, weigthts and countries? Normally i would create a database dealing with this, but i'm trying to figure out some more advanced List<>'s in C#. A hint would be great so i could figure out the rest myself. I've tried to figure out how to automatically assign id's to custom classes when added to a List<>, but i can't seem to find a solution. Is it even the right way to do this?
-
I have this table (with the price for sending a letter to different countries):
WEIGHT Denmark Europe Other Countries
50 g | 6,00 10,50 13,50
100 g | 8,25 15,00 21,00
250 g | 13,50 24,00 36,50
500 g | 22,50 40,00 59,00
1000 g | 30,00 66,00 100,00I need to be able to type which country i want to send a letter to in a console, 1 = Denmark for instance. Then specify the weight of the letter, 120 for instance, and then the program will output 13,50. What is the best way to deal with this if i also want to be able to edit, delete or add prices, weigthts and countries? Normally i would create a database dealing with this, but i'm trying to figure out some more advanced List<>'s in C#. A hint would be great so i could figure out the rest myself. I've tried to figure out how to automatically assign id's to custom classes when added to a List<>, but i can't seem to find a solution. Is it even the right way to do this?
Jan Sommer wrote:
Is it even the right way to do this?
Arguably no. You had the right idea to start with - a database is for this sort of thing. However, if this is just an excercise in understanding generic List's then you just create a class which holds the relevant data, store it in a list, and serialize that list to the filesystem for the purpose of storage.
-
Jan Sommer wrote:
Is it even the right way to do this?
Arguably no. You had the right idea to start with - a database is for this sort of thing. However, if this is just an excercise in understanding generic List's then you just create a class which holds the relevant data, store it in a list, and serialize that list to the filesystem for the purpose of storage.
Well the program doesn't have to save the data on the filesystem, it should only be stored in memory.. i could ofcourse use your method and delete the file afterwords.. but how do i relate the price to weight and country? i'm pretty lost on that part. i can't relate them through their index, because if a weight or country gets deleted, then i'm in trouble. EDIT: and it's kind of stupid to assign id's myself.. there -must- be a way to do it automatically ??
modified on Monday, September 15, 2008 5:12 AM
-
Well the program doesn't have to save the data on the filesystem, it should only be stored in memory.. i could ofcourse use your method and delete the file afterwords.. but how do i relate the price to weight and country? i'm pretty lost on that part. i can't relate them through their index, because if a weight or country gets deleted, then i'm in trouble. EDIT: and it's kind of stupid to assign id's myself.. there -must- be a way to do it automatically ??
modified on Monday, September 15, 2008 5:12 AM
Which part of your data is most constant? Once you've decided that you should build your classes around that. For example - if the weights will rarely change, but you are likely to be adding and removing countries then I'd go for something like...
public class WeightCollection : List<Weight>
{
// ...
}
public struct Weight
{
public Weight(int grammes, List<CountryCost> costs)
{
// ...
}
// ...
}
public struct CountryCost
{
public CountryCost(string country, decimal cost)
{
// ...
}
// ...
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog) -
Which part of your data is most constant? Once you've decided that you should build your classes around that. For example - if the weights will rarely change, but you are likely to be adding and removing countries then I'd go for something like...
public class WeightCollection : List<Weight>
{
// ...
}
public struct Weight
{
public Weight(int grammes, List<CountryCost> costs)
{
// ...
}
// ...
}
public struct CountryCost
{
public CountryCost(string country, decimal cost)
{
// ...
}
// ...
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog)Thanks! i'll see if i can figure it out. Is a struct the way to go with this, and why not use a class?
-
Thanks! i'll see if i can figure it out. Is a struct the way to go with this, and why not use a class?
It doesn't really make any difference, classes are easier to deal with though. I tend to use structs for all my values (structs are value types) and classes for the final objects that use those values. I personally find this makes more sense, but unless you're 100% sure you're handling value types correctly - use classes.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog)