当前位置: 动力学知识库 > 问答 > 编程问答 >

vb.net - Error when checking if an array element actually hold data

问题描述:

I have a short app that check if my music files are names to a specific routine (track number and then track name), but I'm getting an error whenever there are no files that need renaming, because the array in initialised, but the first item is nothing, null, empty (however VB refers to it).

To try and fix this, I'm running this check, but I'm still getting an error.

 ' Array declared like this

Dim nc_full_names(0) As String

<Code goes here to ReDim 'nc_full_names' and add the file name to the array, if a file needs renaming>

For i = 0 To UBound(nc_full_names)

'Checking if the array element actually has something in it like this

If Not nc_full_names Is Nothing Then

My.Computer.FileSystem.RenameFile(nc_full_names(i), nc_new_names(i))

Else

Exit For

End If

Next i

Here is the error that I am getting -

Argument cannont be nothing. Parameter name: file

Can anyone tell me the correct way to carry out this check?

网友答案:

I found that the answer was to check the first element in the array, as opposed the array itself. Thus, changing this...

    If Not nc_full_names Is Nothing Then

...to this...

    If Not nc_full_names(i) Is Nothing Then

...works just fine.

网友答案:

You can also start with a truly empty array:

Dim nc_full_names(-1) As String

Now nc_full_names.Length = 0.

分享给朋友:
您可能感兴趣的文章:
随机阅读: