2014-10-16 68 views
1
Dim amountorhour As String = "A4" 

If amountorhour.Substring(0, 1) = "A" Then 
    amount = amountorhour.Substring(1, amountorhour.Length)--**error comes here** 
    HrsWorked = 0 
Else 
    HrsWorked = amountorhour.Substring(1, amountorhour.Length - 1) 
    amount = 0 

我知道這個錯誤顯示出來,當我詢問字符串的長度超出範圍但在這種情況下,我沒有看到類似的東西請幫助我弄清楚問題如何解決索引和長度必須引用字符串中的位置。參數名稱:長度「?

回答

2

第二個參數是從指數的子您指定的長度,所以通過amountorhour.Length作品只有當你傳遞0作爲將返回原始字符串的第一個參數。

看來你要取除第一個字符以外的所有字符,可以使用帶有一個參數的the overload

amount = amountorhour.Substring(1) 

這是一樣的

amount = amountorhour.Substring(1, amountorhour.Length - 1) 

順便說一句,你可以使用的

If amountorhour.StartsWith("A") Then 

代替

If amountorhour.Substring(0, 1) = "A" Then 
+0

感謝蒂姆,有一個參數重載方法的工作。 – user8811 2014-10-16 12:29:12

相關問題