从会话中的2-d arraylist中获取值

时间:2011-09-05 06:16:19

标签: asp.net vb.net arraylist

我有一个2-d arraylist,有2个固定列和动态行。 arraylist将被分配给下面代码末尾的session变量。我的问题是如何从会话中循环出来的arraylist来获得它的价值?

If .SQLDS.Tables(.sSQLDSTbl).Rows.Count > 0 Then
    Dim NoOfAdjType(1, .SQLDS.Tables(.sSQLDSTbl).Rows.Count - 1)

    For iRow As Integer = 0 To .SQLDS.Tables(.sSQLDSTbl).Rows.Count - 1
        If Not .SQLDS.Tables(.sSQLDSTbl).Rows(iRow).Item("i_commAmt") Is System.DBNull.Value Then
            NoOfAdjType(0, iRow) = .SQLDS.Tables(.sSQLDSTbl).Rows(iRow).Item("productType")
            NoOfAdjType(1, iRow) = Format(.SQLDS.Tables(.sSQLDSTbl).Rows(iRow).Item("i_commAmt"), "#,##0.00")
        End If
    Next

        Session("iNoOfAdjAmtType") = NoOfAdjType
End If

我试过这个,但它给了我错误''公共可覆盖的默认属性项(索引为整数)作为对象的参数太多

Dim NoOfAdjType As ArrayList = CType(Session("iNoOfAdjAmtType"), ArrayList)
For i As Integer = 0 To NoOfAdjType.Count
    Dim a As String = NoOfAdjType(0, i)
    Dim b As String = NoOfAdjType(1, i)
Next

3 个答案:

答案 0 :(得分:0)

您正在处理的类型是Object(,)。因此,当从会话中读取时,您可以将其转换回此类型。

这是一个article on MSDN,它说明了如何从会话中读取值:

Dim NoOfAdjType as Object(,) = CType(Session("iNoOfAdjAmtType"), Object(,))
' do something with the list

如果您想安全地执行检查,确保会话中有一个具有给定ID的项目:

If Session.Item("iNoOfAdjAmtType") IsNot Nothing Then
    ' We have a value in the session with the given id
    Dim NoOfAdjType as Object(,) = CType(Session("iNoOfAdjAmtType"), Object(,))
End If

答案 1 :(得分:0)

我不确定数组的数据类型是什么,但这是如何操作VB.NET中的多维数组,假设数据类型为对象

' declaring variable of multi-dim array
Dim NoOfAdjType As Object(,)
' create array object of needed dimension (you may use redim keyword)
NoOfAdjType = new Object(1, .SQLDS.Tables(.sSQLDSTbl).Rows.Count - 1) {}

...

' push it in session
 Session("iNoOfAdjAmtType") = NoOfAdjType

...

' get back from session
NoOfAdjType = DirectCast(Session("iNoOfAdjAmtType"), Object(,))
...
For i As Integer = 0 To NoOfAdjType.GetLength(0)
   For j As Integer = 0 To NoOfAdjType.GetLength(1)
      Dim a As Object = NoOfAdjType(i, j);
      ...
   Next
Next

在VB.NET中查看有关数组的MSDN文章:http://msdn.microsoft.com/en-us/library/wak0wfyt.aspx

答案 2 :(得分:0)

试试这个,

Dim a As String = NoOfAdjType(0)(0,0)

或使用

For Each arr As Object(,) In NoOfAdjType

Next