Results 1 to 7 of 7
  1. #1
    Lounger
    Join Date
    Oct 2007
    Location
    Wirral, UK
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  2. #2
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Vienna, Wien, Austria
    Posts
    5,009
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Gre

  3. #3
    Lounger
    Join Date
    Oct 2007
    Location
    Wirral, UK
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  4. #4
    Lounger
    Join Date
    Oct 2007
    Location
    Wirral, UK
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  5. #5
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Vienna, Wien, Austria
    Posts
    5,009
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Glad you got there & shared your solution with us!
    Gre

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    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?

  7. #7
    New Lounger
    Join Date
    Feb 2010
    Location
    Mars
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jscher2000 View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •