Remove an element from an array

Craig

Well-known member
Joined
Oct 15, 2005
Messages
110
Location
United Kingdom
Programming Experience
Beginner
Hi all.

I have an array of a class and I need to be able to remove a certain element from the array

Example. I created my class that includes details like:
ID number
FirstName
Lastname
etc etc

then i declare the class in my main form as

public cl_competitors(20) as competitors

Now if i wanted to remove the 15th record how would I go about this?

And is there anyway i could then shift the records that are above it down 1(so that cl_competitors(20) becomes cl_competitors(19) and cl_competitors(20) is removed)

Or would I be better off leaving the Competitors(15) empty once it has been deleted

EDIT: I don't think I will be able to keep competitors(15) empty because, to navigate between records I use a combobox, and if the user were to select the 15th item in the combobox it would show an empty record

Any suggestions welcome :) thanks!
 
i bet there is an easier way but feel free to use this code I wrote for a program of mine. Twist and turn it as much as you need.

VB.NET:
[COLOR=#0000ff]Private[/COLOR] [COLOR=#0000ff]Sub[/COLOR] RemoveIndex ([COLOR=#0000ff]ByVal[/COLOR] _competitors [COLOR=#0000ff]As[/COLOR] competitors)
[SIZE=2][COLOR=#0000ff]    If[/COLOR][/SIZE][SIZE=2] cl_competitors [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return
[/COLOR][/SIZE][COLOR=#0000ff]    If[/COLOR] cl_competitors.Length < 1 [COLOR=#0000ff]Then[/COLOR] cl_competitors = [COLOR=#0000ff]Nothing[/COLOR] : [COLOR=#0000ff]Return
[/COLOR][COLOR=#0000ff]    Dim[/COLOR] temp_array() [COLOR=#0000ff]As[/COLOR] competitors = cl_competitors.Clone
[COLOR=#0000ff]    Dim[/COLOR] y [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]Integer[/COLOR]
[COLOR=#0000ff]
[/COLOR][COLOR=#0000ff]    For[/COLOR] [COLOR=#0000ff]Each[/COLOR] r [COLOR=#0000ff]As[/COLOR] competitors [COLOR=#0000ff]In[/COLOR] cl_competitors
[COLOR=#0000ff]         If[/COLOR] r.id = _routine.id [COLOR=#0000ff]Then
[/COLOR][COLOR=#0000ff]              Exit[/COLOR] [COLOR=#0000ff]For
[/COLOR][COLOR=#0000ff]         End[/COLOR] [COLOR=#0000ff]If[/COLOR][COLOR=#0000ff]
[/COLOR]         y += 1
[COLOR=#0000ff]    Next[/COLOR]
[COLOR=#0000ff]
[/COLOR][COLOR=#0000ff]    For[/COLOR] x [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]Integer[/COLOR] = y [COLOR=#0000ff]To[/COLOR] cl_competitors.Length - 2
          temp_array(x) = cl_competitors(x + 1)
[COLOR=#0000ff]    Next[/COLOR]
[COLOR=#0000ff]
[/COLOR]    cl_competitors = temp_array.Clone
[COLOR=#0000ff]    ReDim[/COLOR] [COLOR=#0000ff]Preserve[/COLOR] cl_competitors(cl_competitors.Length - 2)
[COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]Sub
[/COLOR]

that should work, i replaced the class names I had with your class names. Remember this though, you need to add a public variable called "ID" to the competitors class and make sure each time a new competitors is made this id changes uniquely. This works best if you make your own constructor in the class and use a shared variable to count the classes. Then assign the ID to the latest count for each class. But ID cannot be a shared variable. Put this code inside your class competitors to make the above procedure work.

VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ReadOnly[/COLOR][/SIZE][SIZE=2] ID [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Shared[/COLOR][/SIZE][SIZE=2] Counter [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]()
     competitors.Counter += 1
[/SIZE][SIZE=2][COLOR=#0000ff]    Me[/COLOR][/SIZE][SIZE=2].ID = Counter
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]


Also, since I wrote this against my code I was changing some variables adn what not. If it dosn't work and or it becomes confusing let me know and we can work on it. Anyways, I think I can now do this same procedure with half the code and much more effieciently. However; I wrote this long ago and I still use it because it works. I know more now than I did then so if it turns out bad i'll help you write some code that does work. This does indeed work though, lol, not saying it don't. But maybe your situation is a little different for some reason.
 
Thanks for the reply, however i dont quite understand this part of your example:
What is _routine? and what should i replace it with?


VB.NET:
PrivateSub RemoveIndex (ByVal _competitors As competitors)
    If cl_competitors IsNothingThenReturn
    If cl_competitors.Length < 1 Then cl_competitors = Nothing : Return
    Dim temp_array() As competitors = cl_competitors.Clone
    Dim y AsInteger

    ForEach r As competitors In cl_competitors
         If r.id = _routine.id Then
              ExitFor
         EndIf
         y += 1
    Next

    For x AsInteger = y To cl_competitors.Length - 2
          temp_array(x) = cl_competitors(x + 1)
    Next

    cl_competitors = temp_array.Clone
    ReDimPreserve cl_competitors(cl_competitors.Length - 2)
EndSub



 
oops, that was my old variable that i was using with my code. Sorry. Replace _routine with _competitors. Sorry I missed that. If it dosn't work I will build a harness program and get it working for you b/c I know I can do it but I may have placed some variables in the wrong spots. I was in a hurry to complete it for you. That above however; should work fine b/c I took it from code I was using that worked fine.
 
Better Late than Never...

I know this is really really late but I never got a response the the posts I gave you to start with. I have re-written the routine in a way that I think you can understand and munipulate. Please follow this code and remarks. Let me know if this helps.


VB.NET:
[COLOR=seagreen]'this is your array of classes[/COLOR]
 
[COLOR=blue]Dim[/COLOR] Classes(20) [COLOR=blue]as[/COLOR] Class
 
[COLOR=seagreen]'this is your individual class[/COLOR]
[COLOR=blue]Dim[/COLOR] C [COLOR=blue]as new[/COLOR] Class
 
[COLOR=seagreen]'this is your individual class being added to spot 15 of your array of classes[/COLOR]
 
Classes(15) = c
 
[COLOR=seagreen]'this is your function to remove an individual class ( _c ) from the array of classes ( C() )[/COLOR]
[COLOR=blue][/COLOR] 
[COLOR=blue]Public Function[/COLOR] Remove_Class([COLOR=blue]ByRef[/COLOR] _C [COLOR=blue]as[/COLOR] Class, [COLOR=blue]ByVal[/COLOR] C() [COLOR=blue]as[/COLOR] class) [COLOR=blue]as[/COLOR] Class()
 
      [COLOR=seagreen]'here you declare an integer to watch for the index of the class to remove[/COLOR]
     
      [COLOR=blue]Dim[/COLOR] Index2Remove [COLOR=blue]as[/COLOR] integer = -1
 
     [COLOR=blue] For[/COLOR] each T_C [COLOR=blue]as[/COLOR] Class [COLOR=blue]in[/COLOR] Class()
 
          [COLOR=seagreen] 'if we find the class then reset Index2Remove to 0[/COLOR]
[COLOR=#2e8b57][/COLOR] 
           [COLOR=blue]If[/COLOR] T_C = _c [COLOR=blue]then[/COLOR] Index2Remove = 0 : [COLOR=blue]Exit For[/COLOR]
 
       [COLOR=blue]Next[/COLOR] T_C
 
       [COLOR=seagreen]'end the function without anychanges if the class isn't found[/COLOR]
 
       [COLOR=blue]If[/COLOR] Index2Remove = -1 [COLOR=blue]then return[/COLOR] Class()
 
 [COLOR=seagreen]     'here you start a loop to look for the individual class inside of the array of classes[/COLOR]
     
      [COLOR=blue]For [/COLOR]Index2Remove [COLOR=black]=[/COLOR] 0 [COLOR=blue]to[/COLOR] C.length - 1
  
           [COLOR=seagreen]'here if the actual class found inside the array of classes is the class we want to remove[/COLOR]
[COLOR=seagreen]           'then we mark it by ending the loop and leaving the Index2Remove as the index of that class[/COLOR]
          
          [COLOR=blue] If[/COLOR] c(Index2Remove) [COLOR=blue]is[/COLOR] _c [COLOR=blue]then exit for[/COLOR]
 
      [COLOR=blue]Next[/COLOR] Index2Remove
 
[COLOR=seagreen]     'here we declare a temporary array as the length of the array of classes minus 2  b/c we will be one short[/COLOR]
      
      [COLOR=blue]Dim[/COLOR] temp_Array(c.length - 2) [COLOR=blue]as class[/COLOR]
    
    [COLOR=seagreen]  'here we declare another integer to watch for the Index we found above[/COLOR]
      
      [COLOR=blue]Dim[/COLOR] WatchForIndex [COLOR=blue]as integer =[/COLOR] 0
 
[COLOR=seagreen]      'start another loop counting through each of the indexes in the array of Classes again[/COLOR]
[COLOR=#2e8b57]      'this time we have the Index we want already and will skip it to remove it[/COLOR]
[COLOR=#2e8b57][/COLOR] 
     [COLOR=blue] For[/COLOR] x [COLOR=blue]as integer [/COLOR][COLOR=black]=[/COLOR] 0 [COLOR=blue]to[/COLOR] c.length - 1
 
          [COLOR=seagreen] 'if the current index = the index found earlier then restart the loop[/COLOR]
[COLOR=seagreen]           'NOTE: Continue For is a VB 2005 Feature, use a goto statement or another boolean if your not using VB 2005[/COLOR]
           
[COLOR=seagreen]           'NOTE: If we won't compare WatchForIndex and x to Index2Remove we will also = Index2Remove once found[/COLOR]
[COLOR=#2e8b57][/COLOR] 
           [COLOR=blue]If[/COLOR] WatchForIndex = Index2Remove [COLOR=blue]and[/COLOR] x = Index2Remove [COLOR=blue]then Continue For[/COLOR]
 
           [COLOR=seagreen]'assign the current index to the temporary array[/COLOR]
[COLOR=seagreen]           'NOTE: With Continue For we never reach this step or any following steps when the above is True, the loop simply starts again[/COLOR]
[COLOR=#2e8b57]           'NOTE:  WatchForIndex will become one less than x once Continue For is operated. This will move the following values down one index[/COLOR]
[COLOR=#2e8b57][/COLOR] 
           temp_Array(WatchForIndex) = c(x)
 
           [COLOR=seagreen]'count the next index[/COLOR]
[COLOR=seagreen]           'NOTE:  This will not implement one time out of the process b/c of the Continue For[/COLOR]
           WatchForIndex [COLOR=black]+=[/COLOR] 1

      [COLOR=blue]Next[/COLOR] x
 
      'free up the no longer needed Class
 
      _c = [COLOR=blue]Nothing[/COLOR]
[COLOR=#0000ff][/COLOR] 
[COLOR=#0000ff]  [/COLOR][COLOR=seagreen]    'Return the clone of the newly made array of Classes w/o the removed Class and the .length one less than before[/COLOR]
[COLOR=#2e8b57][/COLOR] 
      [COLOR=blue]Return[/COLOR] temp_Array.clone
 
[COLOR=blue]End Function[/COLOR]

Classes = Remove_Class(TheClass2Remove, Classes)


Hope this helps better for anyone interested. THanks
 
You're trying to find a solution to a problem that doesn't exist. Collections exist specifically so you can do this sort of thing. I'd suggest using an ArrayList or defining your own class that inherits CollectionBase and will thus act just like an ArrayList but be strongly-typed to your Competitor class.

This thread has nothing to do with Windows Forms either so I've moved it.
 
That's cool that the CollectionBase will do it for us. I never used it. But the example I have provided does work and answers his questions exactly.

I have written a very small harness for it if you would like to see it in action. It works great... Here is the code if you would like to build it yourself.

two buttons, two textboxes, and two labels
this is the class I made to test it....

VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] People
[/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] Name [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class
[/COLOR][/SIZE]
this is the code inside the form, very easy to follow...

VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] Form1
[/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] AllPeople() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] People
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] AllPeople [/SIZE][SIZE=2][COLOR=#0000ff]IsNot[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] N [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] People [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] AllPeople
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] N.Name = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TextBox1.Text [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] MsgBox([/SIZE][SIZE=2][COLOR=#800000]"do not duplicate"[/COLOR][/SIZE][SIZE=2]) : [/SIZE][SIZE=2][COLOR=#0000ff]Return
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] AllPeople [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ReDim[/COLOR][/SIZE][SIZE=2] AllPeople(0) [/SIZE][SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ReDim[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Preserve[/COLOR][/SIZE][SIZE=2] AllPeople(AllPeople.Length)
AllPeople(AllPeople.Length - 1) = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] People
AllPeople(AllPeople.Length - 1).Name = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TextBox1.Text
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TextBox1.Clear()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Label2.Text = [/SIZE][SIZE=2][COLOR=#800000]"Names from each class: "
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] N [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] People [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] AllPeople
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Label2.Text &= N.Name & [/SIZE][SIZE=2][COLOR=#800000]"; "
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Label1.Text = [/SIZE][SIZE=2][COLOR=#800000]"All classes total: "[/COLOR][/SIZE][SIZE=2] & AllPeople.Length.ToString
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TextBox1_KeyDown([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TextBox1.KeyDown
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] e.KeyCode = Keys.Return [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Button1.PerformClick()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button2_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button2.Click
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] N [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] People [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] AllPeople
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] N.Name = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TextBox2.Text [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]AllPeople = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Remove_Class(N, AllPeople) : [/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Label2.Text = [/SIZE][SIZE=2][COLOR=#800000]"Names from each class: "
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] N [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] People [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] AllPeople
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Label2.Text &= N.Name & [/SIZE][SIZE=2][COLOR=#800000]"; "
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Label1.Text = [/SIZE][SIZE=2][COLOR=#800000]"All classes total: "[/COLOR][/SIZE][SIZE=2] & AllPeople.Length.ToString
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TextBox2_KeyDown([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TextBox2.KeyDown
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] e.KeyCode = Keys.Return [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Button2.PerformClick()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] Remove_Class([/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] _C [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] People, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] C() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] People) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] People()
[/SIZE][SIZE=2][COLOR=#008000]'here you declare an integer to watch for the index of the class to remove
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Index2Remove [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = -1
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] T_C [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] People [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] C
[/SIZE][SIZE=2][COLOR=#008000]'if we find the class then reset Index2Remove to 0
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] T_C [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2] _C [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] Index2Remove = 0 : [/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] T_C
[/SIZE][SIZE=2][COLOR=#008000]'end the function without anychanges if the class isn't found
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Index2Remove = -1 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] C
[/SIZE][SIZE=2][COLOR=#008000]'here you start a loop to look for the individual class inside of the array of classes
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] Index2Remove = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] C.Length - 1
[/SIZE][SIZE=2][COLOR=#008000]'here if the actual class found inside the array of classes is the class we want to remove
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'then we mark it by ending the loop and leaving the Index2Remove as the index of that class
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] C(Index2Remove) [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2] _C [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] Index2Remove
[/SIZE][SIZE=2][COLOR=#008000]'here we declare a temporary array as the length of the array of classes minus 2 b/c we will be one short
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] temp_Array(C.Length - 2) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] People
[/SIZE][SIZE=2][COLOR=#008000]'here we declare another integer to watch for the Index we found above
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] WatchForIndex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#008000]'start another loop counting through each of the indexes in the array of Classes again
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'this time we have the Index we want already and will skip it to remove it
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] C.Length - 1
[/SIZE][SIZE=2][COLOR=#008000]'if the current index = the index found earlier then restart the loop
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'NOTE: Continue For is a VB 2005 Feature, use a goto statement or another boolean if your not using VB 2005
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'NOTE: If we won't compare WatchForIndex and x to Index2Remove we will also = Index2Remove once found
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] WatchForIndex = Index2Remove [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] x = Index2Remove [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Continue[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'assign the current index to the temporary array
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'NOTE: With Continue For we never reach this step or any following steps when the above is True, the loop simply starts again
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'NOTE: WatchForIndex will become one less than x once Continue For is operated. This will move the following values down one index
[/COLOR][/SIZE][SIZE=2]temp_Array(WatchForIndex) = C(x)
[/SIZE][SIZE=2][COLOR=#008000]'count the next index
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'NOTE: This will not implement one time out of the process b/c of the Continue For
[/COLOR][/SIZE][SIZE=2]WatchForIndex += 1
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] x
[/SIZE][SIZE=2][COLOR=#008000]'free up the no longer needed Class
[/COLOR][/SIZE][SIZE=2]_C = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Return the clone of the newly made array of Classes w/o the removed Class and the .length one less than before
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][SIZE=2](temp_Array.Clone, People())
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function
End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class
[/COLOR][/SIZE]
So I didn't spend all that time answering a problem for nothing. The answer very well exist. I am still interested in your answer as well though with the collections.

here is the attachment...
 

Attachments

  • Removing a Class from an Array of Classes.zip
    64.1 KB · Views: 13
Last edited by a moderator:
Thanks for the reply and I'll use that when I need it.

Just to let you know this was a uni assignment, and its been handed in. I got 97% so thanks for the help :)
 
if vs2003 has an ArrayList then you do this:
VB.NET:
Dim alCompetitors as New ArrayList()
Dim x as New Competitor(blah, blah, blah)
alCompetitors.Add(x)
alCompetitors.Remove(x)


Simplicity at its best :)

If oyu need to access competitors by their name or something, then do this:

VB.NET:
Dim htCompetitors as New HashTable
 
Dim x as New Competitor(blah,blah,blah)
 
'two ways to do it:
htCompetitors.Add("Fred Smith", x) 'throws exception if "Fred Smith" already there
htCompetitors("Joe Jones") = x 'overwrites object indexed by "Joe Jones" with x
 
 
'to remove:
htCompetitors.Remove("Joe Jones")
 
Last edited by a moderator:
Right on, that's a much much simpler way that I didn't know about. I have written similiar classes of my own that harness the hashtable b/c it can store any object including array of objects or other hashtables. My library works much like the ArrayList you described but with other features as well. However; it is customized for my code and isn't something that can be universal. The concept can be and I can build it to be universal for others but looks like it already has been done. Thanks again cjard.:)
 
Like I said, you create what is essentially a strongly-typed ArrayList by inheriting CollectionBase. You can similarly create a strongly-typed HashTable by inheriting DictionaryBase. Neither of these are required in .NET 2.0 because of the Collections.Generic namespace.
 
yes you did say that and you are correct. However; you also said "you are looking for a solution that doesn't exist" when in fact I had a solution. The way I proposed just offered me flexability in what I was doing so I already had the function existing. I have in fact used your exact idea in the HashTable form and built a class just for moving classes in and out of arrays from within arrays. The class that I built works essentially much faster than other options I had. The Hashtable is a really fast object for storing data and really tax free compared to other resources. I never doubted your intelligence or knowledge on the subject jmcilhinney, but I felt as though you were squashing my intelligence on the matter. If you tested my harness application you can see that I was correct in what I achieved and you can also see the possiblilities opening up when using your own code to build an entire library of this nature. I still really appreciate your input. At least sample program I provided and step through it if need be to see what I was trying to demonstrate.

With that said, I do think the answer to the original question is best answered with what you proposed.
 
Last edited:
I didn't say "looking for a solution that doesn't exist". I said "looking for a solution to a problem that doesn't exist", i.e. the problem doesn't exist, not the solution.
 
Back
Top