Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic IITG to be expanded. What is Visual Basic? Object Oriented Programming Language (OOP) Graphical User Interface (GUI) Event Driven – Write.

Similar presentations


Presentation on theme: "Visual Basic IITG to be expanded. What is Visual Basic? Object Oriented Programming Language (OOP) Graphical User Interface (GUI) Event Driven – Write."— Presentation transcript:

1 Visual Basic IITG to be expanded

2 What is Visual Basic? Object Oriented Programming Language (OOP) Graphical User Interface (GUI) Event Driven – Write code for events you care about

3 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

4 Class Contains definitions of: – Properties – Methods – Events Creating class – Public Class name declare variables as Private or Public Property propertyName() As datatype End Property End Class

5 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

6 Data Types Some basic data types – Boolean – Character – Integer – Long – Double (double precision floating-point) – Decimal – String – Object

7 Converting Between Numeric Data Types Implicit – Automatic conversion – Narrower to wider data type Explicit – Casting – Done manually within the code – Uses To method

8 Variables Use Dim (dimensional) at the beginning Declare data type with “As” then data type after variable – Ex: Dim aNumber As Integer Declare multiple variables with commas (,) – Ex: Dim a, b, c As Integer

9 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

10 Arithmetic Operators Addition ( + ) Subtraction ( - ) Multiplication ( * ) Division ( / ) Exponentiation ( ^ ) Integer Division ( \ ) Modulus (MOD)

11 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

12 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

13 Logical Operators Negation (Not) Conjunction (And) Disjunction (Or) Exclusion (Xor) – Evaluates to False if both values are the same Short-Circuit Conjunction (AndAlso) – If first value is evaluated to false, then second value is skipped and whole expression is false Short-Circuit Disjunction (orElse) – If first value is evaluated to true, then second value is skipped and whole expression is true

14 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

15 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(quantityTextBox.Text)

16 Converting to Strings and Displaying Numbers Must convert to string to display in text box All values outputted as strings Use ToString method – Ex: result = sum.ToString() Parenthesis are optional – To output to screen use: textBoxName.Text = stringVariable Ex: resultTextBox.Text = result

17 Loops For…Next loops – Executes for a specific number of iterations Dim counter As Integer For counter = startingValue to endingValue 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

18 Loops (continued) For Each…Next loops – Similar to For…Next loops – Executes for each element in an array – For Each element As datatype in arrayName Statement Next element “As datatype” is optional element in “Next element” is optional

19 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

20 Loops (continued) Do…Loop – Similar to While loops – Do While Number > 6 Number = Number - 1 Loop

21 If…Then…Else Make decision based on Boolean decision If condition Then statements Else statements End If

22 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

23 Handling Exceptions Run-time errors Uses Try/Catch statements – Finally is optional Example: – Try statement of possible error Catch [VariableName As ExceptionType] action for exception [Finally code that executes with or without error] End Try

24 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

25 With Statement Used to address multiple functions of the same object With firstNumberText.Focus().SelectAll() End With

26 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 TabStop property to true or false Set TabIndex property starting with zero

27 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

28 Keyboard Access Keys (Continued) To set hot keys to labels and picture boxes: – Set TabIndex of label to one less than TabIndex of corresponding control Ex: TabIndex of First Number is 0 while TabIndex of firstNumberTextBox is 1

29 Creating and Writing Text Files Use FileStream class Creating/Writing a text file – Declare StreamWriter object Dim sw As StreamWriter = New _ StreamWriter("TestFile.txt") – Use write method sw.Write(“An Example”) – Close file sw.Close()

30 Reading from a Text File Declaring StreamReader object – Dim sr As StreamReader = New _ StreamReader("TestFile.txt") Reading file and displaying – Do line = sr.ReadLine() Console.WriteLine(Line) Loop Until line Is Nothing Close file – sr.Close()

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

32 Other Useful Info. (Continued) Focus method – To set focus on a particular text box – Ex: firstNumberText.Focus() SelectAll method – To select and highlight a text box – Ex: firstNumberText.SelectAll() Clear method – Used to clear text boxes – Ex: firstNumberText.Clear() End method – Used to end program – Ex: End

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


Download ppt "Visual Basic IITG to be expanded. What is Visual Basic? Object Oriented Programming Language (OOP) Graphical User Interface (GUI) Event Driven – Write."

Similar presentations


Ads by Google