0

我是自定义类的新手。这是我的类定义:

Public Class game
    Private strName As String()

    Property name As String()
        Get
            Return strName
        End Get
        Set(ByVal Value As String())
            strName = Value
        End Set
    End Property
End Class

这是我从文件中读取并创建“游戏”实例的代码

Public Sub loadGames()
    Dim game As New game

    Dim dir As New IO.DirectoryInfo(gameFolder)
    Dim fs As IO.FileInfo() = dir.GetFiles("*.gemui")
    Dim f As IO.FileInfo
    For Each f In fs
        Dim path As String = f.FullName
        Dim fi As New FileInfo(path)
        Dim sr As StreamReader = fi.OpenText()
        Dim s As String = ""
        While sr.EndOfStream = False
            game.name = sr.ReadLine() '"Value of type 'String' cannot be converted to '1-dimensional array of String'."
            MsgBox(sr.ReadLine()) 'shows a message box with exactly what I expect to see
        End While
        sr.Close()

    Next
End Sub

game.name = sr.ReadLine()是问题所在。 “‘字符串’类型的值不能转换为‘字符串的一维数组’。”

4

1 回答 1

1

您的问题是,在您的类定义中,您不是在声明字符串,而是在声明字符串数组。更正的代码是:

Public Class game
    Private strName As String

    Property name As String
        Get
            Return strName
        End Get
        Set(ByVal Value As String)
            strName = Value
        End Set
    End Property
End Class

或更简单地在 .net 4.0+ 中

Public Class game
    Property name As String
End Class

在这种情况下,私有变量称为 _name

于 2012-11-30T02:45:09.963 回答