39
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be enclosed in parentheses Val an abbreviation for value Val(ExpressionToConvert) The expression that is converted, can be the Property of a Control, a Variable, or a Constant

Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

  • View
    223

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Val Function

A Function performs an action and returns a value

The expression to operate upon, known as the argument, (or multiple arguments), must be enclosed in

parentheses

Val an abbreviation for value

Val(ExpressionToConvert)

The expression that is converted, can be the Property of a Control, a Variable, or a Constant

Page 2: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

The Val Function returns (produces) a value that can be used as a part of a statement, such as an

assignment statement

iQuantity = Val(txtQuantity.Text)

The Val Function converts an argument to numeric, by beginning at the Left-Most Character

If that character is a numeric digit, decimal point, or sign, Val converts the character to numeric and moves to the next character. As soon as a non-numeric character is found, the operation stops

Page 3: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Conversion Function

The Conversion Function checks if the value stored in the Property of a Control or a Variable is of a specific Data Type required in the execution of a statement(s), and if not the Conversion Function

will return a ‘Run-Time Error’ Message

The Text in Text Boxes is treated as a String, however in performing Arithmetic Operations

(such as ^ * / + -), numeric values are required in the Text property of the Text Boxes

Page 4: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

VB Conversion Functions

CInt - Converts a value to an Integer CLng - Converts a value to a Long Integer

CSng - Converts a value to a Single Precision number

CDbl - Converts a value to a Double Precision nember

CCur - Converts a value to Currency CStr - Converts a value to a String

CVar - Converts a value to a Variant

Page 5: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

CInt Function

Dim X As IntegerX = CInt(Text1.Text)

Print X

Dim X As Integer, Y As IntegerY = CInt(X) * 2

Print Y

CInt(“12”) the brackets are by default - 12CInt(“Twelve”) - Error

CInt(Text1.Text) - the value of the Text in Text1, if it is valid

Page 6: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be
Page 7: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be
Page 8: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

cHours = 12.2

cPayRate = 5

cTotalPay = ?

Page 9: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

In the Sample code the Val Function is replaced by the CCur Function

The value stored in the Text Property of the Hours text box is converted from a String to Currency and the value is assigned to the Variable cHours as a Currency Data Type

If the value in the Text Property is not consistent with the Currency Data Type, then a ‘Run-Time Error’ will occur and the program will stop executing

Page 10: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Using the Val Function and the CCur Function, after multiplication, returns the value of 61

Hours.Text = 12.2

PayRate.Text = 5

However, if the value of Hours.Text = 12.2’

How would the Functions handle it?

Page 11: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

The Val Function will convert the value stored in the Property of the control, to a numeric value, before

assigning it as the value of the variable for use in an Arithmetic Calculation

The Conversion Function checks if the value stored in the Property of a Control is of a specific

Data Type required in the execution of a statement(s) (numeric for the purpose of

calculation), and if not the Conversion Function will return a ‘Run-Time Error’ Message

Page 12: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Basic Elements of Programming

A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands from variables/constants

and the properties of objects

Operand

Operand

Operator Expression Statement

Page 13: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Programming Constructs

VB code is generally comprised of combinations of the following program statements

Sequence: Consisting of a number of instructions which are processed in sequence

Selection: Consisting of branches in the VB program, containing different instructions which are

processed depending on the results of certain tested conditions

Iteration: Consisting of groups of statements which are repeatedly executed until a certain

tested condition is satisfied

Page 14: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Selection (Branching Constructs in VB)

Branching constructs are used to control program flow

A Condition/Expression is evaluated, and the result determines which statements the program

executes

There are 2 main types of Branching Construct in VB:

IF statementsSELECT CASE statements

Page 15: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Program Statements

Program Statements

Program Statements

Program Statements

Evaluate an

Expression

Outcome A Outcome B

Page 16: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

If Statements

Projects can take one action or another, based on a condition

Make a decision and take alternate courses of action based on the outcome

If..Then statement syntax:

If [Condition/Expression] ThenAction/Statements

ElseAction/Statements

End If

Page 17: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

The word Then must appear on the same line as If

Else and End If must appear on separate lines

The statements underneath the Then and Else clause are indented for ‘readability’ and ‘clarity’ (always indent

code, especially with If statements, the indentation helps to visualise the intended logic and saves on

project debugging time)

If..Then statements are generally used in conjunction with the Relational (Comparison) Operators

These Relational (Comparison) Operators are used to compare expressions, and return a result of either

True or False (Boolean Data Type)

Page 18: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

The test of an If statement is based on a condition

To form conditions, Relational(Comparison) Operators are used, resulting in an outcome being

either ‘True’ or ‘False’ (Boolean Data Type)

There are 6 Relational(Comparison) Operators in VB:

Order of Precedence of Relational Operators> greater than

< less than= equal to

<> not equal to>= greater than or equal to

<= less than or equal to

Page 19: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Conditions can be formed with

- numeric variables and constants- string variables and constants

- object properties, and- arithmetic expressions

However, comparisons have to be made on like data types

strings compared to stringsnumeric values compared with numeric values

whether a variable, constant, property of an object, or arithmetic expression

Page 20: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Dim Num1 as Integer

If Num1 > 10 ThenPrint “Num1 is greater than 10”

End If

If Num1 > 10 ThenPrint “Num1 is greater than 10”

ElsePrint “Num1 is less than 10”

End If

Val(Text1.Text)

Val(Text1.Text)

Page 21: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Multiple Branching

The logic of a program may require that there be more than one branch in the program

In this case the ElseIf keyword(clause) is added to the If..Then statement to increase flexibility

An infinite amount of ElseIf statements can be included into the If..Then statement

Page 22: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

If..Then..Else statement syntax:

If [Condition/Expression] ThenAction/Statements

ElseIf [Condition/Expression] ThenAction/Statements

ElseAction/Statements

End If

Page 23: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Dim Temperature As Single

If Temperature <= 0 ThenPrint “Freezing”

ElseIf Temperature >= 30 ThenPrint “Hot”

ElsePrint “Moderate”

End If

Page 24: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

If..Then..Else statements have a definite hierarchy

The order of an If..Then..Else statement is important, due to the fact that if the first line of the If..Then..Else

statement is ‘True’, then none of the other ElseIf statements will be processed/executed

If..Then statements can be given greater flexibility in 2 main ways:

[1] Using Logical Operators [2] Nesting If..Then statements

Page 25: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Logical Operators

Logical Operators should be used when a limited number of conditions are to be tested

The 3 most commonly used Logical Operators in VB are in ‘Order of Precedence’:

ANDOR

NOT

Compound conditions/expressions are created using Logical Operators

Use compound conditions/expressions to test

more than one condition

Page 26: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

AND Both conditions must be true for the entire condition to be true

OR If one condition or both conditions are true, the entire condition is true

NOT Reverses the condition, so that a true condition will evaluate false and vice versa

The use of parentheses can change the ‘Order of Precedence’ of these Logical Operators

Always plan the use of Logical Operators, as their use can often involve confusing logic

Page 27: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Dim StudentName As StringDim Age As Integer

If StudentName = “Dave” AND Age >= 23 Then

If StudentName = “Dave” OR Age >= 23 Then

If StudentName = “Dave” AND NOT Age <= 23 Then

Page 28: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Logical Operator Guidelines

Result = Expression1 Logical Operator Expression2

If Expression1 AND Expression2 Then Result True True True

True False False False True False False False False

If Expression1 OR Expression2 Then Result True True True True False True False True True False False False

Page 29: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Nesting If..Then Statements

If..Then statements that contain additional If..Then statements are said to be nested If statements

If more than one Expression has to be checked in the program, If..Then statements can be nested

You may nest If..Then statements in both the Then and Else portion of the statement syntax

You can continue to nest If..Then statements within If..Then statements as long as each If has an End If

Page 30: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Nested If..Then statement syntax: (nested in the Then clause)

If [Condition/Expression] ThenIf [Condition/Expression] Then

Action/StatementsElse

Action/StatementsEnd If

ElseAction/Statements

End If

Page 31: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Nested If..Then statement syntax: (nested in the Else clause)

If [Condition/Expression] ThenAction/Statements

ElseIf [Condition/Expression] Then

Action/StatementsElse

Action/StatementsEnd If

End If

Page 32: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

The first If..Then statement which the program encounters is called the OUTER If..Then statement

Any If..Then statements placed within the first statement are called INNER If..Then statements

INNER If..Then statements are only processed when the OUTER If..Then statement is True

Nested If..Then statements have to be carefully structured

There is a definite Order/Hierarchy in relation to the way in which they are processed

Page 33: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be
Page 34: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be
Page 35: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be
Page 36: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

If..Then statements

If..Then statements check the value of an expression and carry out different instructions based on the result

If..Then statements are a useful and flexible method of allowing a program to branch into different directions

If..Then statements become more powerful and flexible through the use of:

the ELSEIF clauseNESTED IF..THEN statements, and

LOGICAL OPERATORS

If..Then statements must be carefully structured as there is a definite order in which the conditions are

tested

Page 37: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Using If..Then statements with Option Buttons and Check Boxes

In using If..Then statements, with Option Buttons and Check Boxes, no action should be taken in the

click events for these controls

Code should be written in the Click_Event of a Command Button, where certain actions will be performed when the command button is clicked,

relating to the selection of an option button (value property of the option button), or the checking of a

check box (value property of the check box)

Page 38: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Private Sub Command1_Click

If Option1.Value = True ThenPrint “Option1 Selected”

ElseIf Option2.Value = True ThenPrint “Option2 Selected”

ElseIf Option3.Value = True ThenPrint “Option3 Selected”

ElseIf Option4.Value = True ThenPrint “Option4 Selected”

ElsePrint “No Option Selected, Please Select Option”

End If

End Sub

Page 39: Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be

Private Sub Command1_Click

If Check1.Value = 1 ThenPrint “Check1 Selected”

ElseIf Check2.Value = 1 ThenPrint “Check2 Selected”

ElseIf Check3.Value = 1 ThenPrint “Check3 Selected”

ElseIf Check4.Value = 1 ThenPrint “Check4 Selected”

ElsePrint “Nothing Checked”

End If

End Sub