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. Accessing index arrays via string keys [modified]

Accessing index arrays via string keys [modified]

Scheduled Pinned Locked Moved C#
questiondatabasedata-structuresxmlhelp
9 Posts 4 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.
  • M Offline
    M Offline
    MrEyes
    wrote on last edited by
    #1

    Hello all, I have an array of a simple object type:

    class EntryPoint
    {
    private widgets[];

    public EntryPoint()
    {
    this.widgets = new widgets[1000]

    ...code here to populate 1000 widgets...
    }

    public Widgets[]
    {
    get { return this.widgets; }
    }
    }

    class widgets
    {
    public string key;
    public string value;
    }

    Now, elsewhere in the the application, I need to be able to extract values from the widgets[] from instances of EntryPoint. However I need to be able to do this using the string key, not the index number. So the question is how can I do this? A hacking but slow way is something like this:

    public string GetWidgetValue(string key)
    {
    string returnValue = null;

    foreach(widget w in this.widgets)
    {
    if (w.Key == key)
    {
    returnValue = w.value;
    break;
    }
    }

    return returnValue;
    }

    This would work, however as mentioned enumerating through the array for every value request could prove to be slow. So can anybody give me an idea of what to do? A perfect solution would be to encapsulate the base array with something like a StringDictionary and therefore do something like this:

    EntryPoint ep = new EntryPoint();
    string value = ep.Widgets["widgetkey"];

    So the question is, how to do this? Obviously the sample above is a simplified version of the question, in reality I am working with an array that is generated by deserialising an XML file into a class generated by the XSD tool. This means that unless I ditch XSD for something else (any suggestions?) I am stuck with integer indexed arrays.

    modified on Friday, May 9, 2008 7:23 AM

    C P 2 Replies Last reply
    0
    • M MrEyes

      Hello all, I have an array of a simple object type:

      class EntryPoint
      {
      private widgets[];

      public EntryPoint()
      {
      this.widgets = new widgets[1000]

      ...code here to populate 1000 widgets...
      }

      public Widgets[]
      {
      get { return this.widgets; }
      }
      }

      class widgets
      {
      public string key;
      public string value;
      }

      Now, elsewhere in the the application, I need to be able to extract values from the widgets[] from instances of EntryPoint. However I need to be able to do this using the string key, not the index number. So the question is how can I do this? A hacking but slow way is something like this:

      public string GetWidgetValue(string key)
      {
      string returnValue = null;

      foreach(widget w in this.widgets)
      {
      if (w.Key == key)
      {
      returnValue = w.value;
      break;
      }
      }

      return returnValue;
      }

      This would work, however as mentioned enumerating through the array for every value request could prove to be slow. So can anybody give me an idea of what to do? A perfect solution would be to encapsulate the base array with something like a StringDictionary and therefore do something like this:

      EntryPoint ep = new EntryPoint();
      string value = ep.Widgets["widgetkey"];

      So the question is, how to do this? Obviously the sample above is a simplified version of the question, in reality I am working with an array that is generated by deserialising an XML file into a class generated by the XSD tool. This means that unless I ditch XSD for something else (any suggestions?) I am stuck with integer indexed arrays.

      modified on Friday, May 9, 2008 7:23 AM

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Quick ? What sort of subject line is that ? The obvious solution is a Dictionary, that's what they are for.

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      M M 2 Replies Last reply
      0
      • C Christian Graus

        Quick ? What sort of subject line is that ? The obvious solution is a Dictionary, that's what they are for.

        Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        M Offline
        M Offline
        MrEyes
        wrote on last edited by
        #3

        Christian Graus wrote:

        Quick ? What sort of subject line is that ?

        An extremely poor one, my error - we all make them from time to time :doh:

        Christian Graus wrote:

        The obvious solution is a Dictionary, that's what they are for.

        Yes, however as mentioned in the original post, I am working with classes generated by the XSD tool. As such I need underpin the stringdictionary with the simple arrays this tool creates and keep the array and the dictionary in synchronisation as code adds, removes, edits values. Unfortunately this is where I am not sure where to go or what to do.

        C 1 Reply Last reply
        0
        • M MrEyes

          Christian Graus wrote:

          Quick ? What sort of subject line is that ?

          An extremely poor one, my error - we all make them from time to time :doh:

          Christian Graus wrote:

          The obvious solution is a Dictionary, that's what they are for.

          Yes, however as mentioned in the original post, I am working with classes generated by the XSD tool. As such I need underpin the stringdictionary with the simple arrays this tool creates and keep the array and the dictionary in synchronisation as code adds, removes, edits values. Unfortunately this is where I am not sure where to go or what to do.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          WEll, you really cannot lookup by string unless you build a container that works that way. Why not just build a dictionary at the point of deserialising the list, then use that for lookups ?

          Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          M 1 Reply Last reply
          0
          • C Christian Graus

            WEll, you really cannot lookup by string unless you build a container that works that way. Why not just build a dictionary at the point of deserialising the list, then use that for lookups ?

            Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            M Offline
            M Offline
            MrEyes
            wrote on last edited by
            #5

            Well thats the idea, wrap the base array with a StringDictionary. However I need to keep the StringDictionary and base array in sync as code adds/edits/removes items regardless of whether this is done by string key on integer index. So the question is how to do it?

            C 1 Reply Last reply
            0
            • M MrEyes

              Well thats the idea, wrap the base array with a StringDictionary. However I need to keep the StringDictionary and base array in sync as code adds/edits/removes items regardless of whether this is done by string key on integer index. So the question is how to do it?

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              I guess you need to write a class which contains an array and a dictionary, so that you only add/remove/access items via the interface of that class, which then ensures they are kept in sync Or, you could use the Values collection of your dictionary as your plain vanilla list of items ?

              Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

              1 Reply Last reply
              0
              • C Christian Graus

                Quick ? What sort of subject line is that ? The obvious solution is a Dictionary, that's what they are for.

                Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                M Offline
                M Offline
                MumbleB
                wrote on last edited by
                #7

                Christian Graus wrote:

                The obvious solution is a Dictionary

                And they work brilliantly.

                Excellence is doing ordinary things extraordinarily well.

                M 1 Reply Last reply
                0
                • M MumbleB

                  Christian Graus wrote:

                  The obvious solution is a Dictionary

                  And they work brilliantly.

                  Excellence is doing ordinary things extraordinarily well.

                  M Offline
                  M Offline
                  MrEyes
                  wrote on last edited by
                  #8

                  Yes they do, however what I don't know how to do is encapsulate the XSD tool generate simple array with a StringDictionary while keeping everything in sync and not breaking the (de)serialiser, and therefore allow the following:

                  string val1 = widget[0]
                  string val2 = widget["mykey"]

                  Console.Writeline(val1 == val2)

                  Which would output true

                  1 Reply Last reply
                  0
                  • M MrEyes

                    Hello all, I have an array of a simple object type:

                    class EntryPoint
                    {
                    private widgets[];

                    public EntryPoint()
                    {
                    this.widgets = new widgets[1000]

                    ...code here to populate 1000 widgets...
                    }

                    public Widgets[]
                    {
                    get { return this.widgets; }
                    }
                    }

                    class widgets
                    {
                    public string key;
                    public string value;
                    }

                    Now, elsewhere in the the application, I need to be able to extract values from the widgets[] from instances of EntryPoint. However I need to be able to do this using the string key, not the index number. So the question is how can I do this? A hacking but slow way is something like this:

                    public string GetWidgetValue(string key)
                    {
                    string returnValue = null;

                    foreach(widget w in this.widgets)
                    {
                    if (w.Key == key)
                    {
                    returnValue = w.value;
                    break;
                    }
                    }

                    return returnValue;
                    }

                    This would work, however as mentioned enumerating through the array for every value request could prove to be slow. So can anybody give me an idea of what to do? A perfect solution would be to encapsulate the base array with something like a StringDictionary and therefore do something like this:

                    EntryPoint ep = new EntryPoint();
                    string value = ep.Widgets["widgetkey"];

                    So the question is, how to do this? Obviously the sample above is a simplified version of the question, in reality I am working with an array that is generated by deserialising an XML file into a class generated by the XSD tool. This means that unless I ditch XSD for something else (any suggestions?) I am stuck with integer indexed arrays.

                    modified on Friday, May 9, 2008 7:23 AM

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    If you're using XML, use an XmlDocument. It sounds like your "XSD tool" is causing you more trouble than it's worth.

                    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