二维数组作为字典项 [英] Two dimensional array as item of dictionary

查看:415
本文介绍了二维数组作为字典项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想填充字典,一个项目的几个属性。例如:

样本数据

我用项目1 和项目2 的如词典 >阵列,将持有其属性。
我需要能够分别访问某个项目的每个属性,以便它们连接起来作为一个字符串是不是一种选择。

我正在考虑类似下面的伪code

 随着工作簿(测试宏),表(测试).Range(D7:G8)     对于i = 1到.Rows.count        items_dict.Add键:=细胞(1,1).value的,_。
 项:=阵列(I,1)= .cells(I,2)。价值阵列(I,2)=细胞(I,3)。价值阵列(I,3).cells(I,4)


解决方案

下面是使用类和集合(从例子中的这里

类是pretty简单(类名员工):

 显式的选项私人PNAME作为字符串
私人pAddress作为字符串
私人pSalary为双公共属性获取名称()作为字符串
    NAME = PNAME
高端物业
公共财产让名称(值作为字符串)
    PNAME =值
高端物业
公共属性获取地址()作为字符串
    地址= pAddress
高端物业
公共财产令地址(值作为字符串)
    pAddress =值
高端物业
公共属性拿不到工资()为双
    工资= pSalary
高端物业
公共属性让工资(价值双人间)
    pSalary =值
高端物业

下面是测试code:

 显式的选项子测试()
    昏暗的计数器作为整数    昏暗的员工为集合
    昏暗的Emp由于员工    昏暗currentEmployee由于员工
    设置员工=新集合    对于计数器= 1至10个
        设置的Emp =新员工        Emp.Name =雇员&放大器;计数器
        Emp.Address =地址&放大器;计数器
        Emp.Salary =柜台* 1000        Employees.Add的Emp,Emp.Name    接下来计数器    设置currentEmployee = Employees.Item(雇员1)
    Debug.Print(currentEmployee.Address)结束小组

正如你所看到的,我将项目添加到我的课指定关键字:

  Employees.Add的Emp,Emp.Name

然后您可以使用直接从拉不循环。

I would like to populate a dictionary with several properties of an item. Example:

I was thinking of having Item 1 and Item 2 as Dictionary keys with an array that would hold its properties. I would need to be able to separately access each property of an item so concatenating them as one string is not an option.

I'm thinking about something like the below pseudo-code:

    With Workbooks("testing macro").Sheets(test).Range("D7:G8")

     For i = 1 To .Rows.count

        items_dict.Add Key:=.Cells(i, 1).Value, _
 Item:= array(i,1)= .cells(i,2).value array(i,2)=.cells(i,3).value array(i,3).cells(i,4)

解决方案

Here is a simple example using a class and a collection (basically modified from the examples here:

Class is pretty simple (class name is Employee):

Option Explicit

Private pName As String
Private pAddress As String
Private pSalary As Double

Public Property Get Name() As String
    Name = pName
End Property
Public Property Let Name(Value As String)
    pName = Value
End Property
Public Property Get Address() As String
    Address = pAddress
End Property
Public Property Let Address(Value As String)
    pAddress = Value
End Property
Public Property Get Salary() As Double
    Salary = pSalary
End Property
Public Property Let Salary(Value As Double)
    pSalary = Value
End Property

Here is the test code:

Option Explicit

Sub test()
    Dim counter As Integer

    Dim Employees As Collection
    Dim Emp As Employee

    Dim currentEmployee As Employee


    Set Employees = New Collection

    For counter = 1 To 10
        Set Emp = New Employee

        Emp.Name = "Employee " & counter
        Emp.Address = "Address " & counter
        Emp.Salary = counter * 1000

        Employees.Add Emp, Emp.Name

    Next counter

    Set currentEmployee = Employees.Item("Employee 1")


    Debug.Print (currentEmployee.Address)

End Sub

As you can see, I'm adding items to my class specifying a key:

Employees.Add Emp, Emp.Name

which you can then use to pull directly from without looping.

这篇关于二维数组作为字典项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆