DaVince294
DaVince294

Reputation: 39

How do I remove all null or empty elements from my array in vb.net?

The code block computes two values for speeds; Vsf and Vro using their corresponding parameter values: Angle, Super-elevation and Radius for each iteration of the for- loop statement. During each loop, it selects the minimum of both speed values. In some scenario's, Angle, super-elevation but most of all, Radius are all null values, leading to Vsf and Vro values of null and hence Vmin of null. I want to eliminate these scenarios and produce just non-zero values for Vmin hence my question.

         For i = 1 To CInt(txtNumSections.Text)
                ReDim Preserve Vsf(i)
                ReDim Preserve Vro(i)
                ReDim Preserve Vmin(i)

                Vsf(i) = (((0.91544 - 0.00166 * Angle(i) - 0.000002 * W - 0.054248 * Superelevation(i) - Sidefrictionfactor) / 0.013939) * Radius(i)) ^ 0.5
                Vro(i) = (((1.05653 - 0.004861 * Angle(i) - 0.000004 * W - 0.314653 * Superelevation(i) - rolloverthreshold) / 0.012729) * Radius(i)) ^ 0.5
                Vmin(i) = Math.Min(Vsf(i), Vro(i))
                If Vmin(i) <= "0" Then
                    Vmin(i) = "0"
                End If
            Next

            Dim myList = New List(Of Double)
            For Each s In Vmin
                If Not String.IsNullOrWhiteSpace(s) Then
                    myList.Add(s)
                End If
            Next
            Vmin = myList.ToArray()

Upvotes: 0

Views: 2136

Answers (3)

YaoChen Aan
YaoChen Aan

Reputation: 19

Not the subtlest:
'declare source array
        Dim MyArray1() As String
'declare target array
        Dim MyArray2() As String
'number of items
        Dim MyCount As Integer
'index on target array
        Dim MyIndex As Integer = 0
'numb of empty items
        Dim MyBlanks As Integer = 0
'each item named
Dim MyS as String

'for example populate your source array from a file
Dim MyPath as String = "C:\Users\JohnDoe\Documents\Text.txt"
MyArray1= System.IO.File.ReadAllLines(MyPath)
'get source nb of items
        MyCount = MyArray1.Count
'resize target array to that 'maximum value'
        ReDim Preserve MyArray2(MyCount)
'go thru the source array
        For i = 0 To MyCount - 1
            MyS = MyArray1(i)
'If non-empty
            If MyS <> "" Then
'set that non-empty into target at next index position
                MyArray2(MyIndex) = MyS
'increment MyIndex for target array when a non-empty is set in target
            MyIndex += 1
' taking 'i' as the value would increment too fast leaving the blanks
'How many blanks shall I get?
            Else MyBlanks += 1
            End If
        Next
'New size of target array is source's minus the blanks
        MyCount = MyCount - MyBlanks
'resize target array to the new smaller size
        ReDim Preserve MyArray2(MyCount)

Upvotes: 0

DaVince294
DaVince294

Reputation: 39

Alright guys, I found a workaround this, based on the purpose of the code piece. I simply set all null or negative values to the maximum allowable values. There's some context to it which makes it a sensible approach but I would need to explain the entire operation of the code block which I'm not sure you would have any interest in. Anyway, thanks for your contributions. They are very much appreciated

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460098

Since arrays are immutable(you can't add or remove items) you can just re-create it:

myArray = myArray.Where(Function(s) Not String.IsNullOrEmpty(s)).ToArray()

This is a String() but this LINQ query works similar with any other type of array.

A non-LINQ appproach would be to fill a List(of T) and then use ToArray:

Dim myList = New List(Of String)
For Each s In myArray
    If Not String.IsNullOrEmpty(s)
        myList.Add(s)
    End If
Next
myArray = myList.ToArray()

You see that LINQ can make your code more readable and understandable.

Upvotes: 2

Related Questions