Click here to Skip to main content
15,891,253 members
Home / Discussions / C#
   

C#

 
AnswerRe: Keeping constant strings Pin
Kalvin @ Work9-May-08 2:45
Kalvin @ Work9-May-08 2:45 
GeneralRe: Keeping constant strings Pin
N a v a n e e t h9-May-08 3:14
N a v a n e e t h9-May-08 3:14 
GeneralRe: Keeping constant strings Pin
Roger Alsing10-May-08 4:15
Roger Alsing10-May-08 4:15 
GeneralRe: Keeping constant strings Pin
N a v a n e e t h10-May-08 23:22
N a v a n e e t h10-May-08 23:22 
GeneralRe: Keeping constant strings [modified] Pin
PIEBALDconsult11-May-08 7:12
mvePIEBALDconsult11-May-08 7:12 
AnswerRe: Keeping constant strings Pin
PIEBALDconsult9-May-08 7:21
mvePIEBALDconsult9-May-08 7:21 
GeneralRe: Keeping constant strings Pin
N a v a n e e t h10-May-08 23:23
N a v a n e e t h10-May-08 23:23 
QuestionAccessing index arrays via string keys [modified] Pin
MrEyes9-May-08 0:38
MrEyes9-May-08 0:38 
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

AnswerRe: Quick Pin
Christian Graus9-May-08 0:39
protectorChristian Graus9-May-08 0:39 
GeneralRe: Quick Pin
MrEyes9-May-08 0:51
MrEyes9-May-08 0:51 
GeneralRe: Quick Pin
Christian Graus9-May-08 0:54
protectorChristian Graus9-May-08 0:54 
GeneralRe: Quick Pin
MrEyes9-May-08 0:59
MrEyes9-May-08 0:59 
GeneralRe: Quick Pin
Christian Graus9-May-08 1:01
protectorChristian Graus9-May-08 1:01 
GeneralRe: Quick Pin
MumbleB9-May-08 1:04
MumbleB9-May-08 1:04 
GeneralRe: Quick Pin
MrEyes9-May-08 1:24
MrEyes9-May-08 1:24 
AnswerRe: Accessing index arrays via string keys Pin
PIEBALDconsult9-May-08 7:26
mvePIEBALDconsult9-May-08 7:26 
QuestionNeed help with ToolStripContainer Pin
mcbass9-May-08 0:24
mcbass9-May-08 0:24 
QuestionWeird "Inconsistent Accessibility" problem [modified] Pin
#realJSOP9-May-08 0:03
mve#realJSOP9-May-08 0:03 
AnswerRe: Weird "Inconsistent Accessibility" problem Pin
Christian Graus9-May-08 0:29
protectorChristian Graus9-May-08 0:29 
GeneralRe: Weird "Inconsistent Accessibility" problem Pin
#realJSOP9-May-08 1:26
mve#realJSOP9-May-08 1:26 
GeneralRe: Weird "Inconsistent Accessibility" problem Pin
tgrt9-May-08 1:49
tgrt9-May-08 1:49 
QuestionAbstract class with no body Pin
NarVish8-May-08 23:56
NarVish8-May-08 23:56 
AnswerRe: Abstract class with no body Pin
John Ad9-May-08 0:27
John Ad9-May-08 0:27 
AnswerRe: Abstract class with no body Pin
PIEBALDconsult9-May-08 7:41
mvePIEBALDconsult9-May-08 7:41 
Questionmeta data for assembly Pin
George_George8-May-08 23:52
George_George8-May-08 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.