Different object name for each object

  • Thread starter Thread starter dotNETnews
  • Start date Start date
D

dotNETnews

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.
 
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 said:
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?

I need to use IndexOf in my arraylist.
 
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 said:
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

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?
 
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.
 
Back
Top