E N D
Val FunctionA Function performs anaction and returns a valueThe expression to operate upon, known as the argument, (or multiple arguments), must be enclosed in parenthesesVal an abbreviation for valueVal(ExpressionToConvert)The expression that is converted, can be the Property of a Control, a Variable, or a Constant
The Val Function returns (produces) a value that can be used as a part of a statement, such as an assignment statementiQuantity = Val(txtQuantity.Text)The Val Function converts an argument to numeric, by beginning at the Left-Most CharacterIf 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
Conversion FunctionThe 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
VB Conversion FunctionsCInt - 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
CInt FunctionDim X As IntegerX = CInt(Text1.Text)Print XDim X As Integer, Y As IntegerY = CInt(X) * 2Print YCInt(“12”) the brackets are by default - 12CInt(“Twelve”) - ErrorCInt(Text1.Text) - the value of the Text in Text1, if it is valid
cHours = 12.2 cPayRate = 5 cTotalPay = ?
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
Hours.Text = 12.2 PayRate.Text = 5 Using the Val Function and the CCur Function,after multiplication, returns the value of 61 However, if the value of Hours.Text = 12.2’ How would the Functions handle it?
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 CalculationThe 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
Basic Elements of ProgrammingA VB program is built from statements, statements from expressions, expressions from operators and operands, and operands from variables/constants andthe properties of objects Operand Operator Expression Statement Operand
Programming ConstructsVB code is generally comprised of combinations of the following program statementsSequence:Consisting of a number of instructions which are processed in sequenceSelection:Consisting of branches in the VB program, containing different instructions which are processed depending on the results of certain tested conditionsIteration:Consisting of groups of statements which are repeatedly executed until a certain tested condition is satisfied
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 executesThere are 2 main types of Branching Construct in VB:IF statementsSELECT CASE statements
Program Statements Program Statements Evaluate an Expression Outcome A Outcome B Program Statements Program Statements
If StatementsProjects can take one action or another, based on a conditionMake a decision and take alternate courses of action based on the outcome If..Then statement syntax:If [Condition/Expression] ThenAction/StatementsElseAction/StatementsEnd If
The word Then must appear on the same line as IfElse and End If must appear on separate linesThe 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) OperatorsThese Relational (Comparison) Operators are used to compare expressions, and return a result of either True or False (Boolean Data Type)
The test of an If statement is based on a conditionTo form conditions, Relational(Comparison) Operatorsare 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
Conditions can be formed with- numeric variables and constants- string variables and constants- object properties, and- arithmetic expressionsHowever, comparisons have to be made on likedata typesstrings compared to stringsnumeric values compared with numeric valueswhether a variable, constant, property of an object, or arithmetic expression
Dim Num1 as IntegerIf Num1 > 10 ThenPrint “Num1 is greater than 10”End IfIf Num1 > 10 ThenPrint “Num1 is greater than 10”ElsePrint “Num1 is less than 10”End If Val(Text1.Text) Val(Text1.Text)
Multiple BranchingThe logic of a program may require that there be more than one branch in the programIn this case the ElseIf keyword(clause) is added to the If..Then statement to increase flexibilityAn infinite amount of ElseIf statements can be included into the If..Then statement
If..Then..Else statement syntax:If [Condition/Expression] ThenAction/StatementsElseIf [Condition/Expression] ThenAction/StatementsElseAction/StatementsEnd If
Dim Temperature As SingleIf Temperature <= 0 ThenPrint “Freezing”ElseIf Temperature >= 30 ThenPrint “Hot”ElsePrint “Moderate”End If
If..Then..Else statements have a definite hierarchyThe 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 ElseIfstatements will be processed/executedIf..Then statements can be given greater flexibility in 2 main ways:[1] Using Logical Operators [2] Nesting If..Then statements
Logical Operators Logical Operators should be used when a limited number of conditions are to be testedThe 3 most commonly used Logical Operators in VB are in ‘Order of Precedence’:ANDORNOTCompound conditions/expressions are created using Logical OperatorsUse compound conditions/expressions to test more than one condition
AND Both conditions must be true for the entire condition to be trueOR If one condition or both conditions are true, the entire condition is trueNOT Reverses the condition, so that a true condition will evaluate false and vice versaThe use of parentheses can change the ‘Order of Precedence’ of these Logical OperatorsAlways plan the use of Logical Operators, as their use can often involve confusing logic
Dim StudentName As String Dim Age As IntegerIf StudentName = “Dave” ANDAge >= 23 ThenIf StudentName = “Dave” ORAge >= 23 Then If StudentName = “Dave” AND NOTAge <= 23 Then
Logical Operator GuidelinesResult = Expression1 Logical Operator Expression2If Expression1ANDExpression2 Then ResultTrue True True True False False False True False False False FalseIf Expression1ORExpression2 Then ResultTrue True True True False True False True True False False False
Nesting If..Then StatementsIf..Then statements that contain additional If..Then statements are said to be nested If statementsIf more than one Expression has to be checked in the program, If..Then statements can be nestedYou may nest If..Then statements in both the Then and Else portion of the statement syntaxYou can continue to nest If..Then statements within If..Then statements as long as each Ifhas an End If
Nested If..Then statement syntax:(nested in the Then clause)If [Condition/Expression] Then If [Condition/Expression] ThenAction/StatementsElseAction/Statements End IfElseAction/StatementsEnd If
Nested If..Then statement syntax: (nested in the Else clause)If [Condition/Expression] ThenAction/StatementsElse If [Condition/Expression] ThenAction/StatementsElseAction/Statements End If End If
The first If..Then statement which the program encounters is called the OUTER If..Then statementAny If..Then statements placed within the first statement are called INNER If..Then statementsINNER If..Then statements are onlyprocessed when the OUTER If..Then statement is TrueNested If..Then statements have to be carefully structuredThere is a definite Order/Hierarchy in relation to the way in which they are processed
If..Then statementsIf..Then statements check the value of an expression and carry outdifferent instructions basedon the resultIf..Then statements are a useful and flexible method of allowing a program to branch into different directionsIf..Then statements become more powerful and flexible through the use of:the ELSEIF clauseNESTED IF..THEN statements, andLOGICAL OPERATORSIf..Then statements must be carefully structured as there is a definite order in which the conditions are tested
Using If..Then statements with Option Buttons andCheck BoxesIn using If..Then statements, with Option Buttons and Check Boxes, no action should be taken in the click events for these controlsCode 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)
Private Sub Command1_ClickIf 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 IfEnd Sub
Private Sub Command1_ClickIf 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 IfEnd Sub