Groups | Blog | Home
all groups > vb.net > february 2005 >

vb.net : Different object name for each object


Chris Dunaway
2/11/2005 11:47:10 AM
Do you wish to refer to your object by a certain name? You can add
them to a hash table instead of an arraylist and then assign a name as
the "Key".

Or you can add a Name property to your class and assign the name when
you create the object.

What do you need to do with the name?
Chris Dunaway
2/11/2005 12:22:55 PM
You could use the HashTable and add movies like this:

MovieCollection.Add("Movie1",movie)

and then later access the movie using:

Dim m As AllMovies = MovieCollection("Movie1")

or to see if the MovieCollection had a certain movie, you could use
this:

If MovieCollection.ContainsKey("Movie1") Then
MsgBox("Found the movie!")
End If

Hope this helps a little
Chris Dunaway
2/11/2005 12:51:46 PM
If you had an instance of an object and wanted to see if that object
was already in the arraylist you could use indexof. Or if you wanted
to prevent adding the same object more than once.
dotNETnews
2/11/2005 8:09:11 PM
This clas creates new movie.

--------------CODE--------------
Public Class AllMovies
Sub New(ByVal MovieName As String, ByVal MovieLenght As Integer)
Movie = MovieName
Lenght = MovieLenght
End Sub
Public Movie As String
Public Lenght As String
End Class
-----------END CODE-------------

Then I use Click event of the button to create object and add it to
ArrayList.

--------------CODE--------------
Private Sub AddMovie_Click(...) Handles AddMovie.Click
Dim movie As AllMovies = New AllMovies(TextBox1.Text, TextBox2.Text)
MovieCollection.Add(movie)
End Sub
-----------END CODE-------------

Is there any way I could name the new object As movie1, movies2, movie3 and
so on. Right now all of my on+bjects in ArrayList are called movie.

dotNETnews
2/11/2005 9:13:14 PM

[quoted text, click to view]

I need to use IndexOf in my arraylist.

dotNETnews
2/11/2005 9:32:25 PM

[quoted text, click to view]

Thanks, I'll rewrite my app like you suggested. What's the point of IndexOf
if you can't use it when you create a program in a way I did?

AddThis Social Bookmark Button