2013-02-15 88 views
1

我已經繼承了一個ASP項目,並且我是一個PHP編碼器,沒有ASP知識。很抱歉,如果這篇文章冗長,我只想盡可能多地提供信息。ASP數組元素爲空,並將字符串轉換爲長

我正在努力處理這一塊代碼。

Dim resultArray As String() 
For Each resultitem In resultArray 
    ' Do something with each element of the array 
    hash.Add(dllFunctionObj.ReturnTemplateField(i), resultitem) 
    i = i + 1 
Next 

錯誤:

Exception!!: Index was out of range. Must be non-negative and less than the size of the collection. 
Parameter name: index 

解決辦法似乎很簡單。檢查resultitem是否爲空然後中斷或跳到下一個元素。

所以,我想這一點:

If IsNull(resultitem) Then 
    Break 
End If 

錯誤:

BC30451: Name 'IsNull' is not declared. 

我想我在網上找到其他幾個備選方案:

  • IsEmpty(resultitem) - 爲IsEmpty沒有宣佈
  • String.IsNullOrEmpty(resultitem) - 索引超出範圍錯誤,似乎沒有任何效果
  • resultitem Is Nothing - 索引越界
  • Not (Len(resultitem) > 0)的 - 索引越界
  • Len(resultitem) = 0 - 出界

似乎接近的唯一的事情就是指數:

If Not resultitem Then 
    Break 
End If 

錯誤:

Exception!!: Conversion from string "some_string_here" to type 'Long' is not valid. 

如果我使用的Next代替Break我得到這個錯誤:

If Not resultitem Then 
    Next 
End If 

錯誤:

BC30081: 'If' must end with a matching 'End If'. 

幫助!

我要包括的代碼全部塊的情況下,它是有幫助的

Dim isResultArray As Boolean 
isResultArray = methodInf.ReturnType.IsArray() 
If isResultArray Then 
    Dim resultArray As String() 
    '*** Invoke the dll function 
    resultArray = methodInf.Invoke(REMem, args.ToArray) 
    Dim i As Integer = 0 
    For Each resultitem In resultArray 

     If Not resultitem Then 
      Response.Write("Found null value.") 
      Exit For 
     End If 

     Response.Write("i: " & i & "<br />") 

     hash.Add(dllFunctionObj.ReturnTemplateField(i), resultitem) 
     i = i + 1 
     ' i = 6 will cause Get Constituent Name to work 
     'If i = 6 Then 
     ' Exit For 
     'End If 
    Next 
    outputArray.Add(hash) 
Else 
    '*** could be boolean, string, long etc. 
    Dim result As String 
    '*** Invoke the dll function 
    result = methodInf.Invoke(REMem, args.ToArray) 
    hash.Add(dllFunctionObj.ReturnTemplateField(0), result) 
    outputArray.Add(hash) 
End If 
+2

在我看來,當試圖從ReturnTemplateField(i)獲取一個值爲null時,「索引超出範圍」,這不是因爲resultitem爲空 – Constanta 2013-02-15 16:51:47

+0

+1包括您自己嘗試的值(甚至是如果這是錯誤的)。大多數時候,當我看到類似這樣的帖子時,他們沒有表現出OP的這麼多努力。 – jadarnel27 2013-02-15 17:12:37

+0

我認爲你需要調查什麼ReturnTemplateField(共享一些更多的代碼?),並找出你想要做什麼與散列如果ReturnTemplateField(i)爲null。你總是可以做一些像If Not IsNothing(dllFunctionObj.ReturnTemplateField(i))然後hash.Add(dllFunctionObj.ReturnTemplateField(i),resultitem)End If但你可能需要知道爲什麼你的ReturnTemplateField中沒有任何東西(i) – Constanta 2013-02-15 17:57:25

回答

0

你需要確保你沒有溢出你正在訪問的集合的索引。像這樣的東西應該工作:

If i < dllFunctionObj.ReturnTemplateField.Length Then 

    hash.Add(dllFunctionObj.ReturnTemplateField(i), resultitem) 

End If 

基本上,你需要你的電話換到「hash.Add」在這個if塊,以防止訪問一個不存在的價值。

當然,我不明白代碼的目的,因此您可能需要處理「其他」情況。但是這應該會阻止錯誤。

+0

感謝您的幫助。原來這是一個arrayList,所以我不得不使用.Count而不是.Length。 ASP,爲什麼你必須讓事情變得如此複雜?經過這個過程之後,我確定我從不想成爲一名ASP程序員。感謝您的快速回復! – Brad 2013-02-15 20:39:38

+0

@ user1748204很高興我可以幫忙=)如果它有幫助,我對PHP很哈哈。 – jadarnel27 2013-02-15 20:53:06

0

我看來像數組可能被錯誤地定義。

更改此:

Dim resultArray As String() 
For Each resultitem In resultArray 
    ' Do something with each element of the array 
    hash.Add(dllFunctionObj.ReturnTemplateField(i), resultitem) 
    i = i + 1 
Next 

向該:

Dim resultArray() As String 
For Each resultitem In resultArray() 
    ' Do something with each element of the array 
    hash.Add(dllFunctionObj.ReturnTemplateField(i), resultitem) 
    i = i + 1 
Next 

此外,可能需要定義陣列,例如長度:

Dim resultArray(0 to 9) As String 

如果數組的長度是未知的,你可以定義它沒有最小和最大的指數,但將需要使用ReDim至以後分配的最小和最大指標。一種方法是創建沒有長度的數組,計算您知道要添加到數組的項目,然後使用ReDim作爲該數組的長度。例如:

Dim resultArray() As String 
Dim iCount as integer 'Keep track of the count with this variable. 

'Enter custom counting script here...Use a loop to count the items that will be used in the array. 

ReDim resultArray(0 to iCount - 1) 'Subtract one from the iCount to match the 0-based index. 

如果你試圖在一個沒有設定長度數組的索引來調用(或使用指數大於/小於數組的大小),你最有可能遇到的任何一個「索引超出範圍「或」子串超出範圍「錯誤。

+0

我可能是瘋了(我知道是),但我不認爲它是拋出錯誤的結果數組(因爲OP沒有通過索引訪問它的元素,而是使用foreach循環)。他們試圖通過「i」變量訪問該TemplateField集合的索引,這更有可能拋出錯誤。 – jadarnel27 2013-02-15 17:35:09

+0

感謝您的回覆。不幸的是,我不知道陣列的長度。我嘗試了第一個解決方案的變體,並且解決了問題。感謝您花費的時間。 – Brad 2013-02-15 20:37:36

+0

不客氣。無論如何,jadarnel27可能都是對的。 – Lopsided 2013-02-15 20:41:31

相關問題