5

I'm using a list to store a pair of hexadecimal values (eg. in the list AD38F2D8, displayed as:Value_A: AD 38 F2 D8).

My question is should I use a Dictionary<string, string> or should I use Dictionary<string, NewCustomObject> to store the hexadecimal string as a pair of strings. (Value: [AD, 38, F2, D8]) instead of (Value: AD38F2D8).

It probably won't make to much of a difference between the two.

With Dictionary<string, string> I would just store each hex string in the dictionary, and then split them up in their respective pairs when I need them. if I use the Dictionary<string, NewCustomObject> I would end up first splitting the hex strings in their respective pairs and then store them in the dictionary.

My question is which should I use? Or should I just keep using lists?

It's not entirely necessary for me to use Dictionary<string, string> as I would still know which index what string is on, just that it would look nicer.

List example:

Index = 0,  Value = 3D95FF08
Index = 1,  Value = 8D932A08

Dictionary<string, string> example:

Index = 0, Key = First,  Value = 3D95FF08
Index = 1, Key = Second, Value = 8D932A08

Dictionary<string, NewCustomObject> example:

Index = 0, Key = First,  Value = 3D, 95, FF, 08
Index = 1, Key = Second, Value = 8D, 93, 2A, 08

NOTE: the Index value in each dictionary example is just to show the corespondence to the others, I know a dictionary does not have Index values but uses keys instead. it's just makes it easier to look at this example.

I came across the last Dictionary string here: C# Dictionary with two Values per Key? I was searching for it as I have used that way several times while writing python code. by storing a list of strings within a dictionary.

Thanks in advance for any help!

EDIT

Each string in either the list or dictionary, would be split in their respective bytes. EG.

BYTE   0   1   2   3 to  6 .....
HEX    AD  12  01  0000859D  .....

Byte 0 would hold the Index Byte 1 would hold the Reference Byte 2 the Flags Byte 3 to 6: The Memb_Number_ID

Therefore, if I split them up before possibly putting these in the dictionary I don't have to do it before I use them in their respective place as I would have to split them up to calculate the index, reference and flags.

The database was given to me in this format, I am forced to work with it the way it currently is, so i can not change it. and can only adapt my code to it.

above example would be outputted as:

Index  Reference Link    Flags          Memb_Number_ID
173     18               +A             John (ID: 34205)
Community
  • 1
  • 1
Raskaroth
  • 639
  • 1
  • 9
  • 25
  • 1
    Why do you think about switching to Dictionary? Isn't the code with the lists working? – Daniel Hilgarth Sep 13 '11 at 14:56
  • If you let us know how you intend to use the collection, someone will provide a code sample along with a recommendation. At the moment, you've only told us what you are storing, not how you intend to access it later. – Adam Houldsworth Sep 13 '11 at 15:01
  • The code with the lists is working fine, i'm just looking for a way to make it abit easier to work with. each Hexadecimal value is stored in the database as 1 string, but as explained above each set of characters within that string have their own meaning, therefore putting it in a dictionary with the split up pairs might make things easier, to display the information. – Raskaroth Sep 13 '11 at 15:27

3 Answers3

4

Dictionaries do not expose indexes, they don't have the concept. I'd stick with a list:

List<NewCustomObject> myHexValues;

And if you need to search through it, you can use LINQ. Only use dictionaries if you need fast access based on a defined key.

You haven't really stated how you intend to use the collection, so no one can really tell you to use one or the other as yet - I'm only guessing based on the fact you want index values.

Update: if you want easier access to elements of the hex value, you can either use a rectangular array or wrap the value in a custom type and expose properties for the different parts (your choice on struct vs class, depends on the values I suppose), and then stick that type in a list.

If you make your own type, you can expose an indexer if you wish to get the following syntax:

myCustomType[indexKey] = value;

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • actually the index is just there to show the corresponces to the list. i don't need the indexes. it's just either i would do like myhexvalues[0] or myhexvalues[1]. or the dictionaries respective method – Raskaroth Sep 13 '11 at 15:05
  • @Raskaroth how do you want to use the collection? Ignore for a moment what collection you will eventually use. Do you want to key information out or iterate? – Adam Houldsworth Sep 13 '11 at 15:07
  • If you want to "index" them out, as in, use a value that isn't the "key" of the dictionary, then you can't, you need to use indexes. – Adam Houldsworth Sep 13 '11 at 15:09
  • I added additional information, I hope that will explain it more clearly. – Raskaroth Sep 13 '11 at 15:22
  • Thank you, using an rectangular Array would suit the program best I believe. – Raskaroth Sep 13 '11 at 20:03
0

If you need to associated two (or more) values to each other, a Tuple is what you need. A dictionary is unnecessary because you aren't using the one of the values as a lookup (key)... are you?

Based on what I think you're situation is, I'd say a collection (list works fine) of Tuples.

Also, why are you storing a Hex value as a String? You can leave it as an integer and use String.Format({0:X}, value); to display it as a Hexidecimal String.

myermian
  • 31,823
  • 24
  • 123
  • 215
  • Because initially it is stored in the database as a binary, and i use a method explained here: http://stackoverflow.com/questions/6640898/c-asp-net-show-binary-data-from-database . This is pretty much a continuation of that code, just now that i'm splitting it up to work further with it. it works, but i'm looking for a more efficient way. – Raskaroth Sep 13 '11 at 15:30
  • and no I'm not using the key to lookup the value, I don't necessarily have to. Forgot about tuple's though, thanks for the reminder! – Raskaroth Sep 13 '11 at 15:30
  • Tupple's would not work, as the hexadecimal values are stored 1 after the other, therefore if i have 8 strings stored in that database, and each string contains 8 pairs of hexadecimal values. then i would have a tupple of: 8 * ( 8 * 2 ) = 128 values. And my database stores 80 strings of hexadecimal in one database value, imagine how big that tupple would be? or I would need to create 80 individual tupple's with 8 pairs of hex values in them. – Raskaroth Sep 13 '11 at 15:37
0

Why not parse the hex string immediately you get it into a real object? You've demonstrated what the object looks like (with the index, reference links, etc.). I'd personally parse it straight up into a Record object (or something like that) and then store them in a List. That way its much easier to query them as well as being in a more intuitive format than storing a hex string or array of hex strings or whatever...

Chris
  • 27,210
  • 6
  • 71
  • 92
  • Thing is 1 hex string from the database contains 80 strings, which contain 8 pairs. that's why i need something more easily usable. – Raskaroth Sep 13 '11 at 19:20