Re: Concise loading of data into a hashtable ? by mark4asp
mark4asp
Tue Sep 28 12:23:57 CDT 2004
On Tue, 28 Sep 2004 08:47:08 -0500, "David Browne" <davidbaxterbrowne
no potted meat@hotmail.com> wrote:
>
>"mark4asp" <mark4asp#killspam#@ntlworld.com> wrote in message
>news:k2nil0p4b667hqpr9daspa9k0mn8u571s9@4ax.com...
>> Hello,
>>
>> I am converting an app from JavaScript to ASP.NET
>>
>> I have the following JavaScript 'associative array'. Is there a
>> concise way to create an analogous structure in .NET Framework using
>> c# or VB.NET? I don't want to use JScript.
>>
>> consFin =
>> {2:'m',4:'n',6:'ng',8:'r',10:'l',12:'kh',14:'k',16:'s',18:'hl',19:'tl',20:'s
>> h'};
>>
>> The amount of data is tiny and I have no wish to store it in the
>> database. Likewise, I don't really want to use a pile of
>> hashtable.add() statements to put the data to the hashtable. There are
>> another 6 arrays of similarly sized data and 3 of them are associative
>> arrays too.
>>
>> I could put this into 2-D arrays and have the data read into the
>> hashtable but I would have to store the keys (numbers) as strings (and
>> convert them, or not, to numbers) for my hashtable. Is this the most
>> sensible solution?
>
>I would probably code a couple of static arrays and add them to a hashtable
>at load time.
>
>something like this
>
>int[] keys = {2,4,6,8,10};
>string[] values = { "a", "b", "c", "d" ,"e"};
>Hashtable t = new Hashtable();
>for (int i=0;i<keys.Length;i++)
>{
> t.Add(keys[i],values[i]);
>}
>
>David
I needed to use a SortedList not a HashTable. eg.
Dim AconsFin(,) As String =
{{"2","m"},{"4","n"},{"6","ng"},{"8","r"},{"10","l"},{"12","kh"},{"14","k"},{"16","s"},{"18","hl"},{"19","tl"},{"20","sh"}}
Dim consFin As New SortedList()
Load_SortedList(AconsFin, consFin)
Show_SortedList(consFin)
Sub Load_SortedList(ary, ht)
For i As Integer = 0 To UBound(ary, 1)
ht.Add(CInt(ary(i , 0)), ary(i , 1))
Next
End Sub
Sub Show_SortedList(ht As SortedList)
Dim hEnum As IDictionaryEnumerator = ht.GetEnumerator()
Dim str As String
While hEnum.MoveNext()
str += hEnum.Key.toString() & " : " &
hEnum.Value.toString() & vbCrLf
End While
txtEncounter.text = str
End Sub