Different object name for each object

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.
 
C

Chris Dunaway

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?
 
D

dotNETnews

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.
 
C

Chris Dunaway

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
 
D

dotNETnews

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?
 
C

Chris Dunaway

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top