Concise loading of data into a hashtable ?

  • Thread starter Thread starter mark4asp
  • Start date Start date
M

mark4asp

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?

TIA.
 
mark4asp said:
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,values);
}

David
 
mark4asp said:
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,values);
}

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
 
Back
Top