Results 1 to 13 of 13

Thread: manipulating arraylist?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    61

    manipulating arraylist?

    Hi there,
    I have this arraylist that holds only one kind of objects (a stracture). My question is how can i change properties of those objects. So far i am taking an object like that
    VB Code:
    1. tempobj = myarraylist.item(i)
    and then change what is needed on tempobj and insert the tempobj back at that position. But that doesnt seem very convenient.
    Is there any way to change properties easier...smth like
    VB Code:
    1. myarraylist.item(i).propertyA = 5
    for example
    Maybe the arraylist is not the optimal tool to use here?
    thanks in advance
    Last edited by sarris; Feb 16th, 2007 at 08:54 AM. Reason: misspelling on title

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: manipulating arraylist?

    Which version are you using? In .NET2003, you didn't have really good options. If you are using 2005, however, look at the List (of ...). That is a generic list, where you designate the type with that (of....) part. This tells the comipler what type is found in the list, and works much better if you only have a single type (as you stated).

    Otherwise, you will have to explicitly tell the compiler which type you have to get access to the properties. You might do this most efficiently using DirectCast(myarraylist.item(i), <item type>).propertyA.

    Since the compiler doesn't know what specific type is in the arraylist, it assumes they are all type Object, which doesn't have the attibutes you want. By casting it (using either DirectCast, or CType), you are telling the compiler that it isn't just an object, it is a specific type of object.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    61

    Re: manipulating arraylist?

    hmm..i got that but i am not sure how to do it. i have a structure declared
    VB Code:
    1. Public Structure fish2
    2.         Public altitude As Integer
    3.         Public ID As String
    4.         Public time As DateTime
    5.         Public icon As GpsViewNET.Icon
    6.         Public label As GpsViewNET.Label
    7.     End Structure
    And then i have this arraylist declared
    VB Code:
    1. dim x as arraylist
    which is supposed to contain only fish2 objects.

    So do i cast so the arraylist knows what it keeps?
    Or every time that i want to access a property i cast?
    How exactly?
    Sorry, am being quite new to this, and asking all those questions
    Thanks in advance

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: manipulating arraylist?

    The point of the List(Of T) is to provide the same functionality as the ArrayList but with strong typing. Are you using CF1 or CF2?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    61

    Re: manipulating arraylist?

    i am using CF 1

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: manipulating arraylist?

    Then you have a choice of using an ArrayList, in which case you cannot restrict the types of objects you add and you must cast each item you retrieve, or else you can declare your own class derived from CollectionBase, which wraps an ArrayList and allows you to declare your own properties and methods of the specific type you want the collection to contain.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: manipulating arraylist?

    Or you can use an array of Fish2 objects, but then you have to do all of the adding and deleting on your own. I actually prefer this technique because of the control it offers. Of course, I wrap the array in a class with members to do the adding removing, etc., which is entirely unnecessary unless you want to do some really specific adding and deletion.

    One thing you can do if you make a class that contains the array, is to have the array be a certain fixed size, and have the add and delete functions do no more than toggle array indexes, so that the array remains the same size, but things are added, removed, and indexed based on whether or not they are currently available.

    I don't think I said that very well, but it's kind of a common speed enhancement technique based on the fact that allocating and freeing memory is relatively slow, so games and the like try to avoid doing much of that. By pre-creating an array of structures, and adding a field to indicate whether a particular structure is valid, you can delete an object by simply toggling that one field, and add a structure by toggling the field, and setting the other fields to the desired values.

    Now I'm rambling, though, so I'll shut up.
    My usual boring signature: Nothing

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: manipulating arraylist?

    Given that the ArrayList IS a class that wraps an array then I'd say that that is extra work unnecessarily. If you're going to implement your own class wouldn't you be better off inheriting CollectionBase, which already has the functionality to add and remove items.

    The ArrayList, and thus CollectionBase, try to optimise perfromance by keeping memory reallocation to a minimum. They start with a 16-element array by default. Each time an Add operation breaches the current Capacity the size of the internal array is doubled. This is is somewhat like your idea of the static array but it also allows you to grow the array if needed, thus you don't have to create a ridiculously large array just in case.

    Each approach has it's pros and cons. I'd always go with the collection in a regular WinForms app but maybe there is some perfromance gain to be had on a hand-held by going the other way.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: manipulating arraylist?

    Yeah, I'm not saying my solution was the best one. I think I was actually doing that more because I could rather than because it was the best possible solution. Memory can be an issue in the CF, but not THAT much of an issue.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    61

    Re: manipulating arraylist?

    I think i will go with the array list. So far i had it as Shaggy Hiker said, simple array, but i was doing redim every time an element came in. The idea of doubling seems really nice as well...But to have the option, if i use the array list, jmcilhinney said
    you must cast each item you retrieve
    How do i do that exactly?

    VB Code:
    1. myarraylist.item(i).propertyA = 5
    would be...

    VB Code:
    1. Ctype(myarraylist.item(i).propertyA, integer) = 5
    ???
    OR
    VB Code:
    1. DirectCast(myarraylist.item(i).propertyA, integer) = 5
    ???
    or none of the above???

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: manipulating arraylist?

    When you retrieve an item from the ArrayList it is returned to you as an Object reference. That means that you cannot access any members of the item, i.e. the return value of the Item property, other than those inherited from the Object class. If you want to access an item's PropertyA then you first have to cast the item as a type that has a PropertyA property. You already know that PropertyA is type Integer so that's not an issue. This:
    VB Code:
    1. DirectCast(myArrayList.Item(i), ClassA)
    returns the item from the collection as a ClassA reference instead of the Object reference returned by the Item property. You can now access the PropertyA property of that object:
    VB Code:
    1. DirectCast(myArrayList.Item(i), ClassA).PropertyA = 5
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    61

    Re: manipulating arraylist?

    nice, i see...where classA is the name of the object right? Its allright if its a structure and not a class? I guess structures are objects afterall

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: manipulating arraylist?

    I said class out of habit because the types we use are classes more often than not. DirectCast just requires you specify a type though. Types can be classes, structures, delegates, enumerations, etc.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width