0

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() 
2
  • What kind of array? Commented Jan 31, 2021 at 15:30
  • 2
    You should use Option Strict On because it will point out type errors like Vmin(i) = "0". Commented Jan 31, 2021 at 19:14

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @Tim Schmelter, I've tried both approaches but when I run the program and look at the array, in the local window, I find it still contains null values, jus as before.
Are you sure they're null? Maybe you need a Trim?
@DaVince294: Maybe they are not null and empty but contain spaces, then use String.IsNullOrWhiteSpace
@Tim Schmelter, I tried your new suggestion but it is still producing null values. Let me modify the question and include the relevant code section. Perhaps you may see something I'm doing wrong and can't seem to see. Please see above
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 sure would bore you. Anyway, thanks for your contributions. They are very much appreciated.
|
0

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

Comments

0
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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.