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. Web Development
  3. ASP.NET
  4. Data type. Multi pair of values. How can I do this?

Data type. Multi pair of values. How can I do this?

Scheduled Pinned Locked Moved ASP.NET
questiontutorial
2 Posts 2 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.
  • S Offline
    S Offline
    shapper
    wrote on last edited by
    #1

    Hello, I need to pass to a function a list of pairs of values. For each item the first value is a string and the second value is an Enum named Country. For example, consider I need to pass 3 items: "New York", Country.UnitedStates "Paris", Country.France "London", Country.England What data type should I use create this? And how can I access each item inside my function? Thanks, Miguel

    S 1 Reply Last reply
    0
    • S shapper

      Hello, I need to pass to a function a list of pairs of values. For each item the first value is a string and the second value is an Enum named Country. For example, consider I need to pass 3 items: "New York", Country.UnitedStates "Paris", Country.France "London", Country.England What data type should I use create this? And how can I access each item inside my function? Thanks, Miguel

      S Offline
      S Offline
      szukuro
      wrote on last edited by
      #2

      For storing one object you can either use System.Web.UI.Pair or a custom class. The former takes two objects, so you must cast them apropriately after getting the value:

      Pair p = new Pair("New York", Country.UnitedStates);
      String s = (String)p.First;
      Country c = (Country)p.Second;

      The latter can and should be typed, so no casting is necessary. You just define the members and create properties for them. Independent from your decision I also know two options, for storing multiple objects. One would be ArrayList, which again takes an object value, so casting is needed. It's still better, then a simple array, beacuse of the variable length. The second option would be a generic List, but that's only for .NET 2.0. It's typed, so no need for casting. Also you do the typing yourself.

      List<Pair> pairs = new List<Pair>();
      pairs.Add(new Pair("New York", Country.UnitedStates));
      //Use your custom class here instead if Pair, if you decide, to do it that way

      There is a third option for storing your object, Dictionary, which also is a generic (and therefore a .NET 2.0 feature). It is based on key-value pair, where both of them are typed. But you probably don't need that.

      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