CSCD 302 PROGRAMMING VB NET MR SILAS KWABLA
CSCD 302: PROGRAMMING VB. NET MR. SILAS KWABLA GAH LECTURE 3 UNIVERSITY OF GHANA DEPARTMENT OF COMPUTER SCIENCE
Variables • A variable is what the program used to store value in computers memory. • Data Type of a variable tell the computer to store different types of values such as number, text string, date, currency and true/false. • They also inform the different memory spaces for those variables UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Continue… • In vb. net, a variable can be declared using the following ways • Dim variable_name as Datatype • To declare a variable, you will use keyword Dim before name of the variable followed by keyword As and type of data to be stored in memory. • Example Dim x As Integer UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Accessing Variables • Dim X As Integer = 10 • Dim d As Double = 12332322 • Dim b As Boolean = true • Console. writeline(“x=“ & x) • Console. writeline(“d=”& d) • Console. writeline(“b=“&b) UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Operators • “=“ sign is used as the assignment operator in Vb. net • Dim x as Integer • X = 10, assigns 10 to x variable • Logical operators also can be used • And, Or and Not operators UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Compound Operators • “+=“ y + = t => y = y + t • “-=“ y- = t => y= y- t • The “&” sign is used to concatenate strings or text. • Dim yourname As String • yourname = “Welcome” & yourname UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Conditional Statements • If statement will evaluate the condition. • If it is true, it will execute the statements that follows it. • Otherwise it will execute the statements in else block. UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Continue. . • Example if condition then • Statements to be executed • End if • If you use the above general form, the statements are executed when the condition is true. • There is no statements to be execute when the condition is false UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Sub and Functions Procedures • There are two types of Sub procedures in Visual Basic: event procedures and independent Sub procedures. • Programmers use independent Sub procedures for several reasons. • They allow the programmer to avoid duplicating code when different sections of a program need to perform the same task UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Continue… UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Example • Employees at Legon are paid on an hourly basis and can work a maximum of 5 o hours per week. The employees receive overtime pay as shown here • Create a windows application that displays employees regular pay, overtime pay and gross pay for a week UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Continue… • For the first 40 hours overtime is zero • From 40 through 50 hours rate is 2. 5 UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Continue… UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Including Parameters in Procedure • The parameter. List lists the data type and name of one or more parameters. • Parameter is a memory location that stores an item of data passed to the procedure when the procedure is invoked. • Each parameter in the parameter. List has procedure scope, which means it can be usedonly within the procedure • The number of arguments should agree with the number of parameters. • UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Continue… • Each variable declared in a program has both a value and a unique address that represents the variable’s location in the computer’s internal memory. • Passing a copy of the variable’s value is referred to as passing by value, whereas passing its address is referred to as passing by reference. UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Example • Create an application that prompts the user to enter the name of his favorite actor or actress. The application should the display either the message “Your favorite actor is name” or the message “Your favorite actress is name. In both messages, the name is the entered by the user UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Function Procedure • The difference between both types of procedures is that a Function procedure returns a value after performing its assigned task, whereas a Sub procedure does not return a value. • It is called functions most at times UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Continue. . UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
Continue… • As with Sub procedures, you can enter your functions anywhere in the Code Editor window, as long as you enter them between the Public Class and End Class clauses and outside of any other procedure. UG, DCS 13/06/20 21 CSCD 418 - LT NOTES
- Slides: 19