[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Sub procedure...

Posted on 2004-10-28
23
Medium Priority
?
964 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
Comment
Question by:skathari
  • 8
  • 6
  • 4
  • +3
23 Comments
 
LVL 4

Expert Comment

by:andrewharris
ID: 12441339
In its most simplistic form.

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

Andrew
0
 

Author Comment

by:skathari
ID: 12441372
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
ID: 12441395
what happens?

Andrew
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:skathari
ID: 12441400
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:
arif_eqbal earned 600 total points
ID: 12441412
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
ID: 12441453
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
ID: 12441509
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
ID: 12441516
lets do it with a button
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12441848
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
ID: 12441955
Why "Dim HalveNumber As Integer"?

Andrew
0
 
LVL 19

Expert Comment

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

Expert Comment

by:Arthur_Wood
ID: 12443359
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
ID: 12445024
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
ID: 12445378
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
ID: 12447651
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
ID: 12447745
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
ID: 12449288
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
ID: 12449474
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
ID: 12449483
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
ID: 12449487
Thats better.

Andrew
0
 
LVL 4

Expert Comment

by:andrewharris
ID: 12449522
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
ID: 12450438
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
ID: 12452325
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

607 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