Visual Basic IITG to be expanded What is
Visual Basic IITG to be expanded
What is Visual Basic? • Object Oriented Programming Language (OOP) • Graphical User Interface (GUI) • Event Driven – Write code for events you care about
Objects • Properties – Characteristics i. e. Color, size, etc. • Events – User’s actions • Methods – What object does – Always has parenthesis Ex: Object. Method() • Based on a class – Template • Declared like a variable • Use (. ) to access properties and variables – Ex: employee. salary = 100
Class • Contains definitions of: – Properties – Methods – Events • Creating class – Public Class name declare variables as Private or Public Property property. Name() As datatype End Property End Class
Procedures • Reusable code • Can be called from anywhere in the code • Sub Procedure – Performs a task • Event-handling – Executes in response to user event or occurrence in program • Function Procedure – Returns a value • Property Procedure – Return and assign values of properties
Data Types • Some basic data types – Boolean – Character – Integer – Long – Double (double precision floating-point) – Decimal – String – Object
Converting Between Numeric Data Types • Implicit – Automatic conversion – Narrower to wider data type • Explicit – Casting – Done manually within the code – Uses To method
Variables • Use Dim (dimensional) at the beginning • Declare data type with “As” then data type after variable – Ex: Dim a. Number As Integer • Declare multiple variables with commas (, ) – Ex: Dim a, b, c As Integer
Constants • Use Const (constant) at the beginning • Constant variable in uppercase • Declarations similar to other variables – Ex: Const TAX_RATE As Double • Used for set variables and cannot be changed
Arithmetic Operators • • Addition ( + ) Subtraction ( - ) Multiplication ( * ) Division ( / ) Exponentiation ( ^ ) Integer Division ( ) Modulus (MOD)
Comparison Operators • • Equality ( = ) Inequality ( <> ) Less than ( < ) Greater than ( > ) Less than or equal to ( <= ) Greater than or equal to ( >= ) These operators can be used for numbers or strings
Concatenation • Can combine multiple strings • Use “&” or the “+” operator • Put parentheses around strings – Ex: Dim x As String = “abc” + “def” + “ghi” Dim y As String = “jkl” & “mno” & “pqr” • Can also combine string variables – Ex: Dim a As String = “stu” Dim b As String = “vwx” Dim z As String = a + b Dim w As String = a & b
Logical Operators Negation (Not) Conjunction (And) Disjunction (Or) Exclusion (Xor) – Evaluates to False if both values are the same • Short-Circuit Conjunction (And. Also) – If first value is evaluated to false, then second value is skipped and whole expression is false • Short-Circuit Disjunction (or. Else) • • – If first value is evaluated to true, then second value is skipped and whole expression is true
Interface Design • Text Box – Allows for user input such as numbers, strings or characters • Group Box – Containers for other controls – Improves readability – Place group box in the form first then put controls inside • Check Box – Select/deselect one or more item in a group • Radio Buttion – Select only one in a group • Picture Box – Contains and displays a picture
Converting Strings to Numbers • Needed when using values from text properties • User data inputted as strings • Must be converted to use in calculations • Use Parse methods – Data type then dot (. ) then Parse with object and data type in parentheses – Ex: Integer. Parse(quantity. Text. Box. Text)
Converting to Strings and Displaying Numbers • Must convert to string to display in text box • All values outputted as strings • Use To. String method – Ex: result = sum. To. String() • Parenthesis are optional – To output to screen use: • text. Box. Name. Text = string. Variable • Ex: result. Text. Box. Text = result
Loops • For…Next loops – Executes for a specific number of iterations • Dim counter As Integer For counter = starting. Value to ending. Value Step 1 If counter = 5 Then Exit For messagebox. Show(“Counter = “ + CStr(counter)) Next counter – Step only necessary when counting by something other than 1 – If statement needed for early exit of For loop
Loops (continued) • For Each…Next loops – Similar to For…Next loops – Executes for each element in an array – For Each element As datatype in array. Name Statement Next element • “As datatype” is optional • element in “Next element” is optional
Loops (continued) • While loops – Uses true/false condition – Used when unsure of number of iterations – Dim Number As Integer = 10 While Number > 6 Number = Number – 1 End While • Terminating statement is needed
Loops (continued) • Do…Loop – Similar to While loops – Do While Number > 6 Number = Number - 1 Loop
If…Then…Else • Make decision based on Boolean decision • If condition Then statements Else statements End If
Select…Case • • • Used when comparing expression to several different values Evaluates expression once and uses it for every comparison Dim number As Integer = 6 Select Case number Case 1 statement Case 2 statement Case 3, 4 statement Case 5 To 8 statement Case Else‘Optional statement End Select
Handling Exceptions • Run-time errors • Uses Try/Catch statements – Finally is optional • Example: – Try statement of possible error Catch [Variable. Name As Exception. Type] action for exception [Finally code that executes with or without error] End Try
Handling Exceptions (Continued) • Last Catch statement should be generic • Use with statements that may cause error • If there is an exception while in Try block, control is transferred to Catch block • Exceptions are instances of exception class • Multiple Catch statements • Nested Try/Catch statements
With Statement • Used to address multiple functions of the same object • With first. Number. Text. Focus(). Select. All() End With
Setting Tab Order for Controls • One control always has the focus • Not all controls can have the focus – i. e. labels and picture boxes • Set Tab. Stop property to true or false • Set Tab. Index property starting with zero
Keyboard Access Keys • AKA Hot Keys • User presses Alt and the underlined letter of a command – Ex: Alt + x = Exit • To create a hot key, place an ampersand before the letter you wish to use in the Text property of the object – Ex: E&xit
Keyboard Access Keys (Continued) • To set hot keys to labels and picture boxes: – Set Tab. Index of label to one less than Tab. Index of corresponding control • Ex: Tab. Index of First Number is 0 while Tab. Index of first. Number. Text. Box is 1
Creating and Writing Text Files • Use File. Stream class • Creating/Writing a text file – Declare Stream. Writer object • Dim sw As Stream. Writer = New _ Stream. Writer("Test. File. txt") – Use write method • sw. Write(“An Example”) – Close file • sw. Close()
Reading from a Text File • Declaring Stream. Reader object – Dim sr As Stream. Reader = New _ Stream. Reader("Test. File. txt") • Reading file and displaying – Do line = sr. Read. Line() Console. Write. Line(Line) Loop Until line Is Nothing • Close file – sr. Close()
Other Useful Info. • Line continuation character – Used for long program lines – Space after last character, underscore, then enter to go onto next line – Ex: If (x<y) Or _ (x>y) • Option Strict – Restricts implicit conversions to only widening conversions – Helps prevent loss of data and errors – At top of code type “Option Strict On”
Other Useful Info. (Continued) • Focus method – To set focus on a particular text box – Ex: first. Number. Text. Focus() • Select. All method – To select and highlight a text box – Ex: first. Number. Text. Select. All() • Clear method – Used to clear text boxes – Ex: first. Number. Text. Clear() • End method – Used to end program – Ex: End
Review Questions • What is the purpose of the Try/Catch statements? • What’s special about VB? • Can numbers be converted into strings and vice versa? If so, how?
- Slides: 33