310 likes | 398 Views
Writing Software Kashef Mughal. Algorithms. The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem. An Algorithm describes the program in a step by step manner In order to solve a business problem using a program, we have two choices
E N D
Algorithms • The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem. • An Algorithm describes the program in a step by step manner • In order to solve a business problem using a program, we have two choices • Procedure-Oriented approach • Object-Oriented approach
Solving the Problem Using a Procedure-Oriented Approach • Emphasis of a program is on how to accomplish a task • A flowchart uses standardized symbols to show the steps needed to solve a problem • Pseudocode uses English phrases to describe the required steps • User has little, if any, control
Solving the Problem Using an Object-Oriented/Event-Driven (OOED) Approach • Object-oriented/Event-driven • Emphasis of a program is on the objects included in the interface and the events that occur on those objects • You will use a TOE (Task, Object, Event) chart to assist you in planning your object-oriented/event-driven programs • User has a lot of control • Users can enter information in any order, change what they entered at any time, and calculate a subtotal whenever they like
Using Variables to Store Information • Besides storing data in the properties of controls, a programmer also can store data, temporarily, in memory locations inside the computer • The memory locations are called variables, because the contents of the locations can change as the program is running • You can enter and store data in the box, but you cannot actually see the box
Using Variables to Store Information • You can also store the data contained in a control’s property in a variable • All variables have: • Data type – kind of data the variable can contain • Name – An identifier the programmer creates to refer to the variable • Value – Every variable refers to a memory location that contains data. This value can be specified by the programmer. This process is called assignment
Selecting a Name for a Variable • You should assign a descriptive name to each variable used in an application • The name should help you remember the variable’s data type and purpose • Hungarian Notation is a common standard for variable names. The figure below lists the three characters typically associated with the Visual Basic .NET data types using Hungarian Notation
Declaring a Variable • To declare a VB .NET variable, write: • Keyword “Dim” • Name to be used for identifier • Keyword “As” • Data type • Example: ' declare a variableDim intA As Integer
Assigning Data to an Existing Variable • A value can be assigned by the programmer to a variable • Assignment operator (=) assigns the value on the right to the variable named on the left side • Example: ' populate the variable intA = 1
In-Class Exercises • Try the following exercises • Pages 45,46 • Pages 49, 50 • Page 52 • Page 54 • Review the results with me • Any questions?
Working with Strings • A String is a collection of characters • We use double quotes to mark its beginning and end (delimit) • For example dim s as string used to declare it • Concatenation means combining data • Substring is a subset of any string • Formatting of strings is a big concern especially from a user stand point
Concatenating Strings • Connecting strings together is called concatenating • You use the concatenation operator, which is the ampersand (&), to concatenate strings in Visual Basic .NET • When concatenating strings, you must be sure to include a space before and after the concatenation operator
String Operations • .Length - gets the number of characters in the string • .Substring - retrieves a substring from the string • .Format - Replaces each format item in a specified string • .Replace - Replaces all occurrences of a specified string with another String • .Trim - Removes all occurrences of white space characters from the beginning and end of the string
Working with Dates • This data type has the most properties and methods out of all the data types • When assigning to a variable, should qualify with # like for today it would be #1/14/2004# • Date.Now - Gets date time that is the current local date and time on this computer • .DatePart - Returns an Integer value containing the specified component of a given Date value • Dates also have a number of methods for formatting as these can vary from one region
The Scope of a Variable • A variable’s scope indicates which procedures in an application can use the variable • The scope is determined by where the Dim, Public or Private statement is entered • When you declare a variable in a procedure, the variable is called a local variable and is said to have procedure scope • When you declare a variable in the form’s Declarations section, the variable is called a form-level variable and is said to have module scope • Module includes all the procedures
Creating a Local Variable • Created with the Dim statement • The Dim statement is entered in an object’s event procedure e.g. Private Sub ... • Only the procedure in which it is declared can use the variable • Removed from memory when the procedure ends • Preferred way of coding
Creating a Form-level Variable • Created with the Private statement • Entered in a form’s General declarations section • Can be used by any of the procedures in the form • Removed from memory when the form is destroyed • Should be used on a limited basis
Creating a Global Variable • Created with the Public statement • Best place for this is in a new module like basMain • This can be used by any procedure in any module • Removed from memory when the application ends • Should also be used on a limited basis. An example would be connection to a database
It refers to a memory location whose contents cannot be changed while the program is running Constants are usually declared globally Examples: conPi conRate Constants
Creating a Constant • A memory location whose value cannot change during run time • Syntax: [Public|Private] Const constname [As datatype] = expression • Examples: • Const conPi As Single = 3.141593 • Public Const conMaxAge as Integer = 65
In-Class Exercises • Try the following exercises • Pages 56 through 75 labeled “Try It Out” • Review any problems with me • Any questions?
Number Systems • Humans use Base-10 number system • Computers prefer Base-2 number system • Review the differences on Page 77 • A memory slot in a computer is called a bit • 8 bits make up a byte • Everything is in terms of power of 2 , so 1 KB is really 210 which is equal to 1024 bytes • Review the conversions shown in the book
Methods • A method is a block of code that does something • Helps to break up a program into pieces • Lets you re-use the code • We pass data (parameters) to a method which in turn returns (results) back to us • Two types of methods • Sub (sub procedure or sub routine) • Function
Sub vs. Function • A function is used when we need a block of code to do something and then return a value back. For example a function to calculate area • A sub is used when we do not need a return value. For example if user clicks on a button do something. • We have been working with a sub already • Private Sub .. End Sub
Debugging an Application • Errors are common in computer applications • Two kinds of Errors • Compile Error • Run-time Error • More in Chapter 11
In-Class Exercises • Try the following exercises • Pages 85 • Pages 87 • Page 89 • Page 91 • Page 93 • Review Questions on Page 95