Monday, 8 August 2011

Learning about Parameters

0 comments
 
Parameters are variables that being passed to a Sub. Look at the first line of the Command Button's Click event:


Private Sub Command1_Click()

And at the first line of the Command Button's KeyPress event:

Private Sub Command1_KeyPress(KeyAscii As Integer)


The Click event's first line is ended with
empty parentheses   ()   and the KeyPress event's first
line is ended with  (KeyAscii As Integer)


What is the   (KeyAscii As Integer)   ?
It's a parameter that been passed to the KeyPress event.

This parameter is an Integer variable with the name KeyAscii.
Like if you've declared     Dim KeyAscii As Integer

Why do we need this variable?
Because its value is very useful.

The KeyPress event is being executed when the user
press a key, and This variable holds the Ascii value of the
key that been pressed.
With This Ascii value you can know on which key
the user has pressed.

For example, the Ascii value of the "A" character is 65.
If the user has pressed the key "A" on the keyboard,
The KeyAscii parameter value will be 65.

Lets see an example.
Insert the following line to the Command1 KeyPress event:

Private Sub Command1_KeyPress(KeyAscii As Integer)
    Print KeyAscii
End Sub

Run the program and press several keys.
You will see the Ascii value of every key you're pressing.

Notice that the KeyAscii values of "A" and "a" are differents.
Every characters has its own KeyAscii value,
and 2 characters that are the same letter, but have different case,
have different KeyAscii value.


How can I know what is the Ascii value of a specific character?
Use the Asc command.
For example, the following line:

Print Asc("b")

Will print on the form the Ascii value of the character "b".

How can I know which character's Ascii value is 98?
Use the Chr command.
For example, the following line:

Print Chr(98)

Will print on the form the character that its Ascii value is 98.

Look at the first line of the Command Button's KeyDown Event:

Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)

As you can see, the KeyDown event gets two parameters:
The KeyCode parameter, and the Shift parameter, both are
Integer type.

Every Sub can get no parameters (like the Click event), can get
one parameter (like the KeyPress event) or can get more than
one parameter.

If a sub is getting more than one parameter, the parameters
have to be separated with commas.
For example:

Private Sub MySubName (Parameter1 As String, Parameter2 As Integer, Parameter3 As String)

Lets check out the KeyDown event's parameters.

The first is KeyCode, and it holds the KeyCode value
of the pressed key.
The KeyCode value is usually different from the Ascii Value.

The different between KeyCode and Ascii, is that
every character has Ascii value (for example G, @, |, =, and more)
but there are some keys that don't represent any character, for example:
Alt, F4, Ctrl, The left arrow key.

These keys don't have Ascii value, but they have KeyCode value.
For example, the KeyCode value of the Ctrl Key is 17.

Ascii represent characters, while KeyCode represent Keyboard's keys.
Because of that, the characters "a" and "A" have different Ascii value,
but they have the same KeyCode value, because the same key
is typing "a" and "A".


The second parameter is Shift, and its value helps you
to determine if the user has pressed the Shift, Ctrl or Alt keys.
The Shift holds the value 1 if the user has pressed the Shift key,
the value 2 if the user has pressed the Ctrl Key,
and the value 4 if the user has pressed the Alt Key.

If the user has pressed the Alt key and the Shift key together,
the Shift value will be 4 (for the Alt key) + 1 (for the Shift key) = 5

Examples:

If the user has pressed the "A" key,
The KeyCode parameter will hold the number 65, and
the Shift parameter will hold the value 0.

If the user has pressed Shift and "A" together,
The KeyCode parameter will hold the number 65, and
the Shift parameter will hold the value 1.


The KeyUp event's parameters are the same as the KeyDown event's parameters.

Leave a Reply