Different object name for each object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dotNETnews

    Different object name for each object

    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(TextB ox1.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.


  • Chris Dunaway

    #2
    Re: Different object name for each object

    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?

    Comment

    • dotNETnews

      #3
      Re: Different object name for each object


      "Chris Dunaway" <dunawayc@gmail .com> wrote in message
      news:1108151230 .162539.300810@ g14g2000cwa.goo glegroups.com.. .[color=blue]
      > 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?[/color]

      I need to use IndexOf in my arraylist.


      Comment

      • Chris Dunaway

        #4
        Re: Different object name for each object

        You could use the HashTable and add movies like this:

        MovieCollection .Add("Movie1",m ovie)

        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("M ovie1") Then
        MsgBox("Found the movie!")
        End If

        Hope this helps a little

        Comment

        • dotNETnews

          #5
          Re: Different object name for each object


          "Chris Dunaway" <dunawayc@gmail .com> wrote in message
          news:1108153375 .421511.227940@ l41g2000cwc.goo glegroups.com.. .[color=blue]
          > You could use the HashTable and add movies like this:
          >
          > MovieCollection .Add("Movie1",m ovie)
          >
          > 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("M ovie1") Then
          > MsgBox("Found the movie!")
          > End If
          >
          > Hope this helps a little[/color]

          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?


          Comment

          • Chris Dunaway

            #6
            Re: Different object name for each object

            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.

            Comment

            Working...