Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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

2 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

3 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

4 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

5 CInt Function Dim X As Integer X = CInt(Text1.Text) Print X Dim X As Integer, Y As Integer Y = CInt(X) * 2 Print Y CInt(“12”) the brackets are by default - 12 CInt(“Twelve”) - Error CInt(Text1.Text) - the value of the Text in Text1, if it is valid

6

7

8 cHours = 12.2 cPayRate = 5 cTotalPay = ?

9 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

10 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?

11 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

12 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 OperatorExpressionStatement

13 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

14 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 statements SELECT CASE statements

15 Program Statements Evaluate an Expression Outcome AOutcome B

16 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] Then Action/Statements Else Action/Statements End If

17 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)

18 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 not equal to >= greater than or equal to <= less than or equal to

19 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 strings numeric values compared with numeric values whether a variable, constant, property of an object, or arithmetic expression

20 Dim Num1 as Integer If Num1 > 10 Then Print “Num1 is greater than 10” End If If Num1 > 10 Then Print “Num1 is greater than 10” Else Print “Num1 is less than 10” End If Val(Text1.Text)

21 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

22 If..Then..Else statement syntax: If [Condition/Expression] Then Action/Statements ElseIf [Condition/Expression] Then Action/Statements Else Action/Statements End If

23 Dim Temperature As Single If Temperature = 30 Then Print “Hot” Else Print “Moderate” End If

24 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

25 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’: AND OR NOT Compound conditions/expressions are created using Logical Operators Use compound conditions/expressions to test more than one condition

26 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

27 Dim StudentName As String Dim 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

28 Logical Operator Guidelines Result = Expression1 Logical Operator Expression2 If Expression1AND Expression2Then Result True TrueTrue True False False False True False False False False If Expression1OR Expression2Then Result True TrueTrue True False True False True True False False False

29 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

30 Nested If..Then statement syntax: (nested in the Then clause) If [Condition/Expression] Then If [Condition/Expression] Then Action/Statements Else Action/Statements End If Else Action/Statements End If

31 Nested If..Then statement syntax: (nested in the Else clause) If [Condition/Expression] Then Action/Statements Else If [Condition/Expression] Then Action/Statements Else Action/Statements End If End If

32 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

33

34

35

36 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 clause NESTED 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

37 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)

38 Private Sub Command1_Click If Option1.Value = True Then Print “Option1 Selected” ElseIf Option2.Value = True Then Print “Option2 Selected” ElseIf Option3.Value = True Then Print “Option3 Selected” ElseIf Option4.Value = True Then Print “Option4 Selected” Else Print “No Option Selected, Please Select Option” End If End Sub

39 Private Sub Command1_Click If Check1.Value = 1 Then Print “Check1 Selected” ElseIf Check2.Value = 1 Then Print “Check2 Selected” ElseIf Check3.Value = 1 Then Print “Check3 Selected” ElseIf Check4.Value = 1 Then Print “Check4 Selected” Else Print “Nothing Checked” End If End Sub


Download ppt "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."

Similar presentations


Ads by Google