Collections vs Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AaronL
    New Member
    • Jan 2007
    • 99

    Collections vs Arrays

    Hello,

    I need help determining what the best way to store properties of multiple items.

    For example, I am creating a list box and every item in the list box needs to have properties. I don't want to use arrays because I'm not going to know the maximum items in the list box. I read briefly on Microsoft's site about "collection s" but I don't quite understand how they are used. Can someone give me an example of what I would do as far as code.

    Thanks!
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    collection is the best solution for your case.

    you just need to create an use defined type and use that.

    Comment

    • AaronL
      New Member
      • Jan 2007
      • 99

      #3
      I'm not sure how to use collections, can you give me some code examples??

      Comment

      • rpicilli
        New Member
        • Aug 2008
        • 77

        #4
        This is how to use a collection or list of


        [CODE]

        Dim myList as new List(Of String)

        myList.Add("Som ething")
        myList.Add("Oth er thing no matter what the size is")

        /[CODE]

        Tell me if this help you

        Comment

        • AaronL
          New Member
          • Jan 2007
          • 99

          #5
          Ok, it helps a little, but how to you have multiple fields in a list for example, if I were to create a list that had

          Name
          Address
          Phone Number
          Email

          I would want all of those in different fields and reference them somewhat like this

          MyList.Name(ind ex number)

          or something of that sort, how would I do that? Or do I have to use delimiters in the string??

          Comment

          • AaronL
            New Member
            • Jan 2007
            • 99

            #6
            Also, I want to make my own database, is this the best way to do this in VB6??

            Comment

            • rpicilli
              New Member
              • Aug 2008
              • 77

              #7
              Hi Chelf

              You want to create a database this is not the best way. Try to use a table.

              About your first question you can use struture and keep it into the List of string.

              Create a new project and put in a form just one buttom.

              Copy this code and try to understand it.

              [CODE]
              Public Structure Customer
              Public name As String
              Public age As Integer
              Public address As String
              Public gender As String
              End Structure
              Dim myList As New List(Of Customer)

              Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button2.Click
              'Instanciate a structure
              Dim CurCustomer As Customer
              'Put some information on the structure
              CurCustomer.nam e = "John"
              CurCustomer.age = 24
              CurCustomer.add ress = "Some address"
              CurCustomer.gen der = "Male"
              'Save in the list
              myList.Add(CurC ustomer)
              End Sub
              /[CODE]

              I hope this help you

              Comment

              • AaronL
                New Member
                • Jan 2007
                • 99

                #8
                That's great!! Thank you for your help. Now you mentioned that this isn't the best way to make a database? What do you recommend as far as designing your own database, I don't want to use SQL etc.. I want this to be proprietary...

                Comment

                • rpicilli
                  New Member
                  • Aug 2008
                  • 77

                  #9
                  Ok, if you want to make your data proprietary, you'll need to do by yourself.

                  In the current market if you intend to create a commercial product, you'll repel your possible customer as soon as they know about proprietary data.

                  Think about it.

                  Rpicilli

                  Comment

                  • AaronL
                    New Member
                    • Jan 2007
                    • 99

                    #10
                    I'm using VB6 and I noticed that Structure and List doesn't work with it, I figured out how to make a new structure with the Type declaration but how do I do the List using the Collection declaration? I noticed I can't use Dim MyList as New Collection(Of Type) it says expected end of statement

                    Comment

                    • rpicilli
                      New Member
                      • Aug 2008
                      • 77

                      #11
                      Hi,

                      Excuse me by this. I don't have the VB6 and I don't remember how to do that. I'll try to figured out and let you know.

                      Rpicilli

                      Comment

                      • AaronL
                        New Member
                        • Jan 2007
                        • 99

                        #12
                        Does anyone know what I'm doing wrong?

                        Public Type UserData
                        ID As String
                        Password As String
                        End Type
                        Public User As UserData
                        Public UserFile As Collection

                        Shouldn't I be able to do something like

                        UserData.ID = "username"
                        UserData.Passwo rd = "password"
                        UserFile.Add(Us er)

                        I get some weird message about data types being coerced or something stupid. I just want to make a simple collection instead of using an array, why is this so hard to do?

                        Comment

                        Working...