[Help] How to check if the array is not "Empty"?

Hi, I am new to VB.Net. I have a problem which confuses me for a while. I create several arrays in the program, and let the user to input data into the arrays. Then I try to determine if the user really input the data, so that the following calculation won't be wrong. My question is, how can I know the user really input the data (even input the zero), not the default empty value "0"

For example, I create a Single type array.

Dim test() as single
...
'check if user input the data
If test(0) Is Nothing then
...
End If

After debugging, the error message pop up saing:
"Is" requires operands that have reference types, but this operand has the value type of 'Single'.
It looks like I can not use "Is Nothing" command in the numerical arrays. I notice that even the array has no input, the length of the array is still 1, and the first value, test(0), is zero by default. How can I do to check if the array is not empty and not the default "0" value I want to make sure the user doesn't forget to input data.

Any comment is welcome, and thank you very much for your help.

My OS: Win XP SP2, Visual Basic .Net 2003



Answer this question

[Help] How to check if the array is not "Empty"?

  • CrCr

    Firstly, arrays are 0-based so these are valid:

    Dim test(0) As Single ' 1 position
    test(0) = 100.1
    Dim test2(10) As Single
    For i As Integer = 0 To 10 ' 11 positions!
    test2(i) = i * 0.5
    Next


    To test if the array has been assigned, and not just declared:

    If test Is Nothing then Messagebox.Show("eek!")

    try it with:

    Dim test() As Single ' just declared
    Dim test2() As Single = New Single() {} ' declared and assigned to an Empty array

    test IS nothing, it has not been assigned. (object reference not set to an instance of an object...)
    test2 ISNOT nothing, it is assigned to an empty array. test2.Length is 0



  • [Help] How to check if the array is not "Empty"?