View Single Post
Old 01-03-2013, 02:57 PM   PM User | #8
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
You need to look into collections and generic lists and adding to them. I don't even see a 'basket variable' in your code. I made this quick example. If you create a new project (console app), delete everything from the default page, and copy/paste everything from here it should debug just fine and you will see what is going on (or maybe just reading through the code will be good enough)
Code:
Module Module1

    Sub Main()
        Try


            Dim myAnimals As New List(Of Animals)


            Console.WriteLine("This is an example of adding objects to a collecion, which {0}" _
                              & "is what you need to learn how to do. Right now the collection 'myAnimals'{0}" _
                              & "has {1} members in it. Let's go through and add a few shall we?", vbCrLf, myAnimals.Count)
            Console.ReadLine()

            'This adds a member without any paramerters using the Sub New() constructor
            myAnimals.Add(addAnimalsNoParms())


            Console.WriteLine("Now the collection has {0} members, each with a set of{1}" _
                              & "properties set to default values. Let's see what they are...{1}" _
                              & "Species: {2}{1}" _
                              & "Age: {3}{1}" _
                              & "Eats: {4}{1}" _
                              & "Name: {5}{1}", myAnimals.Count, vbCrLf, myAnimals(0).Species, myAnimals(0).Age, myAnimals(0).Eats, myAnimals(0).Name)
            Console.ReadLine()

            Console.WriteLine("Now let's see another way too add{0}members... (and set properties of the first member){0}*Look at the code to see this*", vbCrLf)
            Console.ReadLine()

            myAnimals(0).Species = "Cow"
            myAnimals(0).Age = 25
            myAnimals(0).Eats = Animals.EatType.herb
            myAnimals(0).Name = "Buttercup"

            'this way does a straight add to the collection using the function
            myAnimals.Add(addAnimalsWithParms("Turtle", 12, Animals.EatType.omni, "Apple"))

            'this way creates and Animals object, sets it's properties, and then adds it to the collection
            Dim _animal As New Animals
            _animal.Species = "Dog"
            _animal.Age = 7
            _animal.Eats = Animals.EatType.omni
            _animal.Name = "Spot"
            myAnimals.Add(_animal)

            'this way creates an animal using the constructor accepting parameters and adds it (same addition as above, but different substantiation)
            Dim _animal2 As New Animals("Cat", 3, Animals.EatType.carn, "Purrs")
            myAnimals.Add(_animal2)

            'this way adds it as it's being substantiated
            myAnimals.Add(New Animals("Buzzard", 17, Animals.EatType.cari, "Buzzy"))

            Console.WriteLine("You can then see that each is created and has values set.{0}" _
                              & "Total members of collection 'myAnimals' = {1}{0}", vbCrLf, myAnimals.Count)
            Console.ReadLine()

            WriteList(myAnimals)
            Console.WriteLine("{0}Tada!", vbCrLf)
            Console.ReadLine()

        Catch ex As Exception
            Console.WriteLine("{0}{0}{0}{1}", vbCrLf, ex.Message)
            Console.ReadLine()
        End Try
    End Sub

    Private Function addAnimalsNoParms() As Animals
        Return New Animals()
    End Function

    Private Function addAnimalsWithParms(ByVal s As String, ByVal a As Integer, ByVal e As Animals.EatType, ByVal n As String) As Animals
        Return New Animals(s, a, e, n)
    End Function

    Private Sub WriteList(ByVal animalList As List(Of Animals))
        For Each a As Animals In animalList
            Console.WriteLine("Animal at index of: {0} is Named: {1}, Age: {2}, Eats: {3}, and is a {4}", animalList.IndexOf(a), a.Name, a.Age, a.Eats, a.Species)
        Next
    End Sub
End Module

Public Class Animals

    Public Enum EatType
        herb 'herbivore
        carn 'carnivore
        omni 'omnivore
        cari 'carion
        Unknown
    End Enum

    Private _species As String
    Private _age As Integer
    Private _eats As EatType
    Private _name As String

    Sub New()
        Species = "Unknown"
        Age = -1
        Eats = EatType.Unknown
        Name = "Unknown"
    End Sub

    Sub New(ByVal s As String, ByVal a As Integer, ByVal e As EatType, ByVal n As String)
        Species = s
        Age = a
        Eats = e
        Name = n
    End Sub

    Public Property Species As String

        Get
            Return _species
        End Get

        Set(ByVal value As String)
            _species = value
        End Set

    End Property

    Public Property Age As Integer

        Get
            Return _age
        End Get

        Set(ByVal value As Integer)
            _age = value
        End Set

    End Property

    Public Property Eats As EatType

        Get
            Return _eats
        End Get

        Set(ByVal value As EatType)
            _eats = value
        End Set

    End Property

    Public Property Name As String

        Get
            Return _name
        End Get

        Set(ByVal value As String)
            _name = value
        End Set

    End Property

End Class
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote