1

私はASPプロジェクトを継承しており、ASPの知識がないPHPコーダーです。この投稿が長すぎる場合は申し訳ありませんが、できるだけ多くの情報を提供したいと思います。

私はこの1ブロックのコードで苦労しています。

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

解決策は簡単なようです。nullかどうかを確認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
4

2 に答える 2

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後で最小インデックスと最大インデックスを割り当てるために使用する必要があります。1つの方法は、長さのない配列を作成し、配列に追加されることがわかっている項目を数え、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.

長さが設定されていない配列のインデックスを呼び出そうとすると(または配列のサイズよりも大きい/小さいインデックスを使用すると)、「インデックスが範囲外」または「サブストリングが範囲外」のいずれかに遭遇する可能性があります。範囲」エラー。

于 2013-02-15T17:20:13.617 に答える
0

アクセスしているコレクションのインデックスがオーバーフローしていないことを確認する必要があります。このようなものが機能するはずです:

If i < dllFunctionObj.ReturnTemplateField.Length Then

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

End If

基本的に、存在しない値へのアクセスから保護するために、このifブロックで「hash.Add」への呼び出しをラップする必要があります。

確かに、私はコードの目的を理解していないので、「else」の場合を処理する必要があるかもしれません。しかし、これでエラーは止まるはずです。

于 2013-02-15T17:20:30.757 に答える