Arraylists, Collections & Dictionaries...
-
I need help... Here's my situation: I have an array as: string[] Array_A; byte[] Array_B; The data inside the array looks like so: Array_A: Array_B: 0x5628, 0 0x0818, 1 0x56EE, 0 0x56EE, 0 Now.. I want to add the DISTINCT values of Array_A to an 'Array_List', or collection, or dictionary :-) This new 'List' will look like so: COL_A COL_B 0x5628, 0 0x0818, 1 0x56EE, 0 ... note that the values in the B column do not matter at this point. They will be updated by another process, which leads me to my next situation. I need to update the B column by referencing the values in the A column. void UpdateArrayList(string strCheckThis, byte bData) { if(strCheckthis == Col_A) COL_ B = bData } That code is really bad in that I have no idea to know what row I am actually updating, but it gives you the basic idea of what I need to accomplish. So that's it in a nutshell.. I need to Add, Update and Remove rows in this arraylist (or Collection, or Dictionary =) The last question is: Which object fits this method best? ANY help anyone can offer will be met with swift and decisive praise and adoration... Thank you in advance Ray
-
I need help... Here's my situation: I have an array as: string[] Array_A; byte[] Array_B; The data inside the array looks like so: Array_A: Array_B: 0x5628, 0 0x0818, 1 0x56EE, 0 0x56EE, 0 Now.. I want to add the DISTINCT values of Array_A to an 'Array_List', or collection, or dictionary :-) This new 'List' will look like so: COL_A COL_B 0x5628, 0 0x0818, 1 0x56EE, 0 ... note that the values in the B column do not matter at this point. They will be updated by another process, which leads me to my next situation. I need to update the B column by referencing the values in the A column. void UpdateArrayList(string strCheckThis, byte bData) { if(strCheckthis == Col_A) COL_ B = bData } That code is really bad in that I have no idea to know what row I am actually updating, but it gives you the basic idea of what I need to accomplish. So that's it in a nutshell.. I need to Add, Update and Remove rows in this arraylist (or Collection, or Dictionary =) The last question is: Which object fits this method best? ANY help anyone can offer will be met with swift and decisive praise and adoration... Thank you in advance Ray
Use a hash table. Use the Add method to add the values, with the string as key, and the byte as value. Use the ContainsKey method first to check if it already exists in the table, so you don't add duplicates. To retrieve the values, use the Item method. --- b { font-weight: normal; }
-
Use a hash table. Use the Add method to add the values, with the string as key, and the byte as value. Use the ContainsKey method first to check if it already exists in the table, so you don't add duplicates. To retrieve the values, use the Item method. --- b { font-weight: normal; }
Ok.. so far so good! Here's what I have: Hashtable table = new Hashtable(); //-- Simple enough! Here's the data: table.Add("0x5628","0x5628, 0, 0"); table.Add("0x0818","0x0818, 1, 0"); table.Add("0x56ED","0x56ED, 4, 0"); Now, I want to enumerate through each element, and "Do stuff" with it... I thought this might work: foreach ( object item in table) { MessageBox.Show((string)item.ToString()); //-- Do other stuff } ...alas, though... it only displays a message as to what the object is, not what it contains. Ideally, I want to return a string from each element: "0x5628, 0, 0" From here I can parse it to my heart's desire... I just can't seem to get to the data portion. Any ideas?
-
Ok.. so far so good! Here's what I have: Hashtable table = new Hashtable(); //-- Simple enough! Here's the data: table.Add("0x5628","0x5628, 0, 0"); table.Add("0x0818","0x0818, 1, 0"); table.Add("0x56ED","0x56ED, 4, 0"); Now, I want to enumerate through each element, and "Do stuff" with it... I thought this might work: foreach ( object item in table) { MessageBox.Show((string)item.ToString()); //-- Do other stuff } ...alas, though... it only displays a message as to what the object is, not what it contains. Ideally, I want to return a string from each element: "0x5628, 0, 0" From here I can parse it to my heart's desire... I just can't seem to get to the data portion. Any ideas?
You are calling the ToString() method on the object before you typecast it. That will use the Object.ToString() method that just returns the name of the class. You have to typecast the object before you call any methods on it:
((string)item).ToString()
The call to ToString is of course completely superflous on a string object. Just remove it:(string)item
--- b { font-weight: normal; }