Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Best way to set up this table in a custom list/array?

Best way to set up this table in a custom list/array?

Scheduled Pinned Locked Moved C#
questioncsharpdatabasedata-structuresjson
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jan Sommer
    wrote on last edited by
    #1

    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,00

    I 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?

    J 1 Reply Last reply
    0
    • J Jan Sommer

      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,00

      I 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?

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      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.

      J 1 Reply Last reply
      0
      • J J4amieC

        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.

        J Offline
        J Offline
        Jan Sommer
        wrote on last edited by
        #3

        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

        D 1 Reply Last reply
        0
        • J Jan Sommer

          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

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          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)

          J 1 Reply Last reply
          0
          • D DaveyM69

            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)

            J Offline
            J Offline
            Jan Sommer
            wrote on last edited by
            #5

            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?

            D 1 Reply Last reply
            0
            • J Jan Sommer

              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?

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              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)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups