How to remove an array item on a class

Hi everyone, how are you?

I have the following structure:
class: c_sala
class: c_movel

inside c_sala, I have a property

Public Property moveis() As c_movel

and inside c_sala a function (just a small part):

For Each movel As c_movel In moveis
if movel.xyz=true then
// how do I remove this movel moveis()?
end if
Next

i know i can do

For i As Integer = moveis DownTo 0
Var movel As c_movel = moveis(i)
and then…
moveis.removeat(i)

but i want the other way should be faster…

No, you’re already really close to the most efficient way to do it:

For i As Integer = moveis DownTo 0
    if moveis(i).xyz=true then
        moveis.Removeat(i)
    end
next
1 Like

You cannot do this with for each at all.

Assuming there’s only 1 item to be removed you can add an exit. This will skip the rest of the loop when the first item is found and removed.

For i As Integer = moveis.LastIndex DownTo 0
    If moveis(i).xyz=true Then
        moveis.Removeat(i)
        Exit For i // If there's more than one to remove then delete this line
    End If
Next
1 Like

if you need the Object index indexof should help

IndexOf typically works with arrays of intrinsic types (such as Single, Double, String etc). The “moveis(i).xyz” would suggest that this is an array of class instances. IndexOf would likely be able to search for the index of the instance of a class as a whole (ie which element of the array holds a reference to this class instance), it cannot search for one where a specified property is of a given value.

sure i meant without the use of property

Var list() As Class1

Var a As New Class1
Var b As New Class1

list.Add(a)
list.Add(b)

System.DebugLog list.IndexOf(a).ToString
System.DebugLog list.IndexOf(b).ToString

Agreed, but the original request was for a property search.