检查数组元素是否实际保存数据时出错

时间:2013-03-02 23:12:11

标签: arrays vb.net

我有一个简短的应用程序,检查我的音乐文件是否是特定例程的名称(曲目编号,然后是曲目名称),但是每当没有需要重命名的文件时我就会收到错误,因为初始化的数组,但第一项是空的,空,空(但VB指的是它)。

要尝试修复此问题,我正在运行此检查,但我仍然收到错误。

    ' 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

这是我得到的错误 -

  

争论不可能是什么。参数名称:file

有人能告诉我进行这项检查的正确方法吗?

2 个答案:

答案 0 :(得分:2)

我发现答案是检查数组中的第一个元素,而不是数组本身。因此,改变这个......

    If Not nc_full_names Is Nothing Then

......对此...

    If Not nc_full_names(i) Is Nothing Then

......工作正常。

答案 1 :(得分:0)

你也可以从一个真正的空数组开始:

Dim nc_full_names(-1) As String

现在nc_full_names.Length = 0