开发者

VB 2008 - Index was outside the bounds of the array

Hey guys I'm having a problem while reading a config.cfg file of my program. I can read the 23. char of the file but I can't read the 24. char (last char in file).

This is the code:

Dim CFGReader2 As System.IO.StreamReader
CFGReader2 = _
My.Computer.FileSystem.OpenTextFileReader(CurDir() & "\Config.cfg")
Dim Server(2) As String
Server(0) = CFGReader2.ReadToEnd.Chars(23)//This part works

    If Server(0) = 0 Then
        Server(1) = CFGReader2.ReadToEnd.Chars(24)//This part results in "Index was outside the bounds of the array".
    ElseIf Server(0) = 1 Then
        Server(2) = CFGReader2.ReadToEnd.Chars(24)//This part results in "Index was outside the bounds of the array".
        Server(1) = 10 + Server(2)
    ElseIf Server(开发者_开发知识库0) = 2 Then
        Server(2) = CFGReader2.ReadToEnd.Chars(24)//This part results in "Index was outside the bounds of the array".
        Server(1) = 20 + Server(2)
    ElseIf Server(0) = 3 Then
        Server(2) = CFGReader2.ReadToEnd.Chars(24)//This part results in "Index was outside the bounds of the array".
        Server(1) = 30 + Server(2)
    End If

And this is the file:

Language = 2

Server = 11

Thanks for the answer!

Frosty


Arrays are idexed from 0 onwards. Thus 24 characters would be array index 0 to array index 23.

edit

Let me explain.

System.IO.StreamReader.ReadToEnd returns a String type as you know.

String.Chars() allows you to access the sring as an array, with an index base of 0.

Thus in your code:

CfgReader.ReadToEnd.Chars(23) works becuase it accessing the last character, that is the 24th character in the string.

CfgReader.ReadToEnd.Chars(24) doesn't work because it trying to access the 25th character in the string , which doesn't exist.

For example:

If a string contains the following characters: "abcdef" it has a length of 6, because it contains 6 characters, but 'a' is at position 0, b is at position 1. So

Dim testString As String
Set testString = "abcdef"

Dim testChar As Char

testChar = testString.Chars(0) // testChar = a
testChar = testString.Chars(5) // testChar = f
testChar = testString.Chars(6) // will throw an exception as we are accessing a position beyond the end of the string.

I hope that explains it. I'll apologise if my VB syntax is out as its not a language that I use very often.


Here is how I solved it for those who might wonder the same:

Dim CFGReader(2) As System.IO.StreamReader

CFGReader(0) = My.Computer.FileSystem.OpenTextFileReader(CurDir() & "\Config.cfg")

Dim Server(2) As String

Server(0) = CFGReader(0).ReadToEnd.Chars(24)

CFGReader(1) = My.Computer.FileSystem.OpenTextFileReader(CurDir() & "\Config.cfg")

Server(1) = CFGReader(1).ReadToEnd.Chars(23)

Server(2) = Server(1) + Server(0)

MsgBox(Server(2)) // This is just to test it.

It works perfectly for now. I'll tell you the result after a day or so.

P.S. For every char you want to read from file you have to make a new reader.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜