AuthorMessage
Meka][Meka
Unstopable
Posts: 700

note: you may use this tutorial on other sites as long as u post the author note
--------------------------
Hash tables
--------------------------
Author: Meka][Meka
--------------------------
http://www.meka-meka.com/
--------------------------
Hashtables are used for storing items, with quick retrieving, for example of you are using more then 1 of the same class
lets get started:
in your main form class paste
Code:

Private usersdata As New Hashtable

we will use the class and functions from the understanding classes tutorial
http://www.meka-meka.com/forum/viewtopic.php?t=104 < if you have not done this tut, do that first.
ok we need to make 5 or so of the same class, in form1 load paste
Code:

        For i As Integer = 0 To 5
            Dim user As New UserData
            With user
                .sName = "Meka][Meka" & i
                .sDesc = "http://www.meka-meka.com/ number " & i
                .sTag = "some misc data here - " & i
            End With
        Next

unfortuably, u cant pass all of them at once, u will lose 1,2,3,4 but still have the 5th thats made, this is where the hash
table comes in, we need to add the item to the table
Code:

usersdata.Add(user.sName, user)

this is simple to understand, add(nametocallby, theclassoritem)
Code:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 0 To 5
            Dim user As New UserData
            With user
                .sName = "Meka][Meka" & i
                .sDesc = "http://www.meka-meka.com/ number " & i
                .sTag = "some misc data here - " & i
            End With
            usersdata.Add(user.sName, user)
        Next
    End Sub

ok now we need a function to loop thru the table to show u we have all the classes inside
Code:

    Private Sub TheFunction()
        Dim user As UserData
        Dim entry As DictionaryEntry
        For Each entry In usersdata
            user = CType(entry.Value, UserData)
            MsgBox(user.sName & " - " & user.sDesc)
            MsgBox(user.sTag) 'we will call this boxed
        Next
    End Sub

ok now it will loop thru them all, done, but now we need to remove them when not being used,so lets do that in 'TheFunction'
after the names and data have been boxed.
Code:

    Private Sub TheFunction()
        Dim user As UserData
        Dim entry As DictionaryEntry
        For Each entry In usersdata
            user = CType(entry.Value, UserData)
            MsgBox(user.sName & " - " & user.sDesc)
            MsgBox(user.sTag) 'we will call this boxed
        Next
        'remember it cant be in the loop, the indexes would change causing an error
        For i As Integer = 0 To 5
            usersdata.Remove("Meka][Meka" & i)
        Next
    End Sub

run your code and see it loop the table
Corayzon
Regular
Posts: 67

very nice methods meka-meka
good to see i dont have to stick to user control arrays when i want variable length arrays of user definded objects in visual basic 7 .net ^^
yay!