Results 1 to 3 of 3

Thread: [RESOLVED] Removing an item from a array

  1. #1

    Thread Starter
    Member Anwin's Avatar
    Join Date
    May 2005
    Posts
    43

    Resolved [RESOLVED] Removing an item from a array

    I know this has been up in earlier posts but I just couldn't figure it out.

    My array is a structure like this:

    VB Code:
    1. Public Structure Appointment
    2.         Public Counter As Integer
    3.         Public Year As Integer
    4.         Public Week As Integer
    5.         Public Weekday As Integer
    6.         Public Frequens As String
    7.         Public Where As String
    8.         Public Note As String
    9.     End Structure
    10.  
    11. Public MyAppointment() As Appointment

    My objective is to remove an item in that array.

    I do it like this (where intindex is the item I want to remove):

    VB Code:
    1. For i = intIndex To MyAppointment.Length - 1
    2.            
    3.       MyAppointment(i) = MyAppointment(i+1)
    4.  
    5.    Next i

    Ok. So far so good. Now I only need to delete the last item of my array. Otherwise the length of my array is still the same (and the two last items of the array will be identical)

    How can I do that ?

    Anwin

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

    Re: Removing an item from a array

    You do it by not using an array in the first place. If you want to be able to add and remove items then you should use a collection. The most basic collection is the ArrayList, but you should use the one that's most appropriate to your circumstances.
    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

  3. #3

    Thread Starter
    Member Anwin's Avatar
    Join Date
    May 2005
    Posts
    43

    Re: Removing an item from a array

    You are probably right that I should have used Arraylist in the first place.
    However I found a solution that was quite simple. I already have a variable that counts when I add an item to my array. I call my variable NumberOfItems.

    VB Code:
    1. NumberOFItems -= 1
    2. ReDim Preserve MyAppointment(NumberOFItems)

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