Giving new instance of a Form a unique 'ID'

Posted on 2006-04-28
Last Modified: 2010-05-18

I'm wondering if there is a way to give each new instance of a form a unique name or ID that i can use to reference back to.. as of now i am using this code to create a new form instance:

Private Sub listBuddy_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listBuddy.DoubleClick
        Dim newIM As New imform()
        newIM.Name = listBuddy.SelectedItem.ToString
    End Sub

Basically what i want to be able to do is have a reference to each new instance so i can have something to the effect of...

Dim myname As String = "daniel"

Dim newIM(myname) As New imform()
newIM(myname).txtbox.Text = "whatever"

the idea is that I am making an instant messenger application and when a user double clicks the name in their buddylist it creates a new instance of the "imform" which includes textbox, send button... etc, and I want to be able to name each instance so when an instant message is recieved, it know's what instance of the form to go into... or if one doesn't exist yet, it can create a new instance of the form with the desired name.

Any help would be appreciated, Thanks!
Question by:j3n0vacHiLd
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    This is exactly what a HashTable is for...accessing an object by key.

    Store the instances of your form in a HashTable using the buddies name as the key....
    LVL 14

    Accepted Solution

    Add the following declaration:

        Public forms As Collection

    Next, try the following piece of code:

            Dim form1 As New Form2
            Dim form2 As New Form2
            Dim form3 As New Form2
            Dim tempForm As Form
            forms = New Collection()
            form1.Name = "form21"
            form2.Name = "form22"
            form3.Name = "form23"
            forms.Add(form1, form1.Name)
            forms.Add(form2, form2.Name)
            forms.Add(form3, form3.Name)
            tempForm = forms("form21")
            tempForm = forms("form22")
            tempForm = forms("form23")


      Nayer Naguib
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    The Collection() class is the VB6 version of the VB.Net HashTable() class...
    LVL 14

    Expert Comment


    First, the code I posted above is VB .NET, not VB 6 code.
    Second, a Collection is *not* a Hashtable, although both have similar behavior:


      Nayer Naguib
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    I know it's VB.Net code...

    What I'm saying is you shouldn't use the Collection class as it is really only there in the .Net Framework to support backwards compability with VB6 code.

    This is why the Collection class is part of the Microsoft.VisualBasic namespace...and not part of the System.Collections namespace like the rest of the preferred .Net classes.

    You are using the Collection class in exactly the same way as a HashTable would be used.
    LVL 14

    Expert Comment

    Well, ...

    1. I do not agree with you about not recommending the use of the Collection class. Personally, I believe that having the Collection class (together with other VB 6.0 classes and functions, such as MkDir(), MsgBox(), Left(), etc...) included in the Microsoft.VisualBasic namespace is not for backward compatability purposes. Rather, the purpose is to continue providing Visual Basic programmers with the old, easy-to-use VB library. What makes me think that backward compatability is not the real intention is that

      a. Almost 100% of your old VB 6.0 source code already needs modification before it would compile on VB .NET. Having to make a few additional
          modifications such as replacing the statement MsgBox() with MessageBox.Show() is not that hard for a programmer migrating his code.
      b. These classes and functions are not marked as "deprecated".
      c. As far as I know, there are three versions of the .NET Framework released until today (1, 1.1 and 2.0). If Microsoft would like to discontinue the
          Visual Basic library, it wouldn't have supported it even in .NET Framework 2.0.

    The reason for not including these classes and functions in other namespaces, is simply that programmers writing in other .NET languages are not familiar with this library, and the .NET FCL already includes other classes that provide the same functionality. A C++ .NET programmer will *not* prefer calling Left(string, length) to calling String.Substring(index, length). A VB .NET programmer might.

    2. Although the code above used the Collection class in a way similar to a hashtable object, again, the Collection is *not* a hashtable! The Collection object also represents an **ordered** list that allows you to retrieve an element using an index ***as well as*** using a key. A hashtable ***would not*** allow you to access an element using its index, simply because a hashtable element should not have an index!! Similarly, the System.Collections.SortedList class allows you to retrieve an element using *only* a key object. To retrieve elements using indices, you need to use the Values.CopyTo() method of a Hashtable or a SortedList object, or use a System.Collections.ArrayList object, which, as you might already guess, does not allow you to retrieve an element using a key! Therefore, no single class from the System.Collections namespace directly provides the same functionality as Microsoft.VisualBasic.Collection.


      Nayer Naguib

    Featured Post

    Training Course: Android App Development

    This course will involve creating widgets, customize list view, grid view, spinners, etc. Creating applications using audio, video, and SQLite database. Ending with publication on Google Play.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Adobe Customization Wizard XI issues 26 135
    wefewf 2 28
    pairs challenge 5 30
    Arduino EDI - Programming language 3 31
    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now