reserved words

12
Special Words & Functions 1 Reserved Words There are words in VB that have been reserved for special meanings. In general, if you use one of these words to (name) an object in your own programs, their special meaning is lost and your use takes over. That can be an inconvenience or that can be a calamity depending on the word used and what is desired.

Upload: ehren

Post on 04-Jan-2016

26 views

Category:

Documents


1 download

DESCRIPTION

Reserved Words. There are words in VB that have been reserved for special meanings. In general, if you use one of these words to (name) an object in your own programs, their special meaning is lost and your use takes over. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Reserved Words

Special Words & Functions 1

Reserved Words

There are words in VB that have been reserved for special meanings. In general, if you use one of these words to (name) an object in your own programs, their special meaning is lost and your use takes over.

That can be an inconvenience or that can be a calamity depending on the word used and what is desired.

Page 2: Reserved Words

Special Words & Functions 2

Loss of Features

“Date” is a word that is used in VB to return the number of days that have passed since January 1, 1900 (there will be more on this later).

If you (name) a variable, or a command “Date” then it will not be possible to include any feature that uses the current date in that program.

Page 3: Reserved Words

Special Words & Functions 3

Real Calamity

“Sub” is used in VB in the processing of a subroutine or event. (The word is automatically added to the beginning of the code block of every event in the program.)

If you (name) any variable or command “Sub” the program will fail to even compile. The diagnostic may not even point to the problem.

Page 4: Reserved Words

Special Words & Functions 4

So What?

Do not (name) objects with what seem to be very common names. When in doubt, deliberately misspell the name. Best of all use the Microsoft convention of naming things with the 3 character prefix that “types” the object. Most important, when you observe that there is a diagnostic that makes no sense, look for a problem with reserved words and names.

Page 5: Reserved Words

Special Words & Functions 5

Reserved Words

System words:

Sub, End, Procedure, Dim, Single, Long, Visible, Else, ...

Reserved words:

Date, Time, Beep, Sin, Rnd, Randomize, ...

Page 6: Reserved Words

Special Words & Functions 6

Date

“Date” is a built-in function that returns the number (this must be a long integer) of days that have passed since December 31, 1899. Thus January 1, 1900 is day number 1 in this system and December 31, 1999 is day number 36525.

This is called “The Julian Date” and takes into account leap years and the Y2K problems.

Page 7: Reserved Words

Special Words & Functions 7

Date

In this system

Label1.Caption = Datewill insert today’s date as a label caption.

Dim dteY as datedteY = 365. * 100 + 100 / 4 + 1Label1.Caption = dteY

will insert 1/1/2000 into the label.(why did I put a decimal point after the 365?)

Page 8: Reserved Words

Special Words & Functions 8

Time

“Time” is a built-in function that returns the fraction of a day that has passed since midnight.

Thus at noon time returns a value of

0.50000000

and one minute later time returns a value of

0.5006944[.0006944 = 1/(24*60)]

Page 9: Reserved Words

Special Words & Functions 9

Time

If you wish to time some event, measure the time at the beginning of the event, then measure the time at the end of an event.

The difference is the fraction of a day that has passed between these two measurements.

This can be converted to more conventional units.

Page 10: Reserved Words

Special Words & Functions 10

Time

Option ExplicitDim sglX As Single

Private Sub cmdStart_Click()sglX = TimeEnd Sub

Private Sub cmdStop_Click()Label1.Caption = Format((Time - sglX) _ * 24 * 60 * 60, "0.00")End Sub

(there is an error here, what is it?)

Page 11: Reserved Words

Special Words & Functions 11

Rnd

The Rnd function returns a value between 0 and 1 that appears to be a random number.

It is not. It follows a complicated formula that obeys many of the rules of random numbers.

The exact same sequence is generated each time unless the Randomize function is called first.

Page 12: Reserved Words

Special Words & Functions 12

Rnd

Private Sub Form_Load()randomizeEnd Sub

Private Sub RollThem_Click()Label1.Caption = Int(rnd * 6 + 1)Label2.Caption = Int(rnd * 6 + 1)End Sub

(why did we add 1?}