Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sub procedure...

Posted on 2004-10-28
23
Medium Priority
?
944 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

618 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