简体   繁体   中英

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)]). 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]] )

I would appreciate the most straightforward way of changing the python

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

into VB Thank you in advance

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. 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! 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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