Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact
A1VBCode Forums
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      

Home » Classic Visual Basic (VB 6 or earlier) » General Visual Basic » Removing element from Array


Removing element from ArrayExpand / Collapse
Author
Message
Posted 8/17/2006 2:11:04 AM


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 3/12/2011 8:33:31 PM
Posts: 276, Visits: 215
Is it possible to remove an element from the middle of an array?

Example:

myArr(0)="Forum"
myArr(1)="Moderator"
myArr(2)="Visual Basic"
myArr(3)="A1VBCode"
myArr(4)="Add new Post"

I want to remove the elment 1, so that the array automatically redims to...

myArr(0)="Forum"
myArr(1)="Visual Basic"
myArr(2)="A1VBCode"
myArr(3)="Add new Post"


___________________________________
You can see my projects in my homepage!

Post #19786
Posted 8/17/2006 3:21:14 AM


Junior Member

Junior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior Member

Group: Forum Members
Last Login: 9/22/2006 1:27:36 AM
Posts: 13, Visits: 31
You can't do that automatically. You would need to copy the data that you want to keep to a new array

Genius can not be scheduled!!!
Remember A clear answer requires a clear question!!!
Post #19789
Posted 8/17/2006 1:19:40 PM


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 3/12/2011 8:33:31 PM
Posts: 276, Visits: 215
Thanks for confirming it.

 

A clear question requires a clear mind.


___________________________________
You can see my projects in my homepage!

Post #19801
Posted 8/17/2006 4:12:00 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: 6/24/2009 3:34:33 AM
Posts: 511, Visits: 265
Here's an example of removing an array item while preserving the order:

http://www.garybeene.com/code/visual%20basic65.htm

But it does have to re-iterate through the entire array which may take awhile depending on the size of the array.

You could also use the Collection data type, but it's a little slower and takes up a little more memory.
Post #19808
Posted 8/21/2006 12:52:38 PM


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 3/12/2011 8:33:31 PM
Posts: 276, Visits: 215
Thanks for the link. It has two very simple logics. Now I have to decide which is the best one to use, including using another array.


___________________________________
You can see my projects in my homepage!
Post #19862
Posted 2/4/2009 9:09:51 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 2/4/2009 8:59:48 AM
Posts: 1, Visits: 1
Here is a three line code for the process...

Public Function RemoveElementFromArray(ByVal objArray As System.Array, ByVal objElement As Object, ByVal objType As System.Type)
Dim objArrayList As New ArrayList(objArray)
objArrayList.Remove(objElement)
Return objArrayList.ToArray(objType)
End Function

the function would be called like this:
For e.g. if there is a String array name strarray then and you need to remove an element "Hello" from the array then..

RemoveElementFromArray(strarray,"Hello",Gettype(String))

Here the last parameter passes Gettype for String because the array is of String type...
If in case the array is of type TextBox then the last parameter would be Gettype(TextBox).
Post #26928
Posted 2/4/2009 9:59:34 AM
Forum God

Forum God

Group: Moderators
Last Login: Today @ 8:08:40 AM
Posts: 1,107, Visits: 10,690
Welcome!

But you are posting a .NET solution to a two and a half year old VB6 thread. You may want to check out this forum instead http://www.a1vbcode.com/vbforums/Forum9-1.aspx

Post #26929
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 1 (1 guest, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Admin, Mod, Mark, Keithuk

PermissionsExpand / Collapse

All times are GMT -5:00, Time now is 10:31am