January 21, 2021
Hot Topics:

Visual Basic Tutorial - Part 6

  • By Karl Moore
  • Send Email »
  • More Articles »

Tutorial five was the most difficult tutorial of all time, ever. In the entire universe and infinitesimal galaxies beyond. Honest.

In that section, we took another look at properties, methods and events. Feel free to check out the tutorial here, should you have a strange desire to review that section.

We then moved onto explaining functions... wow, they're just so useful!

Q1. So, a function can return a value, right?

That's right. Let's take an example function:

Public Function AddThemUp( _
             NumberOne as Integer, _
             NumberTwo as Integer)

	AddThemUp = NumberOne + NumberTwo

End Function

Let's pretend you call this function with the line of code:

MsgBox "1 plus 2 equals " & AddThemUp(1, 2)

When Visual Basic hits the AddThemUp part, it runs the function - passing it the parameters 1 and 2 consecutively.

The function then sets the return value of AddThemUp equal to NumberOne (the first parameter, which is 1) plus NumberTwo (the second parameter we passed, which is 2).

Therefore, VB displays a message box saying "1 plus 2 equals 3".

If it doesn't, check that your computer:
(a) Is Switched On, or
(b) Is Switched On, or
(c) Is Switched On

But who cares. After all, everybody knows that 1 plus 2 equals 4. It's a trick question.

Q2. Karl? KARLIE? Is that you, honey sugar pumpkin?


Q3. In some of the tutorial five examples, you displayed something like this - MsgBox "Textbox One plus Textbox Two equals " & Text1.Text + Text2.Text

Why did you use the "quotes" and the & ampersand & ?

Every time you work with a string - basically, a bit of text - you must enclose it in "quotation marks". This is to ensure Visual Basic understands it's a string of text and not a command.

For instance, your string might contain the text 'MsgBox' - now you don't want VB to go displaying a message box when it sees that. That's why you use quotes - to partition off text strings.

The ampersands are used to concatenate bits of information together. So "Hello " & "You" in Visual Basic comes out as "Hello You". Just like the example you quoted above comes out as "Textbox One plus Textbox Two equals 35".

Get it? Got it! Good!

Page 6 of 7

This article was originally published on November 7, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date