Results 1 to 2 of 2

Thread: n00b question

  1. #1

    Thread Starter
    New Member Laeys's Avatar
    Join Date
    Jun 2003
    Location
    online
    Posts
    8

    Question n00b question

    Greetings all,

    I just found this place and hope to stay for a while. I have read through a few threads and having a question myself, registered and here I am. Hope this isn't to low level for ya.
    I just got through with a 10 week VB.Net course that was included in my curriculum at school. We did all the basic stuff and what have you and here is a little something I am working on modifying....

    Code:
    Public Class FrmDiceSimulator
        Inherits System.Windows.Forms.Form
        Dim objRandom As Random = New Random()
       Dim intDie1 As Integer
       Dim intDie2 As Integer
       Dim intRandom As Integer
       Dim intSide1 As Integer = 0
       Dim intSide2 As Integer = 0
       Dim intSide3 As Integer = 0
       Dim intSide4 As Integer = 0
       Dim intSide5 As Integer = 0
       Dim intSide6 As Integer = 0
    
       Const PRE As String = "/Images/"
       Const SUF As String = ".png"
    
    Private Sub btnRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRoll.Click
            Call Roll()
            DisplayDie1(picDie1, intDie1)
            Call Roll()
            DisplayDie2(picDie2, intDie2)
    End Sub
    
    ...
    ...
    ...
    
    Sub DisplayDie1(ByVal picDie1 As PictureBox, _
                     ByVal intDie1 As Integer)
    
    
            Select Case intRandom
                Case 1
                    picDie1.Image = Image.FromFile(Directory.GetCurrentDirectory _
                                          & PRE & "die1" & SUF)
                    intSide1 = intSide1 + 1
                    lblOutput1.Text = intSide1
                Case 2
                    picDie1.Image = Image.FromFile(Directory.GetCurrentDirectory _
                                          & PRE & "die2" & SUF)
                    intSide2 = intSide2 + 1
                    lblOutput2.Text = intSide2
                Case 3
                    picDie1.Image = Image.FromFile(Directory.GetCurrentDirectory _
                                          & PRE & "die3" & SUF)
                    intSide3 = intSide3 + 1
                    lblOutput3.Text = intSide3
                Case 4
                    picDie1.Image = Image.FromFile(Directory.GetCurrentDirectory _
                                          & PRE & "die4" & SUF)
                    intSide4 = intSide4 + 1
                    lblOutput4.Text = intSide4
                Case 5
                    picDie1.Image = Image.FromFile(Directory.GetCurrentDirectory _
                                          & PRE & "die5" & SUF)
                    intSide5 = intSide5 + 1
                    lblOutput5.Text = intSide5
                Case Else
                    picDie1.Image = Image.FromFile(Directory.GetCurrentDirectory _
                                          & PRE & "die6" & SUF)
                    intSide6 = intSide6 + 1
                    lblOutput6.Text = intSide6
            End Select
    
        End Sub
    
    ...
    ...
    ...
    
        Function Roll() As Integer
            intRandom = objRandom.Next(1, 7)
            'DisplayDie(picDie1, intDie1, picDie2, intDie2)
    
            Return (intRandom)
        End Function
    
    End Class
    All this does right now is 'roll' 2 dice and display the images of them. While it also counts how many times each 'side' appears. Obviusly there is some code missing, but this is the code I was seeking assistance with. I am looking for a way, perhaps something with arrays(kinda shady to me), to be able to reuse this sample code 6 times. Instead of making
    Sub DisplayDie1, Sub DisplayDie2, etc. Any pointers would be greatly appreciated.
    Thanks in advance for your time.
    Laeys

    God is real. Unless declared as an integer.

  2. #2
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    create a class called dice and put all the common properties and functions in there such as roll dice. You can then declare 2 occurences of this class as dice1 and dice2
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width