• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

Giving new instance of a Form a unique 'ID'


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!
  • 3
  • 3
1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
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....
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
Mike TomlinsonMiddle School Assistant TeacherCommented:
The Collection() class is the VB6 version of the VB.Net HashTable() class...
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).


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
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now