将最后一个字母连接到一段代码(Linq?)

时间:2014-01-04 04:04:23

标签: vb.net linq

对于我正在使用visual basic进行设计的桌面游戏,我有6个名为1,2,3,4,5和6的图片文件。

我正在生成1到6之间的随机整数。基本上,如果整数是2,我希望将2连接到My.Resources._成为My.Resources._2等等

我尝试了My.Resources._ & DiceRoll的某些内容,但它不起作用。我想我可能要使用Linq吗?

 Function RollDice()
        Dim DicePic() As PictureBox = {PictureBox3, PictureBox2}
        Dim DiceRoll = Int(Rnd() * 6 + 1)
        DicePic(DiceNumber).Image = My.Resources._

    End Function

2 个答案:

答案 0 :(得分:0)

您必须预先将资源加载到数组或使用Select Case。

Dim ResourcesArray As New ArrayList()
Function PreLoad()
    ResourcesArray.Add(My.Resources._1)
    ResourcesArray.Add(My.Resources._2)
    ResourcesArray.Add(My.Resources._3)
    ResourcesArray.Add(My.Resources._4)
    ResourcesArray.Add(My.Resources._5)
    ResourcesArray.Add(My.Resources._6)
End Function

...
DicePic(DiceNumber).Image = ResourcesArray(DiceRoll)
...

或者

Function ResourceFind(Index As Integer)
    Select Index
       Case 1: Return(My.Resources._1)
       Case 2: Return(My.Resources._2)
       Case 3: Return(My.Resources._3)
       Case 4: Return(My.Resources._4)
       Case 5: Return(My.Resources._5)
       Case 6: Return(My.Resources._6)
    End Select
End Function

...
DicePic(DiceNumber).Image = ResourceFind(DiceRoll)
...

答案 1 :(得分:0)

Private Function DiceImage(diceSide As Integer) As Image
 Dim resultImg As Image = Nothing
 Select Case index
   Case 1
     resultImg = My.Resources._1
   'etc...
 End Select
 Return resultImg
End Function

用法:

Private Sub RollDice()
 Dim DicePic() As PictureBox = {PictureBox3, PictureBox2}
 Dim DiceRoll = Int(Rnd() * 6 + 1)
 DicePic(DiceNumber).Image = DiceImage(DiceRoll)
End Sub