Giving new instance of a Form a unique 'ID'

Posted on 2006-04-28
Medium Priority
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
  • 3
  • 3
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16567497
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

nayernaguib earned 500 total points
ID: 16568620
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 86

Expert Comment

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

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 14

Expert Comment

ID: 16571078

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 86

Expert Comment

by:Mike Tomlinson
ID: 16571350
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

ID: 16571855
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Make the most of your online learning experience.
Simple Linear Regression
Six Sigma Control Plans

862 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