Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2004

Tip: Looking for answers? Try searching our database.

Concise loading of data into a hashtable ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mark4asp - 28 Sep 2004 13:47 GMT
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.
David Browne - 28 Sep 2004 14:47 GMT
> Hello,
>
[quoted text clipped - 18 lines]
> 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
mark4asp - 28 Sep 2004 18:23 GMT
>> Hello,
>>
[quoted text clipped - 33 lines]
>
>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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.