Sub procedure...

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...
skathariAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

andrewharrisCommented:
In its most simplistic form.

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

Andrew
0
skathariAuthor Commented:
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
andrewharrisCommented:
what happens?

Andrew
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

skathariAuthor Commented:
it doesn't do anything... it might be my computer.... I'll check it on my laptop and let you know
0
arif_eqbalCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skathariAuthor Commented:
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
eozz_2000Commented:
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
skathariAuthor Commented:
lets do it with a button
0
arif_eqbalCommented:
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
andrewharrisCommented:
Why "Dim HalveNumber As Integer"?

Andrew
0
arif_eqbalCommented:
Not required
I just copied the code from above and forgot to delete the line
0
Arthur_WoodCommented:
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
skathariAuthor Commented:
Are you sure I have to add "handles btnclick.click" in the line? because it sis "Expected: end of statement"
0
Arthur_WoodCommented:
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
skathariAuthor Commented:
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
skathariAuthor Commented:
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
andrewharrisCommented:
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
Arthur_WoodCommented:
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
Arthur_WoodCommented:
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
andrewharrisCommented:
Thats better.

Andrew
0
andrewharrisCommented:
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
skathariAuthor Commented:
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
kGeniusCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.