tags:

views:

235

answers:

2

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

+2  A: 

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.

ChrisBD
Can you repair the code so it will work because I think I don't get it?Thanks.
Jan
There I haven't corrected your code, but have shown you why it is not quite right.
ChrisBD
Thanks for the answer but it seems wrong. When I tested what is the 23. char it says it's 1 (in txt file I changed "Server = 11" to "Server = 10" ). But when I listened to you the 22.char was SPACE. So the error was "Conversion from string " " to type 'Double' is not valid." But still when I want to get 24. char it's the same old error.Help and thanks for answer.Oh and one more thing: If I use 23. char in both cases (before and after if) the first is good but the second gives me the error.
Jan
A: 

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.

Jan
Gideon Engelberth
Yes - don't forget that when using StreamReader it moves the cursor (location to read from) to the end of where you stopped reading. Hence why creating two StreamReaders works, as it resets where the read from point starts each time.
ChrisBD