Reputation: 331
I was wondering if there was anyway in Visual Basic to check the CURRENT length of an array that has been declared and initialized with a fixed number but may or may not have any data stored in in yet. e.g
Dim arrayStudent As String(3) = {}
is an array with 3 indexes but no current data, and if I used arrayStudent.length
, then that is going to be "3" no matter what.
I'm trying to set up an if statement that will let the input textbox into my for-loop if the current length is less than 3.
Upvotes: 0
Views: 65
Reputation: 1500903
There's no such concept as "current length". It has 3 elements from the start. All of them have a value of Nothing
to start with, but the length is still 3.
If you're trying to count how many non-Nothing elements are in the array, you could use LINQ:
Dim count = arrayStudent.Count(Function(x) x IsNot Nothing)
But frankly you'd be better using a List(Of String)
instead...
Note that as far as I can tell, your variable declaration is invalid to start with - but this works:
Public Class Test
Public Shared Sub Main()
Dim arrayStudent(3) As String
Dim count = arrayStudent.Count(Function(x) x IsNot Nothing)
Console.WriteLine(count)
arrayStudent(1) = "Fred"
count = arrayStudent.Count(Function(x) x IsNot Nothing)
Console.WriteLine(count)
End Sub
End Class
Upvotes: 4