Solved

Sub procedure...

Posted on 2004-10-28
893 Views
Last Modified: 2006-11-17
Hi, what I want to do is.... write a code for a Sub procedure that receives an integer passed to it. The procedure named HalveNumber should divide the integer by 2 and then display the result in the lblNum control.
Can someone show me how to do this? Thank you...
0
Question by:skathari
    23 Comments
     
    LVL 4

    Expert Comment

    by:andrewharris
    In its most simplistic form.

    Private Sub HalveNuber(numberToHalve as Integer)
        frmMain.lblNum = numberToHalve/2
    End Sub

    Andrew
    0
     

    Author Comment

    by:skathari
    It doesnt work, thats what i did and it doesnt do it.
    Private Sub HalveNumber(numbertohalve As Integer)
        Dim HalveNumber As Integer
        Dim numbertohalve As Integer
        frmMain.lblNum = numbertohalve / 2
    End Sub
    0
     
    LVL 4

    Expert Comment

    by:andrewharris
    what happens?

    Andrew
    0
     

    Author Comment

    by:skathari
    it doesn't do anything... it might be my computer.... I'll check it on my laptop and let you know
    0
     
    LVL 19

    Accepted Solution

    by:
    There's a small error with your code

    Private Sub HalveNumber(numbertohalve As Integer)
        Dim HalveNumber As Integer
        Dim numbertohalve As Integer
        frmMain.lblNum = numbertohalve / 2
    End Sub

    See You are passing "numbertohalve" as an argument and in the same procedure you have a variable with the same name
    Dim numbertohalve As Integer

    So the value passed in argument will not be visible as the local variable will eclipse it.
    Just remove the line
    Dim numbertohalve As Integer

    And it will work

    Also you need to write frmMain.lblNum.Text only frmMain.lblNum will not work
    So last line will change to
    frmMain.lblNum.Text = numbertohalve / 2
    0
     

    Author Comment

    by:skathari
    but see, dont i have to add a button too?
    because i  only have a label in the form, and when i begin the program, it doesnt do anything, it only shows a blank form.
    0
     
    LVL 4

    Expert Comment

    by:eozz_2000
    jejeje... well, I could do in two ways, one with a button or in the KeyDown event of the textBox where you will place the number you want to divide (cheking if the key code is Enter).
    0
     

    Author Comment

    by:skathari
    lets do it with a button
    0
     
    LVL 19

    Expert Comment

    by:arif_eqbal
    Add a Button and the following Code on the Button_Click


       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            HalveNumber(5)
        End Sub

    Private Sub HalveNumber(numbertohalve As Integer)
        Dim HalveNumber As Integer
       frmMain.lblNum.Text = numbertohalve / 2
    End Sub
    0
     
    LVL 4

    Expert Comment

    by:andrewharris
    Why "Dim HalveNumber As Integer"?

    Andrew
    0
     
    LVL 19

    Expert Comment

    by:arif_eqbal
    Not required
    I just copied the code from above and forgot to delete the line
    0
     
    LVL 44

    Expert Comment

    by:Arthur_Wood
    the code should read:

    Private Sub HalveNumber(numbertohalve As Integer)
           lblNum.Text = numbertohalve / 2
    End Sub
      Assumiong that this SUb in in the Code for frmMain, and lblNum is a Label control on frmMain.

    as you had it originally coded:

    Private Sub HalveNumber(numbertohalve As Integer)
        Dim HalveNumber As Integer
        Dim numbertohalve As Integer '<<<< This line HIDES the argument of the Sub, replacing the value with 0
        frmMain.lblNum = numbertohalve / 2
    End Sub


    the

    dim numbertohalve as Integer  

    line INSIDE the procedure was HIDING (creating a LOCAL variable named numbertohalve) the value passed as the argument, and the value of the local variable was being assigned the value 0, which HIDES the value that was passed to the Sub.  This a VERY VERY bad idea, but a common mistake made by new programmers who are just learing.

    AW
    0
     

    Author Comment

    by:skathari
    Are you sure I have to add "handles btnclick.click" in the line? because it sis "Expected: end of statement"
    0
     
    LVL 44

    Expert Comment

    by:Arthur_Wood
    did you add the Button to the form by dragging the Command Button from the toolbox onto the form?
    double click on the Button control(named Button1) on the form,and your code should end up looking like this:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            lblNum.Text = HalveNumber(5)  ' << you add this line
    End Sub

    ' and thses lines
    Private Sub HalveNumber(ByVal numbertohalve As Integer)
        return  numbertohalve / 2
    End Sub


    AW
    0
     

    Author Comment

    by:skathari
    ok... this is the code that i put

    Private Sub HalveNumber(ByVal numbertohalve As Integer)
            Dim HalveNumber As Integer
            lblNum.Text = numbertohalve / 2
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            lblNum.Text = HalveNumber(5)
        End Sub
        Private Sub HalveNumber(ByVal numbertohalve As Integer)
            Return numbertohalve / 2
        End Sub
    End Class

    It has 3 errors:
    1)Method 'Halvenumber' has multiple definitions with identical signatures.
    2)'Return' statement in a Sub or Set cannot return a value
    3) 'Sub main' was not found in 'windowsapplication1.form1'.
    0
     

    Author Comment

    by:skathari
    i modified it... so, its this
    rivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            lblNum.Text = HalveNumber(5)
        End Sub
        Private Sub HalveNumber(ByVal numbertohalve As Integer)
            Dim HalveNumber As Integer
            lblNum.Text = numbertohalve / 2
            Return numbertohalve / 2
        End Sub
    End Class
    0
     
    LVL 4

    Expert Comment

    by:andrewharris
    1. You have still declared HalveNumber as both a Sub name _and_ a local variable name
    2. You _cannot_ return a value in a Sub, it must be a Function to return a value
    3. You need to go to the properties of your project (Not Solution) and set your form as the Startup Object

    As you have declared HalveNumber as a Sub, you will need to set the value of lblNum.Text in the Sub. If you dont want to do this and have the Method purely calculate the value and return the value then you need to change the Sub to a Function.

    Andrew
    0
     
    LVL 44

    Expert Comment

    by:Arthur_Wood
    it is not a SUB, it is a FUNCTION:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            lblNum.Text = HalveNumber(5)
        End Sub
        Private FUNCTION HalveNumber(ByVal numbertohalve As Integer) as String
            Return numbertohalve / 2
        End Sub
    End Class

    A Function returns a value, a Sub does NOT.

    AW
    0
     
    LVL 44

    Expert Comment

    by:Arthur_Wood
    or, if you MUST use as SUb, then the code is like this:

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            HalveNumber(5)
        End Sub
        Private FUNCTION HalveNumber(ByVal numbertohalve As Integer) as String
           lblNum.Text = numbertohalve/2
        End Sub

    AW
    0
     
    LVL 4

    Expert Comment

    by:andrewharris
    Thats better.

    Andrew
    0
     
    LVL 4

    Expert Comment

    by:andrewharris
    You could also do the following:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim valueToSet as String
            HalveNumber(5, valueToSet)
            lblNum.Text  = valueToSet
        End Sub

        Private Sub HalveNumber(ByVal numbertohalve As Integer, ByRef retValue as String)
           retValue = numbertohalve/2
        End Sub

    Andrew
    0
     

    Author Comment

    by:skathari
    ok, now I get the error "Sub main was not found in WindowsApplication1.Form 1".
    I have this code
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim valueToSet As String
            HalveNumber(5, valueToSet)
            lblNum.Text = valueToSet
        End Sub

        Private Sub HalveNumber(ByVal numbertohalve As Integer, ByRef retValue As String)
            retValue = numbertohalve / 2
        End Sub
    End Class
    0
     
    LVL 7

    Expert Comment

    by:kGenius
    I think you've deleted all the auto-generated code that the form needs to load.
    You probably better add a new form to you project and remove the old form. Just start over...

    Then follow these steps.
    Drag a label and a button from the toolbox to the form.
    Now double-click the button on the form and inside the Button1_Click handler at the code as you gave above.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim valueToSet As String
            HalveNumber(5, valueToSet)
            lblNum.Text = valueToSet
        End Sub



    Under the "Button1_Click" handler at this code:
        Private Sub HalveNumber(ByVal numbertohalve As Integer, ByRef retValue As String)
            retValue = numbertohalve / 2
        End Sub
    End Class


    Leave all the rest of the code ! Don't touch it...
    Hope you'll get there now.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

    845 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now