2010-04-30 33 views
0

嘿傢伙我在閱讀我的程序的config.cfg文件時遇到問題。我可以讀取該文件的23.字符,但我無法讀取24. char(文件中的最後一個字符)。VB 2008 - 索引超出了陣列的範圍

這是代碼:

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 

這是文件:

語言= 2

服務器= 11

感謝您的回答!

Frosty

回答

3

數組從0開始編號。因此24個字符。將數組索引0到數組索引23.

編輯

讓我解釋。

System.IO.StreamReader.ReadToEnd如您所知返回字符串類型。

String.Chars()允許您訪問SRING作爲一個數組,0

因此,在你的代碼的索引基礎:

CfgReader.ReadToEnd.Chars(23)作品,原因是其訪問的最後一個字符,也就是第24個字符字符串。

CfgReader.ReadToEnd.Chars(24)不起作用,因爲它試圖訪問字符串中不存在的第25個字符。

例如:

如果字符串包含下列字符:「ABCDEF」,它具有6的長度,因爲它包含6個字符,但「a」是在位置0,b爲1位。 所以

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. 

我希望能解釋它。 我會很抱歉,如果我的VB語法是因爲它不是我經常使用的語言。

+0

你能修復代碼,所以它會工作,因爲我認爲我不明白它? 謝謝。 – Jan 2010-04-30 10:15:24

+0

那裏我沒有糾正你的代碼,但已經告訴你爲什麼它不是正確的。 – ChrisBD 2010-04-30 10:46:26

+0

感謝您的答案,但它似乎是錯誤的。當我測試什麼是23. char它說它是1(在txt文件中,我將「Server = 11」更改爲「Server = 10」)。但是當我聽你的時候,22.char是SPACE。所以錯誤是「從字符串轉換」到「雙精度型」無效。「但是,當我想要獲得24個字符時,它仍然是同樣的舊錯誤。 幫助並感謝您的回答。 哦,還有一件事:如果我在這兩種情況下使用23. char(之前和之後),第一個是好的,但第二個給我錯誤。 – Jan 2010-04-30 11:04:55

0

這是我如何解決它爲那些誰可能會問相同的:

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. 

它完全適用於現在。我會在一天左右後告訴你結果。

P.S.對於你想從文件中讀取的每一個字符,你必須創建一個新的讀者。

+0

儘管如此,那麼最好將文件首次讀取的結果存儲在變量中。類似於'Dim fileContents as String = IO.File.ReadAllText(CurDir()&「\ Config.cfg」)'然後你可以隨時訪問'fileContents.Chars(23)'和'fileContents.Chars(24)' 。另外,如果您試圖以整數形式獲取服務器編號,則應該逐行讀取文件,並使用「String.Split」和「Integer.Parse」而不是逐字符方法你在這裏使用。 – 2010-04-30 13:01:26

+0

是的 - 不要忘記,當使用StreamReader時,它將光標(要讀取的位置)移動到停止讀取的位置的末尾。因此,爲什麼要創建兩個StreamReader,因爲它會重新設置每次從讀取點開始的位置。 – ChrisBD 2010-05-04 07:04:58