1

Hi i am trying to read an array but i get this error: The array bounds can not appear in type specifiers, i want to get the array data to make an object at line Dim user As Usuarios(line(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

Current class

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

Current .txt format name/surname/.... my objective, split the information contained on .txt and make an user object saved on arraylist.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Icaro
  • 11
  • 3

1 Answers1

1

First, you should learn about Scope in Visual Basic. Where you declare a variable determines its scope - where it exists. So in your code:

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 declares user inside the While blocks, so when it ends, the user ceases to exist. At the top, outside the Try/Catch declare it:

Dim user As Usuarios

Then, as written, you can only create a new Usuarios passing those 5 pieces of data which I guess comes from the file. So you need to pass them when you create one:

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

Dim declares the variable and its type. New creates an instance.

Your code should check if the split resulting in all the data elements expected. By the way, if tel As Integer means telephone number, they really are not numbers/integer. That would be better as string.

And do yourself a favor and turn on Option Strict it will let the compiler tell you about errors.

This post may help you understand Scope.


If you are using Visual Studio 2010 or later, you dont need all that boilerplate code for properties. All you need is:

Public Property erApellido As String

ByVal is no longer needed as it is the default now.

Community
  • 1
  • 1
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178