‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.

Slides:



Advertisements
Similar presentations
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 10 Introduction to Arrays
1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier.
VBA Modules, Functions, Variables, and Constants
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
PSU CS 106 Computing Fundamentals II VB Statements HM 5/19/2008.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Lec4 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 4 Back To Index v Iteration - Looping v Arrays.
VB .NET Programming Fundamentals
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
IE 212: Computational Methods for Industrial Engineering
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Using Arrays and File Handling
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Group of variables that have a similar type
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
Chapter Six: Working With Arrays in Visual Basic.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
ISAT 252 Visual Basic Repetition. Assignment Should have read Chapter 5 ( ) on loops Do tutorial 5-4.
Arrays Chapter 8. Chapter 8 - Part 12 Variable and Variable Array Variable Stores only one value Variable Array Variable that has 1 symbolic name but.
Tutorial 6 The Repetition Structure
Visual Basic Programming
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
‘Tirgul’ # 2 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
Chapter 7 - Lists, loops and printing w List boxes and combo boxes several types can add items at design time or during run time user select from predefined.
Visual Basic Programming I 56:150 Information System Design.
CompMathBSc, English 5 October 2006 Programming basics — continued  Arrays  Cycle Statements: Loops  Control Structures vs Conditions  Subs: Procedures.
CHAPTER 9 PART II. MULTIDIMENSIONAL ARRAYS Used to represent tables of values arranged in rows and columns. Table element requires two indexes: row and.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
31/01/ Selection If selection construct.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
Controlling Program Flow with Looping Structures
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
VBA - Excel VBA is Visual Basic for Applications
Starting Out with Programming Logic & Design
Arrays.
Introduction to Computer Programming IT-104
Presentation transcript:

‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3

‘Tirgul’ # 3 Short Quiz Write a simple Function that receives a String and returns its length Write a Simple Function that receive an Integer, if it is negative, returns a String with error message, otherwise returns the Integer as a String

‘Tirgul’ # 3 Objectives Parameters passing Getting Deeper –If – Then – Else –Case –Loops Arrays –Dimension arrays correctly –Recognize the default values of array elements –Access array items by subscript or all elements in a loop –Use parallel arrays for storage of related lists of items –Use list boxes for displaying choices –Use 2-D arrays

‘Tirgul’ # 3 Review: important concepts Remember to plan your programs (algorithm) before starting to write the code! (80:20) Use procedures and functions –parameters must be carefully set –Use ByRef and ByVal Fundamental Structures: –Sequence, Selection, and Iteration

‘Tirgul’ # 3 Parameter Passing By default, variables are passed ByRef –called procedure uses same memory location for the variable –assigns a new name for that location, uses that name within the procedure –contents of this location may be changed Sub Add_And_Change (ByRef x as Single, ByRef y as Single) x = x + y End Sub

‘Tirgul’ # 3 Parameter passing Parameters can also be passed ByVal –called procedure sets up new memory locations –value of parameter is copied into the new locations –changes do not affect original variable Sub Add_And_Print (ByVal x as Single, ByVal y as Single) x = x + y picOut.print x End Sub

Example What will be printed out? What if we called Sample2 (b, a) ? Sub Sample1 (ByRef x as Single, ByRef y as Single) x = x - y picOut x, y End Sub Sub Sample2 (ByVal x as Single, ByVal y as Single) x = x - y picOut x, y End Sub Sub cmdCompute Dim a, b as Single a = 2 b = 3 Sample2 (a, b) picOut1.Print a, b Sample1 (a, b) picOut.Print a, b End Sub

‘Tirgul’ # 3 Nested IF statements If condition1 Then If condition2 Then statement1 End If Else statement2 End if

‘Tirgul’ # 3 Example If Grade >= 90 Then txtLetterGrade.Text = “ A ” Else If Grade >=80 Then txtLetterGrade.Text = “ B ” Else txtLetterGrade.Text = “ C ” End If

‘Tirgul’ # 3 Example If Income >= Then If Status = “ Single ” Then TaxRate = 0.33 ElseIf Status = “ Married ” Then TaxRate = 0.25 End If Else sTaxRate =.15 End If Less end if’s

‘Tirgul’ # 3 If statement summary VB is sensitive at design-time and run- time Indentation! Use if… Then Use end if Use elseif Sub Sample_if () If a = b then … End if End Sub Sub Sample_if2 () If a = b then … elseif … End if End Sub Sub Sample_if2 () If a = b then … else if a > b then … end if End if End Sub

‘Tirgul’ # 3 Select Case Structure Multiple IF statements can be replaced by more readable Select CASE statements Select Case selector Case valueList1 action1 Case valueList2 action 2 … Case Else if no other match End Select u valueList options: Case 1 Case 2 to 5 Case 6, 9 Case “text” Case Is >= 10

‘Tirgul’ # 3 Example Select case AccessCode case is < 1000 message = “ Access Denied ” case 1465 To 1689 message = “ Technical Personal ” case , To message = “ Scientific Personal “ case Else message = “ Access Denied ” End Select

‘Tirgul’ # 3 Case statement summary Use String/Integer type When using String pay attention to content! Don’t forget default

‘Tirgul’ # 3 Do / Loops Format 2: Do loop body Loop {While | Until} Condition Format 1: Do {While | Until} Condition loop body Loop Loop body executes while the condition is true or until the condition is true The first form uses a pretest, the second uses a posttest. With a pretest, loop may not execute at all. With a posttest, loop always executes at least once.

‘Tirgul’ # 3 Do While Example Do While sTotal < 25 sNum = Val(Inputbox(“Enter a number”)) If sNum > 0 then sTotal = sTotal + sqr(sNum) End If Loop

‘Tirgul’ # 3 Choosing Loop Type For..Next loop should be used when you know the number of loops Do..While is used when you do not know in advance the number of iteration. –Examples You want to get user input until she hits the escape button You want your server to keep listening for request until it receives a shut down message.

‘Tirgul’ # 3 Loops summary For – next While – wend Do – while Common mistakes –Stop condition –Increment Exit loop Do For I = 1 To 1000 MyNum = Int(Rnd * 100) Select Case MyNum Case 7: Exit For Case 29: Exit Do Case 54: Exit Sub End Select Next I Loop While true … index = index +1 Wend Danger

‘Tirgul’ # 3 Arrays Array – Set of elements of the same type indexed in a data structure Each array object is called an element Each element is identified with an Index

‘Tirgul’ # 3 Why use arrays? Often, all data must be read in and stored for future use. Not always possible to get each value and process it, it is better to get all data, store it in an array and process it Use of data structures is preferred and unavoidable.

‘Tirgul’ # 3 Declaration Array name - identifier Array type – type of elements in the array Array range – how many elements class(1)class(30) Dim class(1 to 10) as students

‘Tirgul’ # 3 Subscripts Recall – Array starts at 1 BUT: Array of size 25 subscripts may be constants, variables, or numeric expressions Use Constants!!! Dim GradeArr(MAX_STUDENTS) as Single Dim GradeArr(0 to 29) as Single

‘Tirgul’ # 3 Array Value set Dim GradeArr(1 to MAX_STUDENTS) as Single GradeArr(1) = 95 for index = 2 to 10 GradeArr(index) = 100 next index GradeArr(11) = (sGrade(1) + sGrade(2) )/2 GradeArr(30) = ?

‘Tirgul’ # 324 Array bounds Ubound, Lbound will return the array declared size (NOT elements) To prevent errors, check Ubound for highest index Common use to iterate the array: for index = Lbound(GradeArr) to Ubound(GradeArr) … next index

‘Tirgul’ # 325 Declaring Dynamic Array An Array declaration at the module level – No memory location yet: Memory allocation at sub/function level: Dim DBConnections() as Connection Public DBRecordSet() as RecordSet Sub openConnection ()... ReDim DBConnections(1 To MAX_CONNECTIONS) End sub

‘Tirgul’ # 326 Redim Resizing an existing array Procedure level allocation Deallocate memory (free memory) Array values are lost at Redim! Use preserve to keep old values ReDim preserve newArray(MAX_ARRAY_SIZE + 1) Redim array(0)

‘Tirgul’ # 3 For Each…Next Statement Use For Each…Next Iteration to iterate al array elements Use variant type variable if you don’t know element type. Dim day as Variant For each day in week print day Next day

‘Tirgul’ # 3 Split - Join Split – Namely split a String to the array Join – Namely create a String from array dim wordsArr()as String dim Sentence as string Sentence = “ good morning ” wordsArr = Split(Sentence, “ “ ) Sentence = join(wordsArr, “ “ ) 1 2 Good Morning wordsArr Sentence “ good morning ” Delimiter

‘Tirgul’ # 3 Two-Dimensional Arrays Think of a table structure use two indexes –row index –column index all elements must be of the same type

‘Tirgul’ # 3 Examples Dim iArray (1 to 2, 1 to 5) as Integer array of 2 rows and 5 columns Col Row

‘Tirgul’ # 3 Accessing Array Elements iArray (1, 2) = 10 iArray (2, 5) = iArray(1, 4) + iArray(1, 5) I = 7 : J = 5 sGrades (I, J) = 93 x = 1 : y = 2 iArray (1, x + y) = 15

‘Tirgul’ # 3 Practical - Combo Boxes Combo box represent a list of items to select from. Items can be specified during Design/Run time Uses: –Inset Item –Delete Item –Get selected Item

‘Tirgul’ # 3 Code sample for Combo Box ListIndex represents the selected Index Text represents the item String value Remove selected item Add items to list cmbDays.RemoveItem cmbDays.ListIndex cmbDays.AddItem “ Sunday ” Debug.print cmbDays.text

‘Tirgul’ # 3 Using Combo Boxes As an Array Combo box is an array controlled by VB Count – combobox.ListCount Add/Remove operations Lists start at 0, array at 1

‘Tirgul’ # 3 Controls array In a from, you can group elements –Tab, Option button… Useful when having lots of controls Notation: Design time –Copy/Paste in the form –setting index Option(0), Option(1)