简体   繁体   English

在python中,我执行“ dictionary = {“ key”:[value,value],(etc)}”。如何在Visual Basic中做到这一点?

[英]In python I do “dictionary = {”key“: [value, value], (etc)}” How do I do this in Visual Basic?

I'm doing a small adventure game for my computer science class in VB and to do the inventory of the character, I need to know how to make a dictionary(so I can have the name of the item) and a list as the values of the item(the list would be something like obj = [Quantity,Damage_Dealt(or Health_Received)]). 我正在VB中为我的计算机科学课做一个小型冒险游戏,并且要对角色进行盘点,我需要知道如何制作字典(这样我才能得到商品的名称)和清单作为值项目(列表类似于obj = [Quantity,Damage_Dealt(或Health_Received)])。 I've done some research and I tried to do 我做了一些研究,我试图做

Dim knife As New List(Of Integer)
knife.add(1)#the first one is how many knives and the second is the dmg
knife.add(1)

Dim inv As New Dictionary(Of String, List(Of Integer))
inv.add("knife", knife)

but there are some things missing or hopefully there is an easier way to do it. 但是有些东西丢失了,或者希望有一种更简单的方法可以做到。 Even if it is possible to create 2 or 3-d array (eg inv = [[knife, 1, 1], [bread, 2, 0]] ) 即使可以创建2或3维数组(例如inv = [[knife, 1, 1], [bread, 2, 0]]

I would appreciate the most straightforward way of changing the python 我将很欣赏更改python的最直接方法

dictionary = {"key": [value, value], (etc)}

into VB Thank you in advance 进入VB提前谢谢

I forgot to mention that all that is in a sub within a class. 我忘了提到所有在类中的子类中。

You can totally make a new list (Of Dictionary(Of String, List(Of Integer))) and populate it. 您完全可以制作一个新列表(字典(字符串,列表(整数)))并填充它。

You already seems to know the code. 您似乎已经知道代码了。 Have fun implementing it! 祝您实施愉快!

Here is a basic example of how to accomplish what you're looking for. 这是一个如何完成您要寻找的东西的基本示例。

Public Sub TestData()
        //Create List and populate it

        Dim list As New List(Of KeyValuePair(Of String, List(Of Integer)))
        Dim integerList As List(Of Integer) = New List(Of Integer)
        integerList.Add(1)
        integerList.Add(2)

        list.Add(New KeyValuePair(Of String, List(Of Integer))("Test", integerList))



        //Iterate through the list to get data

        For Each pair As KeyValuePair(Of String, List(Of Integer)) In list
            Dim key As String = pair.Key

            Dim newIntegerList As New List(Of Integer)
            For Each value As Integer In pair.Value
                newIntegerList.Add(value)
            Next
        Next
    End Sub

Edit: Had to change the comments to C# comments, as it didn't like the VB comments. 编辑:不得不将注释更改为C#注释,因为它不喜欢VB注释。 But obviously you'd get rid of those. 但是显然您会摆脱这些。

There are ways of doing it by creating a dictionary of lists or dictionary of arrays. 有多种方法可以通过创建列表字典或数组字典来实现。 But if you must rely on indexes to find the right property, the code will become messy. 但是,如果必须依靠索引来查找正确的属性,则代码将变得混乱。

A slightly better option is to use Tuples ; 更好的选择是使用元组 however, those are more thought to be used for temporary storage, for intermediate results and for functions returning more than one value. 但是,这些被更多地认为是用于临时存储,用于中间结果以及用于返回多个值的函数。

Items are an important aspect of your game. 项目是游戏的重要方面。 Create an Item class! 创建一个Item类! This will allow you to deal with items much easier 这将使您更轻松地处理物品

Public Class Item
    Property Quantity As Integer
    Property Damage As Integer
    Property Health As Integer
End Class

You can initialize it with 您可以使用

Dim Items As New Dictionary(Of String, Item) From {
   {"knife", New Item With {.Quantity = 1, .Damage = 0, .Health = 100}},
   {"arrow", New Item With {.Quantity = 5, .Damage = 0, .Health = 100}}
}

Now, you can retrieve properties easily 现在,您可以轻松检索属性

Dim knifeDamage = Items("knife").Damage

Classes have other advantages: You can add Subs and Functions to it, you can derive more specific item types having properties that apply only to them and much more. 还具有其他优点:您可以向其添加Subs和Function,可以派生具有仅适用于它们的属性的更多特定项目类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM