数组边界不能出现在类型说明符中

时间:2016-02-19 16:37:47

标签: arrays vb.net split filereader streamreader

嗨我正在尝试读取一个数组,但是我得到了这个错误:数组边界不能出现在类型说明符中,我希望得到数组数据来生成一个对象在线Dim user As Usuarios(第(0)行) .....

Try
    Dim filePath As String
    filePath = System.IO.Path.Combine(
               My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Clients.txt")
    Dim fileReader As System.IO.StreamReader
    fileReader = My.Computer.FileSystem.OpenTextFileReader(filePath)
    While fileReader.EndOfStream <> True
        Dim line As String = fileReader.ReadLine
        Dim lineSplit As String() = line.Split("/")
        Dim user As Usuarios(line(0))
        ComboBox1.Items.Add(line)
        MsgBox(line)
    End While
Catch ex As Exception
    MsgBox("Error, file not found Clientes.txt")
End Try

当前班级

Public Class Usuarios
Private _nombre As String
Private _erApellido As String
Private _onApellido As String
Private _Dni As String
Private _movil As Integer
Private _direccion As String
'Private _fecha As Date

'Constructor
Public Sub New(ByVal Nombre As String, ByVal erApellido As String, 
            ByVal onApellido As String, ByVal Dni As String, 
            ByVal tel As Integer, ByVal direccion As String)
    Me._nombre = Nombre
    Me._erApellido = erApellido
    Me._onApellido = onApellido
    Me._Dni = Dni
    Me._movil = tel
    Me._direccion = direccion
    'Me._fecha = fecha
End Sub

'Getters y Setters
Public Property Nombre() As String
    Get
        Return _nombre
    End Get
    Set(ByVal Value As String)
        _nombre = Value
    End Set
End Property

Public Property erApellido() As String
    Get
        Return _erApellido
    End Get
    Set(ByVal Value As String)
        _erApellido = Value
    End Set
End Property

Public Property onApellido() As String
    Get
        Return _onApellido
    End Get
    Set(ByVal Value As String)
        _onApellido = Value
    End Set
End Property

Public Property Dni() As String
    Get
        Return _Dni
    End Get
    Set(ByVal Value As String)
        _Dni = Value
    End Set
End Property

Public Property Movil() As Integer
    Get
        Return _movil
    End Get
    Set(ByVal Value As Integer)
        _movil = Value
    End Set
End Property

Public Overrides Function ToString() As String
    Return String.Format(Me._Dni + "/" + Me._nombre + "/" + Me._erApellido + "/" + Me._onApellido + "/" + String.Format(Me._movil))
End Function

End Class

当前.txt格式名称/姓氏/ .... 我的目标是,拆分.txt中包含的信息,并将用户对象保存在arraylist上。

1 个答案:

答案 0 :(得分:1)

首先,您应该了解Scope in Visual Basic其中您声明一个变量确定其范围 - 它存在的位置。所以在你的代码中:

Try
   ...
    While fileReader.EndOfStream <> True

        Dim lineSplit As String() = line.Split("/")
        Dim user As Usuarios(line(0))

    End While
Catch ex As Exception

End Try

Dimuser块中声明While,因此当它结束时,user不再存在。在顶部,Try / Catch外面声明它:

Dim user As Usuarios

然后,正如所写的那样,你只能创建一个新的Usuarios传递那些我认为来自文件的数据。所以你需要在创建它时传递它们:

Dim data = line.Split("/"c)
user = New Usuarios(data(0), data(1), data(2), data(3),
                      Convert.ToInt32(data(4)), data(5))

Dim声明变量及其类型。 New创建一个实例。

您的代码应检查分割是否会导致所有数据元素的预期。顺便说一下,如果tel As Integer表示电话号码,那么它们实际上不是数字/整数。那就像字符串一样好。

帮自己一个忙,打开Option Strict它会让编译器告诉你错误。

This post可以帮助您了解范围。

如果您使用的是Visual Studio 2010或更高版本,则不需要所有属性的样板代码。您所需要的只是:

Public Property erApellido As String

ByVal不再需要,因为它现在是默认值。