2016-02-19 68 views
1

嗨,我想读取一个数组,但我得到这个错误:数组边界不能出现在类型说明符中,我想获取数组数据以使对象在行Dim用户作为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上并使用户对象保存在数组列表中。

+0

parens表明你希望'Usarios'是一个数组,因为'line'是一个字符串,它没有多大意义:'Dim user As Usuarios(“Bob/Jones”)'唯一的数组有'lineSplit '。是'Usuarios'类型(类)? – Plutonix

+0

我想得到分裂的信息,使用户...唯一的办法是暗arr作为字符串= {“”.....} – Icaro

+0

好吧,我会猜测然后....你*可能*想要: Dim user as New Usuarios(lineSplit(0))''。 'usr'将只存在于该块中 - 对象只存在于声明它们的地方。你也应该打开选项严格 – Plutonix

回答

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 

Dim声明userWhile块里面,所以当它结束时,将user不复存在。在顶部,try/catch语句外声明它:

Dim user As Usuarios 

然后,写的,你只能创建一个新的Usuarios通过这5个数据,我想来自文件。所以你需要通过他们,当你创建一个:

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不再需要,因为它是默认了。

+1

呵呵。爱的选择玩具为小孩!确定删除我的上述评论,因为它可能会被误解。 –