【问题标题】:get the value from 2-d arraylist in session从会话中的二维数组列表中获取值
【发布时间】:2011-09-05 14:38:06
【问题描述】:

我有一个包含 2 个固定列和动态行的二维数组列表。数组列表将在下面的代码末尾分配给会话变量。我的问题是如何从会话中遍历数组列表以获取其值?

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

我已经尝试过了,但它给了我错误'Public Overridable Default Property Item(index As Integer) As Object'的参数太多

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

【问题讨论】:

    标签: asp.net vb.net arraylist


    【解决方案1】:

    您正在处理的类型是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
    

    【讨论】:

    • 谢谢,但我怎样才能得到数组列表中的行数?
    • @Danferd、NoOfAdjType.GetLength(0)NoOfAdjType.GetLength(1)
    【解决方案2】:

    我不确定数组的数据类型是什么,但这是您在 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

    【讨论】:

      【解决方案3】:

      试试这个,

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

      或者使用

      For Each arr As Object(,) In NoOfAdjType
      
      Next
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-06
        • 2012-10-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多