|
-
Jul 5th, 2006, 10:02 AM
#1
Thread Starter
Member
removing an element from the array
I am populating a listbox from an array. What I want is that when somebody clicks on an item on the listbox and then hits the delete button that it deletes that value in the array and in the listbox. How do I do that?
Thank You!
-
Jul 5th, 2006, 12:46 PM
#2
Re: removing an element from the array
Set the value to (Nothing)
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 5th, 2006, 05:39 PM
#3
Hyperactive Member
Re: removing an element from the array
That's a good quick solution, but you could run into runtime errors if you try to loop through the array (because it has a null value in it). You could right a loop to move every item after the deleted one back by one index to avoid this.
Note: This might not be fully accurate. I'd know for sure in C, but I'm still learning some of the finer (or not so fine) details of VB...
-
Jul 5th, 2006, 06:21 PM
#4
Re: removing an element from the array
ZaNi, we already have two obsessive Aussies (JM and Penagate) on this forum, are you going to be a third? Well, good.
I think the best solution depends on the situation. If the array is of numbers, perhaps rather than setting it to Nothing, you could set it to an out of range value. For instance, if all the values are positive integers, then set it to -1. You can clean it up later, the -1 is just a placeholder for the time being. If you are using strings, then perhaps the empty string "" will do.
However, you might also consider how the listbox and the array will be used. Is it essential that they remain synched? Could several things be removed from the listbox before the array is used again? If so, the solution might be to simply wait until deletions have completed, then recreate the array from the listbox, rather than deleting each item as it comes up.
If you have to actually delete the items from the array, you will have the advantage that ListItem x will mean delete array(x). In that case, start from index x+1, copying Array(x+1) to Array(x) until x+1 > array indexMax. Hmmm, I was thinking that Array(x) = Array(x+1) wouldn't work for reference types, but it seems now that it should.
My usual boring signature: Nothing
 
-
Jul 5th, 2006, 07:43 PM
#5
-
Jul 5th, 2006, 08:01 PM
#6
Re: removing an element from the array
If possible I'd suggest using a collection instead of an array for the reason ZaNi quotes. Use a StringCollection, ArrayList, List or whatever is most appropriate.
-
Jul 5th, 2006, 09:59 PM
#7
Re: removing an element from the array
Now we have two in one thread. Well, if you are as productive as JM and Penagate, there won't be much need of the rest of us around here.
To get back to the original question, which version of VS are you using? There are better ArrayList options in 2005. I'm not all that enamored of it in 2003, not for any solid reason, it just doesn't "feel" as type safe to me.
My usual boring signature: Nothing
 
-
Jul 5th, 2006, 10:15 PM
#8
Re: removing an element from the array
Just to make it three:
Why do you need to maintain your own array in the first place? You've already got the list of items in the listbox, always up to date. Maybe you could elaborate on the reasons behind what you're trying to achieve?
-
Jul 6th, 2006, 09:25 AM
#9
Junior Member
Re: removing an element from the array
As above ^^ the dropdown list is an array in itself.
However, using a linked list would avoid this problem, I know VB6 didn't support linked lists, not sure if .NET does as I've not needed them recently.
In either case a simple linked list can be created with a 2D array, whereby the second part of each array element points to the next field, to delete an element just change it to point to another element, certainly this removes the problem of null values even if it over-complicates things.
-
Jul 6th, 2006, 02:13 PM
#10
Re: removing an element from the array
There are no simple 2D arrays in VB, a 1D array of classes or structures can be done more easily. However, I posted some suggestion about something like that about a year ago, and several people chimed in to point out that there was a built-in feature that worked better.....I just forget what it was at the moment.
My usual boring signature: Nothing
 
-
Jul 6th, 2006, 02:28 PM
#11
Junior Member
Re: removing an element from the array
When I say 2D array I mean as this:
VB Code:
Dim StrArray(5,2) As Integer
Therefore the contents of which can be arranged as below, where the array item (n,2) is the pointer to the next item in the linked list. Although as you say chances are that there's now a function in VB that can do this.
Code:
# | value | pointer
1 | 5 |
2 | 2 | 5
3 | 4 | 1
4 | 1 | 2
5 | 3 | 3
-
Jul 6th, 2006, 04:09 PM
#12
Re: removing an element from the array
I understood what you meant, but because it is so painful to extend a 2D array in VB, I feel that a 1D array of structures or classes is almost always a better option. Your structure would look something like this:
VB Code:
Public structure Node
Public Value as integer
Public Pointer as integer
End structure
My usual boring signature: Nothing
 
-
Jul 6th, 2006, 04:16 PM
#13
Junior Member
Re: removing an element from the array
Ah, sorry, I misunderstood, I can see how that would make things easier
-
Jul 6th, 2006, 05:33 PM
#14
Re: removing an element from the array
.NET does have a LinkedList class but it is not required. The point of a linked list is to be able to get to the next, and possibly the previous, node from the current node. That is not required in this situation. If all that is required is that you be able to remove items then a simple collection is all that's required. That's all the Items property of the ListBox is anyway: a simple collection.
Basically there are two sensible choices here. If you do not need the data maintained separately to the ListBox then don't use a an array or collection or anything else. Just use the Items property of the ListBox itself. If you do need the data maintained in a location separate to the ListBox then you would use a simple collection. In .NET 1.x you would use a StringCollection, ArrayList or your own collection derived from CollectionBase. In .NET 2.0 you would use a List(Of T) where T is the type of the objects.
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
|