Wrox Programmer Forums
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 13th, 2007, 04:24 PM
Registered User
 
Join Date: Nov 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to k.veness
Default Functions and Procedures

I need to take my code for a shell game and divide it into functions and procedures. The program works as it should but I am having difficulty dividing the code up further. I also have a feeling that some of my procedures should be functions. Any help would be appreciated. Thanks.

Here is the code:

Public Class frmShellGame

    Private Sub picShell_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles picShell1.Click, picShell2.Click, picShell3.Click

        'Determine which shell contains the pearl
        Dim shellWithPearl = randomizeNumber()

        'Determine which shell was clicked by user
        Dim picShellClicked As PictureBox = sender
        Dim shellClicked As Integer = Val(picShellClicked.Tag)

        'Sets a shell to be removed on guess
        Dim junkShell = randomizeNumber()
        Dim alternateChoice As Integer

        'Ensures that junkShell is not shellWithPearl and is not alternateChoice
        Call pickJunkShell(junkShell, shellWithPearl, shellClicked)

        Call pickAlternateChoice(alternateChoice, junkShell, shellClicked)

        'Removes junkShell
        Call removeJunkShell(junkShell)

        'Asks for confirmation and switches choice.
        Dim confirmChoice As String
        confirmChoice = InputBox("Do you want to keep your answer? (Yes/No)" & vbCrLf & "If not, then your guess will automatically switch to the other remaining shell.", "Confirm Guess")
        If confirmChoice = "No" Then
            shellClicked = alternateChoice
        End If

        'Shows Pearl
        Call showPearl(shellWithPearl)

        'Display message to player
        If shellClicked = shellWithPearl Then
            MsgBox("You won!")
        Else
            MsgBox("Sorry, you lose.")
        End If

        'Reset images
        Me.picShell1.Visible = True
        Me.picShell2.Visible = True
        Me.picShell3.Visible = True
        Me.picPearl1.Visible = False
        Me.picPearl2.Visible = False
        Me.picPearl3.Visible = False


    End Sub

    Function randomizeNumber() As Integer
        Randomize()
        Return Int(3 * Rnd()) + 1
    End Function

    Sub pickJunkShell(ByRef junkShell As Integer, ByRef shellWithPearl As Integer, ByRef shellClicked As Integer)
        While (junkShell = shellWithPearl) Or (junkShell = shellClicked)
            junkShell = randomizeNumber()
        End While
    End Sub

    Sub pickAlternateChoice(ByRef alternateChoice As Integer, ByVal junkShell As Integer, ByVal shellClicked As Integer)
        alternateChoice = 6 - junkShell - shellClicked
    End Sub

    Sub removeJunkShell(ByVal junkShell As Integer)
        Select Case junkShell
            Case 1
                Me.picShell1.Visible = False
            Case 2
                Me.picShell2.Visible = False
            Case 3
                Me.picShell3.Visible = False
        End Select
    End Sub

    Sub showPearl(ByVal shellWithPearl As Integer)
        Select Case shellWithPearl
            Case 1
                Me.picPearl1.Visible = True
            Case 2
                Me.picPearl2.Visible = True
            Case 3
                Me.picPearl3.Visible = True
        End Select
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub
End Class
 
Old November 13th, 2007, 04:41 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Functions are procedures. The distinction is between Functions and Subroutines. In a Function, part of the procedure the code is to enact is the returning of a value.

There is no reason to use the Call keyword. The purpose of that modifier is to run a Function, and discard the return value.

ShowPearl is a Sub. Therfore there is no value to discard through Call ShowPearl().
removeJunkShell is a Sub. Therfore there is no value to discard through Call removeJunkShell().
pickAlternateChoice is a Sub. Therfore there is no value to discard through Call pickAlternateChoice(). (And so on.)

This could be wrapped into a sub:
       'Reset images
        Me.picShell1.Visible = True
        Me.picShell2.Visible = True
        Me.picShell3.Visible = True
        Me.picPearl1.Visible = False
        Me.picPearl2.Visible = False
        Me.picPearl3.Visible = False

This could be wrapped into a Sub

        If shellClicked = shellWithPearl Then
            MsgBox("You won!")
        Else
            MsgBox("Sorry, you lose.")
        End If


Anytime you take a step that is foundational to the process, you can encapsulate that step in a procedure with a pertinent name, that either does– or does not return a value.
 
Old November 13th, 2007, 04:54 PM
Registered User
 
Join Date: Nov 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to k.veness
Default

Ok well I made those two sub procedures. Am I supposed to remove the Call functions that I wrote? Because I did that and then the program did not work as it should.

 
Old November 14th, 2007, 06:06 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Could you post the modified statements?

(BTW, if you use the Reply button rather than just typing in the Quick Reply text area, and then click the [[u]#</u>] button, you will see the way to post fixed-spacing code listings...)
 
Old November 15th, 2007, 12:51 AM
Registered User
 
Join Date: Nov 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to k.veness
Default

I actually finished the code for everything now. So thanks for the help. And unless anyone is interested in the full code then I suppose this topic could be closed now.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Stored Procedures help psnow1985 SQL Server 2005 2 April 12th, 2008 01:31 AM
2 procedures? scandalous Access 5 January 26th, 2007 05:06 PM
Help me to DO Stored Procedures msbsam SQL Server 2000 3 October 23rd, 2006 01:54 PM
Procedures Recompilation alyeng2000 SQL Server 2000 2 February 11th, 2005 10:38 AM
Regarding Procedures spraveens MySQL 1 March 10th, 2004 10:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.