By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,949 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,949 IT Pros & Developers. It's quick & easy.

Concise loading of data into a hashtable ?

P: n/a
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.
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"mark4asp" <ma****************@ntlworld.com> wrote in message
news:k2********************************@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
Nov 16 '05 #2

P: n/a
On Tue, 28 Sep 2004 08:47:08 -0500, "David Browne" <davidbaxterbrowne
no potted me**@hotmail.com> wrote:

"mark4asp" <ma****************@ntlworld.com> wrote in message
news:k2********************************@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

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.