Variable outside matrix boundaries

Asked

Viewed 109 times

2

I’m trying to put the items in positions of a matrix, but I keep getting the error that the variable is outside the matrix limits. I don’t know how to fix the mistake and I need help.

Follows the code:

Dim posicao As String = String.Join("", itens)
Dim separa As String()
Dim contagem As String

contagem = ListBox1.Items.Count

Dim i As Integer
For i = 0 To contagem
separa = Split(posicao, ";")
cpf = separa(0)
nome = separa(1) 'O erro ocorre aqui.
anonasc = separa(2)
Next

TextBox1.Items.Add(cpf & ";" & nome & ";" & anonasc)

The program does not point out code errors. I am analyzing the variable items, which receives lines from a file . txt.

The local_file variable takes the . Filename of an Openfiledialog.

Variable itens:

Dim local_arquivo As String
itens = (My.Computer.FileSystem.ReadAllText(local_arquivo, System.Text.Encoding.Default)).Replace(vbLf, String.Empty).Split((CChar(vbCr)))
ListBox1.Items.AddRange(itens)
  • It would be good to put what is the error and where it occurs, what is the data being analyzed (itens and ListBox1.Items), etc..

  • All right, I added.

  • But what is the content of itens?

  • Corrected above.

  • I’m talking about the content, not its definition. I’m going to try to respond the way it does.

  • The content will depend on what is in the file that the user selects.

  • Yes, and this is what I want to know to see why the error occurs.

  • There are three Cpfs, followed by names and year of birth. They are just examples. They are separated by the character ;. 000.000.000-00;Maria da Silva;1995 111.111-11;José de Souza;2005 222.222.222-22;João Antônio;1945

  • They are separated by a ENTER, which did not appear when posting the comment.

  • Did you notice that the syntax of Split is wrong? You saw in my answer the code working?

  • Did any of the answers solve your problem? Do you think you can accept one of them? If you haven’t already, see [tour] how to do this. You would help the community by identifying the best solution for you. You can only accept one of them, but you can vote for any question or answer you find useful on the entire site (if you have enough score).

Show 6 more comments

2 answers

2

Clearly the data being analyzed does not have the expected semicolons. This is a badly formed datum, something that can happen.

Unless it is a misinterpretation of what the code should do, the correct thing is to check before accessing the data. You have to decide what to do if the data is poorly formed, probably indicate an error. That is, you cannot simply access the separa(0), separa(1) and separa(2), before doing this has to do with the size of the array is 3. If not, you have to interrupt the algorithm and inform that the file is invalid.

Another detail to be analyzed is whether this For is serving for something. It seems to me not. Maybe it is intended to do something with it, but right now it is useless.

With editing the problem may be another. I did a test with the past data and everything works. I had to change some things, the code even compiled.

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

The answer remains valid because even if the data are not in trouble now, one day they may be.

  • I’m sorry if I’ve upset you. I know a lot about VB, but this is the first time I’ve ventured into what I need to do. I’m testing your code here, but I don’t know what’s going on that doesn’t compile, it says it can’t create form because the value can’t be null.

1

Dim posicao As String = String.Join(";", itens)
Dim separa As String() = posicao.split(;)
Dim contagem As Integer = separa.count

 cpf = separa(0)
 nome = separa(1) 
 anonasc = separa(2)

for x as integer = 0 to Contagem-1
    textbox1.text = textbox1.text & ";" & separa(x)
next

Browser other questions tagged

You are not signed in. Login or sign up in order to post.