Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete an array element - HOWTO? 1

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
568
US
Colleagues,
I vaguely remember that there was something like Array.Delete(ElementIdx) in one of the programming platforms I've used in the past.
Can't recall was it VB6 or VFP 3.0-9.0...
Probably in VFP, coz I remember giving my students in VB6 class assignment to develop a sub or function for deleting an array element... but that was in 1997-2000...
So, the Q. is: is there a method in the .NET to do just that, delete an element from an array?
Or I have to "roll er own"? (NP, not the 1st time :))

Regards,

Ilya
 
If you want to add or remove elements then do not use arrays but lists.
However, you can convert the array to the list, do the add/remove operations on the list and finally convert the list back to the array.

Example:
Code:
Imports System

Module Program
    Sub Main(args As String())

        Dim my_array As String()
        Dim my_list As List(Of String)

        my_array = {"foo", "foobar", "bar", "baz"}
        print_array(my_array, "my_array")

        ' convert array to list
        my_list = my_array.ToList
        ' remove specific value
        my_list.Remove("foobar")
        ' convert list to array
        my_array = my_list.ToArray
        print_array(my_array, "my_array")

        ' convert array to list
        my_list = my_array.ToList
        ' remove value at specific position
        my_list.RemoveAt(0)
        ' convert list to array
        my_array = my_list.ToArray
        print_array(my_array, "my_array")

        ' convert array to list
        my_list = my_array.ToList
        ' add value to end of list
        my_list.Add("spam")
        ' convert list to array
        my_array = my_list.ToArray
        print_array(my_array, "my_array")

    End Sub

    Sub print_array(str_array() As String, str_array_name As String)
        Console.WriteLine("{0}: ", str_array_name)
        For i = 0 To str_array.Length - 1
            Console.WriteLine("{0}({1}) = ""{2}""", str_array_name, i, str_array(i))
        Next
        Console.WriteLine()
    End Sub
End Module

Output:
Code:
my_array:
my_array(0) = "foo"
my_array(1) = "foobar"
my_array(2) = "bar"
my_array(3) = "baz"

my_array:
my_array(0) = "foo"
my_array(1) = "bar"
my_array(2) = "baz"

my_array:
my_array(0) = "bar"
my_array(1) = "baz"

my_array:
my_array(0) = "bar"
my_array(1) = "baz"
my_array(2) = "spam"
 
You can use LINQ methods to remove items from an array.

e.g the following results in an array from which foobar has been removed

Code:
[COLOR=blue]Dim myArray = {"foo", "foobar", "bar", "baz"}
myArray = myArray.Where(Function(word) word <> "foobar").ToArray[/color]



 
Thank you, StrongM!
Where this "Function()" comes from? Tried to find it in MS's Help - got "U R in a helicopter" results...
For that matter: where can I find comprehensive description of LINK? Searched MS Help... (see above).
BTW, I know "a thing or two" (sorry, Sir Paul! :)) about Transact SQL (30 yrs. experience with XBase)

Regards,

Ilya
 
LINQ, not LINK

Overview:

>Where this "Function()" comes from?
It's a lambda

Essentially LINQ works against enumerable object (such as arrays) and allows us to define a lambda function that then operates against each item in the enumeration. In this case the operation is simply checking if the item is equal to 'foobar' and returning true or false as a result; the LINQ Where query only returns results that are True. And then we use another LINQ capability, whioch is to convert the link result to another enumerable type, in this case an array vi ToArray
 
Interesting question, it made me look around and I found this on stackoverflow
So i tested it, and it does the trick:
Sub DelEle(Ary, SameTypeTemp, Index As Integer) '<<<<<<<<< pass only not fixed sized array (i don't know how to declare same type temp array in proceder)
Dim I As Integer, II As Integer
II = -1
If Index < LBound(Ary) And Index > UBound(Ary) Then MsgBox "Error.........."
For I = 0 To UBound(Ary)
If I <> Index Then
II = II + 1
ReDim Preserve SameTypeTemp(II)
SameTypeTemp(II) = Ary(I)
End If
Next I
ReDim Ary(UBound(SameTypeTemp))
Ary = SameTypeTemp
Erase SameTypeTemp
End Sub

Sub Test()
Dim A() As Integer, B() As Integer, I
ReDim A(3)
Debug.Print "InputData:"
For I = 0 To UBound(A)
A(I) = I
Debug.Print " " & A(I)
Next
DelEle A, B, 1
Debug.Print "Result:"
For I = 0 To UBound(A)
Debug.Print " " & A(I)
Next
End Sub
Perhaps it will help you too :)

Herman
Say no to macros
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top