使用数组VB .net

时间:2015-07-17 17:54:39

标签: arrays vb.net list

我正在研究一个基于拖放来解析文件名的程序,并处理并删除可能在文件中找到的单词列表。我让程序工作,但是不是那么优雅,以及稍后会在程序中引入问题的方式。

我正在尝试创建自定义类或结构的列表。

以下是我所拥有的:

Public Class moviePath
   Public currentPath As String
   Public currentNameExt As String
   Public currentName As String
   Public currentExt As String
   Public correctedName As String
   Public correctYear As String
End Class

当我尝试基于此创建可用变量时,问题是:

Dim workingList as New List(Of moviePath)

我离开了没有好办法使用这样的子句正确地将数据添加到列表中:

Sub scanParent(ByVal sDir As String)
    Dim f As String
    Dim i As Integer = 0
    Try

        For Each f In Directory.GetFiles(sDir)

            workingList(i).currentName = (Path.GetFileNameWithoutExtension(f))
            workingList(i).currentNameExt = (Path.GetFileName(f))
            workingList(i).currentPath = (Path.GetFullPath(f))

            i += 1
        Next
    Catch excpt As System.Exception
        MessageBox.Show("It didn't work " & excpt.ToString)

    End Try

End Sub

希望这些花絮有意义。 scanParent sub被称为将路径传递给文件夹作为参数(即C \ somefolder),我打算使用有关文件和文件夹的信息填充一系列排序。我的理由是,我希望能够删除电影标题中的单词,以便使用解析IMDB来获取电影信息的开源库(不确定是否正确的语言)。

我需要的主要功能是使类moviePath中的每个项都可以寻址并绑定到列表中该位置的每个其他项。

编辑:即。 moviePath(0).currentPath将引用同一个文件moviePath(0).currentName

3 个答案:

答案 0 :(得分:1)

For Each f In Directory.GetFiles(sDir)
    dim info as new moviePath

    info.currentName = (Path.GetFileNameWithoutExtension(f))
    info.currentNameExt = (Path.GetFileName(f))
    info.currentPath = (Path.GetFullPath(f))

    workingList.add(info)
Next

答案 1 :(得分:0)

您应该使用List(T).Add方法。

Sub scanParent(ByVal sDir As String)
    Dim f As String
    Try

        For Each f In Directory.GetFiles(sDir)
            Dim newPath As New moviePath()

            newPath.currentName = (Path.GetFileNameWithoutExtension(f))
            newPath.currentNameExt = (Path.GetFileName(f))
            newPath.currentPath = (Path.GetFullPath(f))

            workingList.Add(newPath)
        Next
    Catch excpt As System.Exception
        MessageBox.Show("It didn't work " & excpt.ToString)

    End Try
End Sub

答案 2 :(得分:0)

我同意Plutonix。除了Bradley的修复之外,还要将FileName传递给你的movePath类,如下所示:

Public Class moviePath

    Public Sub New(ByVal FullFilePath As String)
        Me.currentPath = FullFilePath
        Me.currentName = Path.GetFileNameWithoutExtension(FullFilePath)
        Me.currentNameExt = Path.GetFileName(FullFilePath)
    End Sub

    Public currentPath As String
    Public currentNameExt As String
    Public currentName As String
    Public currentExt As String
    Public correctedName As String
    Public correctYear As String

End Class

现在你的循环变得简单了:

For Each f In Directory.GetFiles(sDir)
    workingList.add(New moviePath(f))
Next