3词典 [英] 3 Dimentional Dictionary

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

问题描述

我正在努力使3维词典以工具(材料)(部分)(属性)的形式存储数据,并且我设法创建了这样的字典:

I'm trying make to a 3 Dimension Dictionary to store the data in the form of tools(material)(part)(attribute), and I have managed to create the Dictionary like this:

Dim Tools As New Dictionary(Of String, Dictionary(Of String, Dictionary(Of String, Decimal)))

我基本上想做的是有一些子系统来管理我,而不是处理那个烂摊子,我希望它像这样如下所示:

And what I basically want to do is have some subs that manage that for me instead of dealing with that mess, and I want it to be like this like this:

Add_Attribute("Iron", "Pickaxe Head", "Durability", 204)
Get_Attribute("Stone", "Pickaxe Head", "Mining Speed")

任何答案都将不胜感激

推荐答案

Jules的一个自定义类的答案和三个字符串的连接作为一个关键,将会非常适合你,而且对您的问题的一个整洁的解决方案。

Jules' answer of a custom class and concatenation of the three strings as a key will work very nicely for you and is a neat solution to your problem.

我在这里发布另一个答案,任何人想要更多的点符号解决方案。因此,您的示例中的一行可能如下所示:

I'm posting another answer here for anyone who wants more of a dot notation style of solution. So one of the lines in your example could look something like:

mTools("Pickaxe Head").Attr("Durability").Material("Iron") = 204

我猜你是从一个comboxbox或类似的东西,所以使用字符串可能会为您服务。但是,如果您希望,您可以进一步进行一个阶段,并为属性材质参数创建对象,以实现真正的点符号(我没有执行零件参数,但您也可以这样做):

I'm guessing you're deriving the values from a comboxbox or something similar, so working with strings might serve you fine. However, if you wished, you could go one stage further and create objects for the Attributes and Material parameters to achieve true dot notation (I didn't do the Parts parameter but you could do that one too):

mTools("Pickaxe Head").Durability.OnIron = 204

从开发的角度来看,耗时的部分是创建所有参数对象和钥匙,但是如果您打算以任何方式操纵数据,可能会让您的生活更加轻松。

From a development point of view, the time consuming part would be to create all the parameter objects and keys, but if you are intending to manipulate the data anything more than trivially, it could make your life easier further down the track.

对于您自己的项目,是吗确定数据是真正的3维?也许这只是您选择的变量名称,但似乎您有一个主要对象,即具有某些属性( Durability )的部分( Pickaxe Head )和采矿速度),其本身具有基于他们正在操作的材料( Stone Iron )的值。在结构上,可以这样吗?:

For your own project, are you certain that the data is genuinely 3 dimensional? Perhaps it's just the variable names that you've picked, but it seems as though you have one main object, ie the part (Pickaxe Head) which has some attributes (Durability and Mining Speed) which themselves have values based on the material they're operating on (Stone and Iron). Structurally, could it look like this?:

对于此解决方案的代码,创建三个类。我打电话给他们 clsKeys , clsMaterials clsPart

In terms of the code for this solution, create three classes. I've called them clsKeys, clsMaterials and clsPart.

code> clsKeys ,代码只是你的字段名称:

For your clsKeys, the code is simply your field names:

Public Durability As String
Public MiningSpeed As String
Public Iron As String
Public Stone As String

对于 clsPart ,代码包含对象名称和通过字符串访问它们的方法:

For clsPart, the code contains the object names and a means of accessing them by string:

Public Name As String
Public Durability As New clsMaterials
Public MiningSpeed As New clsMaterials
Private mProperties As New Collection
Public Property Get Attr(field As String) As clsMaterials
    Set Attr = mProperties(field)
End Property
Private Sub Class_Initialize()
    With Keys
        mProperties.Add Durability, .Durability
        mProperties.Add MiningSpeed, .MiningSpeed
    End With
End Sub

clsMaterials 是类似的:

Public OnStone As Integer
Public OnIron As Integer
Private mProperties As New Collection
Public Property Let Material(field As String, value As Variant)
    mProperties.Remove field
    mProperties.Add value, field
End Property
Public Property Get Material(field As String) As Variant
    Material = mProperties(field)
End Property
Private Sub Class_Initialize()
    With Keys
        mProperties.Add OnStone, .Stone
        mProperties.Add OnIron, .Iron
    End With
End Sub

这些类可以采取尽可能多的对象,你喜欢。你会注意到我已经在声明中实例化了不是最好的形式的对象,但是我已经为了空间的利益而做了。

These classes can take as many objects as you like. You'll note I've instantiated the objects in the declaration which isn't best form but I've done it in the interest of space.

最后,在模块您需要3个例程:一个用于创建字段键,一个用于填充数据,一个用于检索。

Finally, in a Module you need 3 routines: one to create the field keys, one to populate the data and one to retrieve it.

对于键:

Option Explicit
Public Keys As clsKeys
Private mTools As Collection
Sub CreateKeys()
    Set Keys = New clsKeys
    With Keys
        .Durability = "Durability"
        .MiningSpeed = "Mining Speed"
        .Iron = "Iron"
        .Stone = "Stone"
    End With
End Sub

对于数据填充:

Sub PopulateData()
    Dim oPart As clsPart

    Set mTools = New Collection

    Set oPart = New clsPart

    With oPart

        .Name = "Pickaxe Head"

        'You could use dot notation
        .Durability.OnIron = 204
        .Durability.OnStone = 100

        'Or plain strings
        .Attr("Mining Speed").Material("Stone") = 50
        .Attr("Mining Speed").Material("Iron") = 200

        mTools.Add oPart, .Name

    End With

End Sub

和数据检索:

Sub RetrieveValue()
    Dim oPart As clsPart
    Dim v As Variant

    Set oPart = mTools("Pickaxe Head")

    With oPart

        'Using dot notation
        v = oPart.Durability.OnIron
        Debug.Print v

        'Using plain strings
        v = oPart.Attr("Durability").Material("Stone")
        Debug.Print v

    End With

    'Or even without assigning the oPart variable
    v = mTools("Pickaxe Head").Attr("Mining Speed").Material("Iron")
    Debug.Print v

End Sub

这篇关于3词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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