使用vb.net的属性,数组和ArrayList

时间:2018-12-08 10:31:02

标签: vb.net arraylist

在我的Web项目后面的代码中,我有一个属性

Public Shared UserAttributes(2) As String
    Public Property _UserAttributes(ByVal Index As Integer) As String
        Get
            Return UserAttributes(Index)
        End Get
        Set(value As String)
            UserAttributes(Index) = value
        End Set
    End Property

我还有一个ArrayList声明为Friend

Friend UserParameters As New ArrayList

我这样称呼我的财产:

_UserAttributes(0) = "parameter1"
_UserAttributes(1) = "parameter2"
_UserAttributes(2) = "parameter3"
UserParameters.Add(UserAttributes)
_UserAttributes(0) = "parameter1,1"
_UserAttributes(1) = "parameter2,1"
_UserAttributes(2) = "parameter3,1"
UserParameters.Add(UserAttributes)

从上面的代码中,我们可能会看到两对属性,每对属性各有一个文本。
我现在需要的是:
Property的三个属性添加到ArrayList后,
我财产的后三个属性不会破坏第一个属性。
他们现在在做什么
最后,我的_items中有两个(2)ArrayList,每个_item上的文本相同(这是最后一个)。
我需要编写第二组(或更多组)属性,而不会破坏_items中的前ArrayList

1 个答案:

答案 0 :(得分:0)

最后,我通过以下方法解决了这个难题
一个属性为ArrayList

Public Property _UserParameters As ArrayList
        Get
            Return UserParameters
        End Get
        Set(value As ArrayList)
            UserParameters = value
        End Set
    End Property

第二个属性为Array

Public Property _UserAttributes(ByVal Index As Integer) As String
        Get
            Return UserAttributes(Index)
        End Get
        Set(value As String)
            UserAttributes(Index) = value
        End Set
    End Property

在后面的代码中,我使用以下代码:

Dim UserAttributes As New Hashtable
    Dim key As Object = Nothing
    Dim Param As Object = Nothing
    Dim myList As New ArrayList
    Dim item As Object = UserAttributes
    UserAttributes.Add("UserId", "Parametr1")
    UserAttributes.Add("UserName", "Parametr2")
    UserAttributes.Add("UserMail", "Parametr3")
    For Each item In UserAttributes
        key = item.Key
        Param = item.value
        logHandler._UserParameters.Add(key & "^" & Param)
    Next
    myList.Add(logHandler.UserParameters.ToArray)
    UserAttributes.Clear()
    logHandler.UserParameters.Clear()
    UserAttributes.Add("UserId", "Parametr1-1")
    UserAttributes.Add("UserName", "Parametr2-1")
    UserAttributes.Add("UserMail", "Parametr3-1")
    For Each item In UserAttributes
        key = item.Key
        Param = item.value
        logHandler._UserParameters.Add(key & "^" & Param)
    Next
    myList.Add(logHandler.UserParameters.ToArray)

使用HashTable解决了我的问题,以及从HashTable参数到String的转换
首先将它们添加到ArrayList属性中
然后将它们添加到第二个ArrayList
然后将这些ArrayList的结果添加到Pull Down menu控件中。
为什么我要那样做?
那是因为我有许多用户具有与键相同的属性,但是值不同
祝大家有美好的一天,非常感谢。