Group Forums >> web developers and designers >> Visual Basic Problem

Rate

Visual Basic Problem

59 Views
2 Replies Flag as inappropriate
New_hair_max50

3 posts

back to top

Posted 7 months ago

 

Hay guys, what is going on.  I am taking a beginning VB class.  The professor gave us this problem.  I'm not sure how to handle work it.


Private Sub btnDisplay_Click(...) Handles btnDisplay.Click




Dim num As Double = 7




AddTwo(num)




txtOutput.Text = CStr(num)




End Sub




Sub AddTwo(ByRef num As Double)




num += 2




End Sub


If anyone has any ideas, I am willing tl learn.


 

120-overwatch_max50

30 posts

back to top
Rate

Rate This | Posted 7 months ago

 

The "Private Sub btnDisplay_Click(...) ...." is the button click even handler for the button placed on the form in the visual mode.


"Dim num As Double = 7" is creating a variable of type double with the value of 7 in it.


"AddTwo(num)" is a combo of two things. The first is that it is a subprocedure call which executes the code from "Sub AddTwo(...)" In the () you are passing the variable num that you created in the above statement.


"txtOutput.Text = CStr(num)" The txtOutput I am assuming is a text box. The .text is refering to the text attribute of the object (textbox) the = is placing the text into the text box. To create the text you are converting the number into text by using the CStr() function. You are passing the num variable that you have to the CStr function and it is returning the number as a string.


"End Sub" simply ends the button click event.


"Sub AddTwo(ByRef num As Double)" In this line you are creating a subprocedure named AddTwo. This was called by the button click event above. There are two ways to pass a variable. ByRef and ByVal. ByRef is passing the memory address which means you can change the information within the variable. ByVal would only pass the information int he variable and would not allow you to change the infomation in the original variable. You can use any naming convention for a variable in the place of num. The instructor is justkeeping it formated the same for conveninece. the only thing is if you chaneg num to num1 you will need to change the usage of num in the AddTwo subprocedure to  reflect that change. the As Double sets the variable that can be passed to a double.


"num += 2" is the same as puting num = num + 2, just is is shorter and a good programming practice.


"End Sub" ends the AddTwo subprocedure.


Basically to make this code work you will want to open Visual Studio. You can then start a VB project and drag a button to the form. If you double click the button you will be taken to the button click even for that button. Copy the code between the Private Sub... and the End Sub of the button click event and paste it in the code. Then copy the entire AddTwo procedure and paste it belowe the button click event End Sub line. Go back to the form and place a text box on the form. Name the text box txtOutput. Instead of btnDisplay you probably will notice that it says Button1. You can change this by changing the button name in the properties window to btnDisplay but make sure you replace Button1 in the code to reflect that change.


I hope this helps. =-)


James