|
-
Feb 16th, 2007, 08:54 AM
#1
Thread Starter
Member
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:
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:
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
-
Feb 16th, 2007, 02:25 PM
#2
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
 
-
Feb 17th, 2007, 01:08 PM
#3
Thread Starter
Member
Re: manipulating arraylist?
hmm..i got that but i am not sure how to do it. i have a structure declared
VB Code:
Public Structure fish2
Public altitude As Integer
Public ID As String
Public time As DateTime
Public icon As GpsViewNET.Icon
Public label As GpsViewNET.Label
End Structure
And then i have this arraylist declared
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
-
Feb 17th, 2007, 11:37 PM
#4
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?
-
Feb 18th, 2007, 02:46 AM
#5
Thread Starter
Member
Re: manipulating arraylist?
-
Feb 18th, 2007, 03:33 AM
#6
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.
-
Feb 18th, 2007, 09:37 PM
#7
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
 
-
Feb 18th, 2007, 09:54 PM
#8
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.
-
Feb 18th, 2007, 11:21 PM
#9
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
 
-
Feb 21st, 2007, 07:33 PM
#10
Thread Starter
Member
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:
myarraylist.item(i).propertyA = 5
would be...
VB Code:
Ctype(myarraylist.item(i).propertyA, integer) = 5
???
OR
VB Code:
DirectCast(myarraylist.item(i).propertyA, integer) = 5
???
or none of the above???
-
Feb 21st, 2007, 08:07 PM
#11
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:
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:
DirectCast(myArrayList.Item(i), ClassA).PropertyA = 5
-
Feb 21st, 2007, 08:10 PM
#12
Thread Starter
Member
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
-
Feb 21st, 2007, 08:42 PM
#13
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|