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

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

问题描述

我想填写一个包含项目的几个属性的字典。示例:





我在想着拥有项目1 项目2 作为字典键与数组保持其属性。
我需要能够单独访问项目的每个属性,因此将它们连接起来,因为一个字符串不是一个选项。



我正在考虑以下伪代码

 使用工作簿(测试宏)。表(测试).Range(D7:G8)

对于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)


解决方案

这是一个使用类和集合的简单示例(基本上从示例这里



类很简单(类名是Employee):

  Option Explicit 

私人pName As String
私人pAddress As String
私人pSalary作为双

公共属性获取名称()作为字符串
名称= pName
结束属性
公共属性让名称(值作为字符串)
pName =值
End Pro perty
公共属性获取地址()As String
地址= pAddress
结束属性
公共属性让地址(值作为字符串)
pAddress = Value
结束属性
公共属性获取工资()As Double
工资= pSalary
结束属性
公共财产让工资(价值为双倍)
pSalary =价值
结束属性

以下是测试代码:

  Option Explicit 

子测试()
Dim counter As Integer

Dim Employees As Collection
Dim Emp As Employee

Dim currentEmployee As Employee


Set Employees = New Collection

对于counter = 1 To 10
设置Emp =新员工

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

Employees.Add Emp,Emp.Name

下一个计数器

设置currentEmployee = Employees .Item(Employee 1)


Debug.Print(currentEmployee.Address)

End Sub
/ pre>

正如你所看到的,我正在添加项目到我的课堂,指定一个键:

  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天全站免登陆