Results 1 to 14 of 14

Thread: removing an element from the array

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2003
    Posts
    58

    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!

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    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...
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

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

    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

  5. #5
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Re: removing an element from the array

    I believe setting objects to Nothing is dangerous because it can cause null reference exceptions... This is important to be aware of more than anything else. I see a major possibility in someone deciding to set things to nothing, then deciding to handle the exceptions that arise by putting an empty try/catch combination... This is a lazy and inefficient solution (I think you'll agree there).

    So all of it loops back to good programming practice, and I believe it is good practice to not have null references as much as possible (I group it with "goto").

    Thank you for noticing that I'm obsessive I've always believed I am, but I don't hear it often enough to confirm it (and I sleep a lot, so people think I'm lazy, which contradicts the obsessive paradigm).

    Now I just have to wait for someone to notice that I'm neurotic
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

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

    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.
    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: 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

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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?

  9. #9
    Junior Member
    Join Date
    Nov 2002
    Posts
    23

    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.

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

    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

  11. #11
    Junior Member
    Join Date
    Nov 2002
    Posts
    23

    Re: removing an element from the array

    When I say 2D array I mean as this:

    VB Code:
    1. 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

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

    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:
    1. Public structure Node
    2.   Public Value as integer
    3.   Public Pointer as integer
    4. End structure
    My usual boring signature: Nothing

  13. #13
    Junior Member
    Join Date
    Nov 2002
    Posts
    23

    Re: removing an element from the array

    Ah, sorry, I misunderstood, I can see how that would make things easier

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

    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.
    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