www hndit com HNDIT 23073 Rapid Application Development

  • Slides: 70
Download presentation
www. hndit. com HNDIT 23073 Rapid Application Development Week 1 -Visual Basic: Introduction

www. hndit. com HNDIT 23073 Rapid Application Development Week 1 -Visual Basic: Introduction

www. hndit. com Visual Studio. NET �A platform that allows the development and deployment

www. hndit. com Visual Studio. NET �A platform that allows the development and deployment of desktop and web applications �Allows user choice of many. NET languages May program in One of them May create different parts of application in different languages ▪ ▪ ▪ Visual Basic C# (C Sharp) C++ J++ Etc.

www. hndit. com Visual Studio. NET �Integrated Development Environment – allows the automation of

www. hndit. com Visual Studio. NET �Integrated Development Environment – allows the automation of many of the common programming tasks in one environment Writing the code Checking for Syntax (Language) errors Compiling and Interpreting(Transferring to computer language) Debugging (Fixing Run-time or Logic Errors) Running the Application

www. hndit. com What is Visual Basic. Net � 4 th Generation Programming Environment

www. hndit. com What is Visual Basic. Net � 4 th Generation Programming Environment / Development Language �Based on BASIC language Beginners All-Purpose Symbolic Instructional Code �Most widely used tool for developing Windows Applications Graphical User Interface (GUI) Menus, Buttons, Icons to help the user �Full Object-Oriented Programming Language

www. hndit. com How a VB Application is Compiled and Run

www. hndit. com How a VB Application is Compiled and Run

www. hndit. com Project and Solution Concepts • User creates a new project in

www. hndit. com Project and Solution Concepts • User creates a new project in Visual Studio – A solution and a folder are created at the same time with the same name as the project – The project belongs to the solution – Multiple projects can be included in a solution • Solution – Contains several folders that define an application’s structure – Solution files have a file suffix of. sln • Project: contains files for a part of the solution – – Project file is used to create an executable application A project file has a suffix of. vbproj Every project has a type (Console, Windows, etc. ) Every project has an entry point: A Sub procedure named Main or a Form

www. hndit. com Project and Solution Folders/Files • Solution folder – Solution file (.

www. hndit. com Project and Solution Folders/Files • Solution folder – Solution file (. sln) – Project folder • Project file (. vbproj) • Visual Basic source files (. vb) • My Project folder: contains configuration information common to all projects – The file Assembly. Info. vb contains assembly metadata – The References folder contains references to other assemblies • The bin folder contains the executable file produced as a result of compiling the application

Using Visual Studio. NET www. hndit. com

Using Visual Studio. NET www. hndit. com

Creating an Application www. hndit. com • Select the “Create Project” option from the

Creating an Application www. hndit. com • Select the “Create Project” option from the “Recent Projects” box on the Start Page

www. hndit. com Default Settings

www. hndit. com Default Settings

Visual Basic Forms • This is a Visual Basic GUI object called a form

Visual Basic Forms • This is a Visual Basic GUI object called a form • Forms are the windows and dialog boxes that display when a program runs. • A form is an object that contains other objects such as buttons, text boxes, and labels www. hndit. com

Visual Basic Controls www. hndit. com • Form elements are objects called controls •

Visual Basic Controls www. hndit. com • Form elements are objects called controls • This form has: – Two Text. Box controls – Four Label controls – Two Button controls • The value displayed by a control is held in the text property of the control • Left button text property is Calculate Gross Pay • Buttons have methods attached to events

www. hndit. com T o o l b o x Design Window Solution Explorer

www. hndit. com T o o l b o x Design Window Solution Explorer Properties Window

www. hndit. com Creating the Application • Step 1: Add a Control to the

www. hndit. com Creating the Application • Step 1: Add a Control to the Form – Button – Look in the Toolbox for the Button Control – Select the Button with the Mouse – Draw a Rectangle Region in the Design Window by holding the mouse button down – Release the mouse button to see your button – (Can also be added by double clicking on the button in the Toolbox)

www. hndit. com

www. hndit. com

www. hndit. com Creating the Application • Add a Second Button to the Form

www. hndit. com Creating the Application • Add a Second Button to the Form • Put it in the lower right corner • The project now contains • a form with 2 button • controls

www. hndit. com Control Properties • Properties – All controls have properties – Each

www. hndit. com Control Properties • Properties – All controls have properties – Each property has a value (or values) – Determine the Look and Feel (and sometimes behavior) of a Control – Set initially through the Properties Window • Properties Set for this Application – Name – Text

www. hndit. com Name Property • The name property establishes a means for the

www. hndit. com Name Property • The name property establishes a means for the program to refer to that control • Controls are assigned relatively meaningless names when created • Change these names to something more meaningful • Control names must start with a letter • Remaining characters may be letters, digits, or underscore

Examples of Names www. hndit. com The label controls use the default names (Label

Examples of Names www. hndit. com The label controls use the default names (Label 1, etc. ) Text boxes, buttons, and the Gross Pay label play an active role in the program and have been changed Label 1 txt. Hours. Worked Label 2 txt. Pay. Rate Label 3 lbl. Gross. Pay btn. Calc. Gross. Pay btn. Close

www. hndit. com Control Naming Conventions • Should be meaningful • 1 st 3

www. hndit. com Control Naming Conventions • Should be meaningful • 1 st 3 lowercase letters indicate the type of control – txt… for Text Boxes – lbl… for Labels – btn… for Buttons • After that, capitalize the first letter of each word • txt. Hours. Worked is clearer than txthoursworked • Change the name property – Set the name of button 1 to btn. Welcome – Set the name of button 2 to btn. Exit

Setting Control Properties www. hndit. com • Click on the Control in the Design

Setting Control Properties www. hndit. com • Click on the Control in the Design Window • Select the appropriate property in the Properties Window

www. hndit. com Text Property • Determines the visible text on the control •

www. hndit. com Text Property • Determines the visible text on the control • Change the text property – bnt. Welcome set to “Say Welcome” – btn. Exit set to “Exit” – Do not need to include the “ “ in your text field – Notice how the buttons now display the new text

www. hndit. com Event Driven Programming • The GUI environment is event-driven • An

www. hndit. com Event Driven Programming • The GUI environment is event-driven • An event is an action that takes place within a program – Clicking a button (a Click event) – Keying in a Text. Box (a Text. Changed event) • Visual Basic controls are capable of detecting many, many events • A program can respond to an event if the programmer writes an event procedure

Event Procedures www. hndit. com • An Event Procedure is a block of code

Event Procedures www. hndit. com • An Event Procedure is a block of code that executes only when particular event occurs • Writing an Event Procedure – Create the event procedure stub • Double click on control from Design Window – for default event for that control OR • Open the Code Editor (F 7 or View Menu/Code option) • Select Control & Select Event from drop down windows in Code Editor – Add the event code to the event procedure stub

Open the Code Editor www. hndit. com

Open the Code Editor www. hndit. com

www. hndit. com

www. hndit. com

www. hndit. com Select the Control for the Event Procedure • Select the btn.

www. hndit. com Select the Control for the Event Procedure • Select the btn. Welcome control from the Form Controls List Box

www. hndit. com Select the Event for the Event Procedure • Select the Click

www. hndit. com Select the Event for the Event Procedure • Select the Click event from the list of many available events • Buttons have 57 possible events they can respond to

Event Procedure Stub www. hndit. com • Beginning of Procedure is created for you

Event Procedure Stub www. hndit. com • Beginning of Procedure is created for you – If you create stub by double clicking on control it will create a stub for the most commonly used event for that control

www. hndit. com Add the Event Code • Write the code that you want

www. hndit. com Add the Event Code • Write the code that you want executed when the user clicks on the btn. Welcome button – Type: Msg. Box (“Welcome to Visual Basic”) – Must be contained within the Event Procedure Stub

Writing Visual Basic Code www. hndit. com • Not Case Sensitive – Visual Basic

Writing Visual Basic Code www. hndit. com • Not Case Sensitive – Visual Basic will “correct” case issues for you • Keywords are in Blue – Special reserved words • Comments in Green • Problems with Syntax (Language) will be underlined in blue

Coding Conventions www. hndit. com • Rules – Use spaces to separate the words

Coding Conventions www. hndit. com • Rules – Use spaces to separate the words and operators – Indentation and capitalization have no effect • Recommendations – Use indentation and extra spaces for alignment – Use blank lines before and after groups of related statements – Code all variable declarations at the start of the procedure – Group related declarations

Comments www. hndit. com • Usage – Type an apostrophe ( ' ) followed

Comments www. hndit. com • Usage – Type an apostrophe ( ' ) followed by the comment – The compiler ignores everything on the line after ‘ – Used for documentation/readability and to disable chosen statements during testing • Recommendations – Follow apostrophe with a star for readability ( ‘* ) – Use at beginning of program to indicate author, purpose, date, etc. – Use for groups of related statements and portions of code that are difficult to understand

www. hndit. com Create Event Procedure for Exit Button • Create an Event Procedure

www. hndit. com Create Event Procedure for Exit Button • Create an Event Procedure for when the btn. Exit button is clicked • Have it display “Goodbye” in a Msg. Box • Then “End” – this will terminate the program

www. hndit. com Switching to Design Window • You can switch between the Design

www. hndit. com Switching to Design Window • You can switch between the Design Window and the Code Window (once opened) by clicking on the tabs at the top • Design and Code Windows – Form 1. vb(Design) is the design window – Form 1. vb is the Code Window

www. hndit. com Running the Application �Click the Run Icon on the Standard Toolbar

www. hndit. com Running the Application �Click the Run Icon on the Standard Toolbar �Or Press F 5 �This will begin the program �Display the Form/Window �Nothing will happen Waiting on an Event

www. hndit. com Test the Events • Click on the “Say Welcome” button –

www. hndit. com Test the Events • Click on the “Say Welcome” button – The message box should display • Click on the “Exit” button – The message box should display – The application should terminate

Save the Project • Make sure to save your work – SAVE ALL (not

Save the Project • Make sure to save your work – SAVE ALL (not Save Form) – Visual Basic applications are made of • several files • Often even several forms www. hndit. com

Variables www. hndit. com �A storage location in memory (RAM) Holds data/information while the

Variables www. hndit. com �A storage location in memory (RAM) Holds data/information while the program is running These storage locations can be referred to by their names �Every variable has three properties: Name - reference to the location - cannot be changed Value - the information that is stored - can be changed during program execution, hence the name “variable” Data Type - the type of information that can be stored cannot be changed

www. hndit. com How to Think About Variables • You the programmer make up

www. hndit. com How to Think About Variables • You the programmer make up a name for the variable • Visual Basic associates that name with a location in the computer's RAM • The value currently associated with the variable is stored in that memory location • You simply use the name you chose when you need to access the value

www. hndit. com Usage of Variables Copy and store values entered by the user

www. hndit. com Usage of Variables Copy and store values entered by the user Perform arithmetic manipulation on values Test values to see if they meet a criteria Temporarily hold and manipulate the value of a control property • Hold data/information so that it can be recalled for use at a later point in the code • •

Choosing Data Type www. hndit. com Data type - Specifies type of data variable

Choosing Data Type www. hndit. com Data type - Specifies type of data variable can store • • Integer variables: Long, Integer, Short, Byte Floating-point variables: Single, Double Fixed decimal point variable: Decimal Boolean variables: True, False Character variable: Char Text variable: String The Object variable – Default data type assigned by Visual Basic – Can store many different types of data – Less efficient than other data types

Visual Basic Data Types www. hndit. com Data type Prefix Size Values Byte Short

Visual Basic Data Types www. hndit. com Data type Prefix Size Values Byte Short Integer Long byt shr int lng 1 byte 2 byte 4 byte 8 byte positive integer value from 0 to 255 integer from – 32, 768 to +32, 767 integer from +/- 2, 147, 483, 647 integer from +/- 9, 223, 372, 036, 854, 775, 807 Single Double Decimal sng dbl dec 4 byte single-precision, floating-point number 8 byte double-precision, floating-point number 16 byte number with up to 28 significant digits Char Boolean chr bln 2 byte Any single character 2 byte True or False String Date Object str dtm obj (4 byte) Text - Any number/combination of characters 8 byte 8 character date: #dd/mm/yyyy# (4 byte) An address that refers to an object

Variable Names www. hndit. com �First character must be a letter or underscore �Must

Variable Names www. hndit. com �First character must be a letter or underscore �Must contain only letters, numbers, and underscores (no spaces, periods, etc. ) �Can have up to 255 characters �Cannot be a VB language keyword �Naming Conventions Should be meaningful Follow 3 char prefix style - 1 st 3 letters in lowercase to indicate the data type After that, capitalize the first letter of each word Example: int. Test. Score

www. hndit. com Declaring a Variable • A variable declaration is a statement that

www. hndit. com Declaring a Variable • A variable declaration is a statement that creates a variable in memory • Syntax: Dim Variable. Name As Data. Type – Dim (short for Dimension) - keyword – Variable. Name - name used to refer to variable – As - keyword – Data. Type - one of many possible keywords to indicate the type of value the variable will contain • Example: Dim int. Length as Integer

www. hndit. com Declaring and Initializing a Variable • A starting or initialization value

www. hndit. com Declaring and Initializing a Variable • A starting or initialization value may be specified with the Dim statement • Good practice to set an initial value unless assigning a value prior to using the variable • Syntax: Dim Variable. Name As Data. Type = Value Just append " = value” to the Dim statement = 5 assigning a beginning value to the variable • Example: Dim int. Length as Integer = 5

www. hndit. com Variable Declaration Rules • Variable MUST be declared prior to the

www. hndit. com Variable Declaration Rules • Variable MUST be declared prior to the code where they are used • Variable should be declared first in the procedure (style convention) • Declaring an initial value of the variable in the declaration statement is optional – Refer to default values (next slide)

www. hndit. com Default Values for Data Types Data type All numeric types Boolean

www. hndit. com Default Values for Data Types Data type All numeric types Boolean Char String or Object Date Default (Initial) value Zero (0) False Binary 0 Empty 12: 00 a. m. on January 1, 0001

www. hndit. com Literal • Actual value/data/information • Similar to a variable, but can

www. hndit. com Literal • Actual value/data/information • Similar to a variable, but can NOT change during the execution of a program. • Examples of Literals: – Numeric: 5 ; 157 ; 195. 38256 – String: “Paul” ; “Hello!!!” ; “Jackson, AL 36545” – Char: ‘a’ ; ‘ 1’ ; ‘? ’ ; ‘@’ – Boolean: True ; False

www. hndit. com Named Constants • Programs often need to use given values –

www. hndit. com Named Constants • Programs often need to use given values – For example: dec. Total *= 1. 06 – Adds 6% sales tax to an order total • Two problems with using literals for these types of values – The reason for multiplying dec. Total by 1. 06 isn’t always obvious – If sales tax rate changes, must find and change every occurrence of. 06 or 1. 06

www. hndit. com Named Constants (cont. ) • Use of named constants resolves both

www. hndit. com Named Constants (cont. ) • Use of named constants resolves both these issues • Can declare a variable whose value is set at declaration and cannot be changed later: • Syntax: Const CONST_NAME As Data. Type = Value Looks like a normal declaration except: – Const used instead of Dim – An initialization value is required – By convention, entire name capitalized with underscore characters to separate words

www. hndit. com Named Constants (cont. ) • The objective of our code is

www. hndit. com Named Constants (cont. ) • The objective of our code is now clearer – Const sng. SALES_TAX_RATE As Single = 1. 06 – dec. Total *= sng. SALES_TAX_RATE • Can change all occurrences in the code simply by changing the initial value set in the declaration – If tax rate changes from 6% to 7% – Const sng. SALES_TAX_RATE As Single = 1. 07

Scope of Variables www. hndit. com �What – Indicates the part of the program

Scope of Variables www. hndit. com �What – Indicates the part of the program where the variable can be used �When – From the variable declaration until the end of the code block (procedure, method, etc. ) where it is declared Variable cannot be used before it is declared Variable declared within a code block is only visible to statements within that code block ▪ Called Local Variable Can be declared at the beginning of the class code window (General Declarations section) and be available to all blocks ▪ Called Form Level Variables that share the same scope cannot have the same name (same name ok if different scope)

Lifetime of Variables www. hndit. com • What – Indicates the part of the

Lifetime of Variables www. hndit. com • What – Indicates the part of the program where the variable exists in memory • When – From the beginning of the code block (procedure, method, etc. ) where it is declared until the end of that code block – When the code block begins the space is created to hold the local variables • Memory is allocated from the operating system – When the code block ends the local variables are destroyed • Memory is given back to the operating system

Assignment Statement www. hndit. com • Syntax: variablename = expression • Assigns the value

Assignment Statement www. hndit. com • Syntax: variablename = expression • Assigns the value of the expression to the variable. (The variable must be on the left and the expression on the right. ) • Example: – int. Number 1 = 4 – int. Number 2 = 3 * (2 + 2) – int. Number 3 = int. Number 1 – Int. Number 1 = int. Number 1 + 6

Implicit Type Conversions www. hndit. com • A value of one data type can

Implicit Type Conversions www. hndit. com • A value of one data type can be assigned to a variable of a different type – An implicit type conversion is an attempt to automatically convert to the receiving variable’s data type • A widening conversion suffers no loss of data – Converting an integer to a single – Dim sng. Number as Single = 5 • A narrowing conversion may lose data – Converting a decimal to an integer – Dim int. Count as Integer = 12. 2 ‘int. Count becomes 12

www. hndit. com Explicit Type Conversions • VB provides a set of functions that

www. hndit. com Explicit Type Conversions • VB provides a set of functions that perform data type conversions • These functions will accept a literal, variable name, or arithmetic expression • The following narrowing conversions require an explicit type conversion – Double to Single – Single to Integer – Long to Integer • Boolean, Date, Object, String, and numeric types represent different sorts of values and require conversion functions as well

www. hndit. com The Val Function • The Val function is a more forgiving

www. hndit. com The Val Function • The Val function is a more forgiving means of performing string to numeric conversions • Uses the form Val(string) • If the initial characters form a numeric value, the Val function will return that • Otherwise, it will return a value of zero

www. hndit. com The Val Function – Val("34. 90“) – Val("86 abc“) – Val("$24.

www. hndit. com The Val Function – Val("34. 90“) – Val("86 abc“) – Val("$24. 95“) – Val("3, 789“) – Val("x 29“) – Val("47%“) – Val("Geraldine“) Value Returned 34. 9 86 0 3 0 0 47 0

www. hndit. com The To. String Method • Returns a string representation of the

www. hndit. com The To. String Method • Returns a string representation of the value in the variable calling the method • Every VB data type has a To. String method • Uses the form Variable. Name. To. String • For example Dim number as Integer = 123 lbl. Number. text = number. To. String – Assigns the string “ 123” to the text property of the lbl. Number control

www. hndit. com Performing Calculations with Variables �Arithmetic Operators ^ * /  MOD

www. hndit. com Performing Calculations with Variables �Arithmetic Operators ^ * / MOD + – & Exponential Multiplication Floating Point Division Integer Division Modulus (remainder from division) Addition Subtraction String Concatenation (putting them together)

www. hndit. com Common Arithmetic Operators • Examples of use: – – – dec.

www. hndit. com Common Arithmetic Operators • Examples of use: – – – dec. Total = dec. Price + dec. Tax dec. Net. Price = dec. Price - dec. Discount dbl. Area = dbl. Length * dbl. Width sng. Average = sng. Total / int. Items dbl. Cube = dbl. Side ^ 3

www. hndit. com Special Integer Division Operator • The backslash () is used as

www. hndit. com Special Integer Division Operator • The backslash () is used as an integer division operator • The result is always an integer, created by discarding any remainder from the division • Example – int. Result = 7 2 ‘result – shr. Hundreds = 157 100 ‘result – shr. Tens = (157 - 157 100 * 100) ‘result is 3 is 1 10 is ?

www. hndit. com Special Modulus Operator • This operator can be used in place

www. hndit. com Special Modulus Operator • This operator can be used in place of the backslash operator to give the remainder of a division operation int. Remainder = 17 MOD 3 ‘result is 2 dbl. Remainder = 17. 5 MOD 3 ‘result is 2. 5 • Any attempt to use of the or MOD operator to perform integer division by zero causes a Divide. By. Zero. Exception runtime error

Concatenating Strings • • • www. hndit. com Concatenate: connect strings together Concatenation operator:

Concatenating Strings • • • www. hndit. com Concatenate: connect strings together Concatenation operator: the ampersand (&) Include a space before and after the & operator Numbers after & operator are converted to strings How to concatenate character strings – str. FName = "Bob" – str. LName = "Smith" – str. Name = str. FName & " “ “Bob ” – str. Name = str. Name & str. LName “Bob Smith” – int. X = 1 int. Y = 2 – int. Result = int. X + int. Y – str. Output = int. X & “ + “ & int. Y & “ = “ & int. Result “ 1 + 2 = 3”

www. hndit. com Combined Assignment Operators • Often need to change the value in

www. hndit. com Combined Assignment Operators • Often need to change the value in a variable and assign the result back to that variable • For example: var = var – 5 • Subtracts 5 from the value stored in var Operator += -= *= /= = &= Usage Equivalent to x += 2 x=x+2 x -= 5 x=x– 5 x *= 10 x = x * 10 x /= y x=x/y x = y x=xy x &= “. ”x = x & “. ” Effect Add to Subtract from Multiply by Divide by Int Divide by Concatenate

www. hndit. com Arithmetic Operator Precedence • Operator precedence tells us the order in

www. hndit. com Arithmetic Operator Precedence • Operator precedence tells us the order in which operations are performed • From highest to lowest precedence: – Exponentiation (^) – Multiplicative (* and /) – Integer Division () – Modulus (MOD) – Additive (+ and -) • Parentheses override the order of precedence • Where precedence is the same, operations occur from left to right

www. hndit. com All Operators Precedence • • • Parenthesis Exponential Multiplication / Division

www. hndit. com All Operators Precedence • • • Parenthesis Exponential Multiplication / Division Integer Division MOD Addition / Subtraction String Concatenation Relational Operators (< , >= , <>) Logical Operators (AND, OR, NOT)

www. hndit. com Precedence Examples • 6 * 2 ^ 3 + 4 /

www. hndit. com Precedence Examples • 6 * 2 ^ 3 + 4 / 2 = 50 • 7*4/2– 6=8 • 5 * (4 + 3) – 15 Mod 2 = 34 • • • int. X = 10 int. Y = 5 int. Result. A = int. X + int. Y * 5 i. Result. B = (int. X + int. Y) * 5 d. Result. A = int. X - int. Y * 5 d. Result. B = (int. X - int. Y) * 5 'i. Result. A is 35 'i. Result. B is 75 'd. Result. A is -15 'd. Result. B is 25

www. hndit. com Reference • http: //www. cis. usouthal. edu, 2014/03/10

www. hndit. com Reference • http: //www. cis. usouthal. edu, 2014/03/10