previous page main page next page

The line
 

LFCR = Chr(10) + Chr(13)
 

needs some explanation. Chr(10) is the linefeed character and Chr(13) is the carriage return character and so LFCR is a combination of the two, which we need when we print out the text in the label. OK, it's a bit messy but, put simply, all it does is move any following text onto a new line, just as if you'd pressed the ENTER key.
 

Days = 365 * Age
 

This calculates the number of days assuming that there are 365 days in a year - we don't want to worry about leap years here...

The next line
 

'build output string
 

doesn't in fact do anything and is ignored by the computer. It does however act as a comment to us and it is considered good practice to use comments in order to help other people understand your programs and make them more readable.

And now to the following code:
 

TextOut = "Hello " + UserName
TextOut = TextOut + LFCR
TextOut = TextOut + "You are at least "
TextOut = TextOut + Str(Days)
TextOut = TextOut + " days old!"
 

The comment said it all really. We are building up a string, or piece of text, step by step. We start with the word "Hello " and we add the variable UserName, which we know is another piece of text. The line
 

TextOut = TextOut + LFCR
 

takes the string TextOut and adds to it our linefeed/carriage return combination. We proceed to add to the TextOut string as we go. There is a slight problem because, while UserName is a string, Days is an integer, and it doesn't make sense to add a number to a string - they are completely different data types. The way round it, though, is to use a special function called Str which Visual Basic has thoughtfully provided. It's job is to take a numerical value and convert it into a string, which we can then add to TextOut. Easy, see?
 

  • as an experiment, try removing the Str function and see what happens when you run the program using this line instead:
     

TextOut = TextOut + Days

You should get a 'type mismatch' error and if you click the 'Debug' button you'll find that the offending line of code has been highlighted.

Finally, the line
 

lblResults.Caption = TextOut
 

assigns our completed string to the Caption property of the label, and that's what you see printed out.

It's fair to say that we could have done all this using fewer lines of code - adding two, or more parts in a single line of code, for example - but we think it makes it clearer to set it out as shown.

Phew, heavy stuff, huh?

This program, then, has used different input and output methods to the previous one. It's very much a matter of preference and how you want your program to look. You should always consider how best to make your program easy to use. All we can do is to try and point out the possibilities.

We're not quite done with this program yet, because we can use it to make a couple of important points...
 

previous page main page next page ©  ePublish Scotland  1999