首页 > 解决方案 > 使用 vb.net 的属性、数组和 ArrayLists

问题描述

在我的 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
的属性的后三个属性之后,不要破坏第一个属性。
到目前为止,他们正在做什么
最后我有两个(2)_items在我的ArrayList其中每个都有相同的文本_item(这是最后一个)。
我需要的是编写第二组(或更多)属性,而不会破坏之前_itemsArrayList.

标签: vb.netarraylist

解决方案


最后我已经成功解决了这个难题,如下
一个属性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参数到字符串的转换,
首先将它们添加到ArrayList属性
中,然后将它们添加到第二个ArrayList
,这些结果ArrayList我将它添加到Pull Down menu控件中。
为什么我要这么做?
那是因为我有很多用户具有与键相同的属性但不同的值
大家好,非常感谢。


推荐阅读