• Collections in Visual Basic 2008

    Home » Forums » Developers, developers, developers » DevOps Lounge » Collections in Visual Basic 2008

    Author
    Topic
    #466412

    I’m trying to work out how to retrieve an object from a collection in VB 2008.

    The class defining the object is called Predator and has three public properties:

    strName (string)
    strKind (string)
    blnIsBig (boolean)

    example “Dogfish”, “Shark”, False

    I have a collection class called PredatorCollection. There is no problem adding Predator objects to it using the Add method:

    Dim Predator1 As New Predator(“Dogfish”, “Shark”, False)
    PredatorCollection.Add(Predator1).

    What I am trying to do is retrieve the strName property from the the last object in the collection. If I key:

    PredatorCollection.Item(PredatorCount).

    Intellisense gives me 5 options; Equals, GetHashCode, GetType, ReferenceEquals and ToString. I suspect ToString is the one to use, but it gives a concatenation of all the properties in the object. I only want strName. There must be a way to do it!

    Viewing 4 reply threads
    Author
    Replies
    • #1208039

      If you’re firstCapping all your entries, then it seems something to do with searching ToString for the 2nd instance of UpperCase & only retaining everything before that. HTH

    • #1208082

      I can’t believe it’s not possible to get the object back unmangled. If you carefully put Easter eggs in a box (objects into collection) you expect to be able to take them out undamaged, you don’t expect them to come out crushed. I have a feeling I should be adding parameters in brackets after ToString, i.e. ToString(something or other) but I can’t work out how to do it.

    • #1208151

      Fiddling around a bit more I think I’ve found the answer. I think you have to unload the object from the collection before you can dismantle it, i.e..

      Dim Predator1 As New Predator(“Dogfish”, “Shark”, False)
      Dim PredatorCount As Integer
      PredatorCollection.Add(Predator1)
      Predator1 = Nothing
      Predator1 = PredatorCollection.Item(PredatorCollection.Count)
      MessageBox.Show(Predator1.strName)

      This displays “Dogfish” as expected.

      Continuing the Easter eggs theme, It seems VB is saying if you have packed your eggs into a crate, and you want to take the little bag of sweets out of one, you must first take that egg out of the crate. Reasonable enough.

    • #1208158

      Glad you got there & shared your solution with us!

    • #1209395

      In “old” VB you could shortcut the code by appending the object property reference to the collection member reference like so:

      Code:
      MessageBox.Show(PredatorCollection.Item(PredatorCollection.Count).strName)

      No longer possible in the world of .Net, or simply undocumented?

      • #1210676

        In “old” VB you could shortcut the code by appending the object property reference to the collection member reference like so:

        Code:
        MessageBox.Show(PredatorCollection.Item(PredatorCollection.Count).strName)

        No longer possible in the world of .Net, or simply undocumented?

        It’s very possible. The poster, however, was asking about a collection of objects which is more like an array. If I put three items in a collection of objects I have to reference the item by index. So assuming a class of EasterEggs with UpperShell, Candy, and LowerShell as the objects within a class:

        Dim Crate As New List(Of EasterEgg)
        Dim Egg1 As new EasterEgg
        Dim Egg2 as New EasterEgg
        Dim Egg3 as New EasterEgg

        Crate.Add(Egg1)
        Crate.Add(Egg2)
        Crate.Add(Egg3)

        ‘ To access the candy of egg2 (assuming it’s convertible string object)
        Dim eggCandy as string = Crate.Item(1).Candy ‘ or Crate(1).Candy because VB 2008 assumes the index is to an item

    Viewing 4 reply threads
    Reply To: Collections in Visual Basic 2008

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: