在 Visual Basic 中将字符串转换为预定义变量名?

发布于 2024-12-14 10:19:53 字数 1321 浏览 0 评论 0原文

我正在 Visual basic 中编程一个基本老虎机,并且想要使用 for 循环为每个老虎机随机选择图像,在每个老虎机中显示图像,并更改 slotName 变量(这样我可以稍后检查哪些符号是在插槽中)每个插槽。

我在 for 循环中发现的问题是每个槽的变量和对象具有不同的名称(slot1Name、slot2Name、slot3Name、lblSlot1、lblSlot2、lblSlot3 等)。有什么办法可以得到类似的东西:

currentSlotName = "slot" & i & "Name"

这是目前的代码,对于 3 个插槽中的每一个,该代码都是重复的(使用不同的变量和对象名称),这是相当低效的。我该如何整理这段代码?

' Randomise numbers and assign images to slots based on random numbers, if the hold isn't on
    ' Slot 1
    If Not held1 Then
        slot1Value = Int(Rnd() * numbersGenerated + 0.5)
        Select Case slot1Value
            Case 0 To 5
                lblSlot1.Image = imgBanana
                slot1Name = "Banana"
            Case 6 To 11
                lblSlot1.Image = imgOrange
                slot1Name = "Orange"
            Case 12 To 16
                lblSlot1.Image = imgCherries
                slot1Name = "Cherries"
            Case 17 To 19
                lblSlot1.Image = imgSeven
                slot1Name = "Seven"
            Case 20
                lblSlot1.Image = imgBatman
                slot1Name = "Batman"
            Case Else
                lblSlot1.Text = "Error. slot1value = " & slot1Value
        End Select
    End If

我已经对此进行了搜索,但我对 Visual Basic 非常陌生,并且希望使我的代码尽可能简单。

I'm programming a basic slot machine in Visual basic, and want to use a for loop to randomly choose the image for each slot, display the image in each slot, and change the slotName variable (so I can check later on which symbols are in the slots) for each slot.

The problem I'm finding with a for loop is that the variables and objects for each slot have different names (slot1Name, slot2Name, slot3Name, lblSlot1, lblSlot2, lblSlot3, etc). Is there any way I could have something like:

currentSlotName = "slot" & i & "Name"

This is the code at the moment, this code is repeated (with different variable and object names), for each of the 3 slots, which is pretty inefficient. How can I tidy this code up?

' Randomise numbers and assign images to slots based on random numbers, if the hold isn't on
    ' Slot 1
    If Not held1 Then
        slot1Value = Int(Rnd() * numbersGenerated + 0.5)
        Select Case slot1Value
            Case 0 To 5
                lblSlot1.Image = imgBanana
                slot1Name = "Banana"
            Case 6 To 11
                lblSlot1.Image = imgOrange
                slot1Name = "Orange"
            Case 12 To 16
                lblSlot1.Image = imgCherries
                slot1Name = "Cherries"
            Case 17 To 19
                lblSlot1.Image = imgSeven
                slot1Name = "Seven"
            Case 20
                lblSlot1.Image = imgBatman
                slot1Name = "Batman"
            Case Else
                lblSlot1.Text = "Error. slot1value = " & slot1Value
        End Select
    End If

I have searched around for this, but I'm very new to Visual Basic, and want to keep my code as simple as possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最笨的告白 2024-12-21 10:19:53

太多需要解释。数组是你接下来需要学习的。

http://msdn.microsoft.com/en-us/library/wak0wfyt.aspx

Too much to explain. Arrays is what you need to learn next.

http://msdn.microsoft.com/en-us/library/wak0wfyt.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文