VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a...

40
VB.Net Introduction

Transcript of VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a...

VB.Net Introduction

What is programming?

• Writing computer programs means writing instructions in a logical sequence, that will make the computer follow and run a program based on those instructions.

Write a Program that asks user to enter two numbers and displays the sum

• A program typically consists of three parts:– Input, process, output

• For this program:– Inputs: two numbers– Process: add the two numbers– Output: the sum

• This program must be able to handle any two numbers.

What is a variable?

• A named location where a value is stored.

• Examples:– Variables: X, Y– Variables: Number1, Number2, Sum– Variables Salary, TaxRate– Variables CustomerName, CityName

• Meaningful names

• Numeric data and text (or string) data

Input, process, output

• Input: Enter two numbers and save them in variables Num1, Num2

• Process: Add the two numbers and same the result in a variable Sum

• Output: Display Sum

User Interface for Input/Output

• Command interface– Example:

• Start/Run/Cmd

• Graphical User Interface, GUI– Example: Mortgage calculator

• http://www.mortgage-calc.com/mortgage/simple.html

Assignment Statement

• Example:Sum=Num1 + Num2

Tax = Salary * TaxRate

Write a Program that asks user to enter two numbers and displays the larger number

• For this program:– Inputs: two numbers

• Num1, Num2

– Process: Determine the larger number, Larger• If Num1 is greater than Num2 then Larger = Num1

• Otherwise, Larger = Num2

– Output: Display larger

Write a Program that asks user to enter three numbers and displays the largest number

• For this program:– Inputs: three numbers

• Num1, Num2, Num3

– Process: How to determine the largest number?• There are many ways to do this.

– Output: the largest number

• How about 5 numbers?

• Or any number of values?

Write a Program that asks user to enter 5 numbers and displays the largest number

Inputs: Num1, Num2, Num3, Num4, Num5Process:

Largest = Num1If Num2>Largest Then largest = Num2 If Num3>Largest Then largest = Num3 If Num4>Largest Then largest = Num4 If Num5>Largest Then largest = Num5

Output: Largest

Note: Pseudo Code

Write a Program that asks user to enter any numbers and displays the largest

number

Process:Largest = the first numberGet the next number If the next number is greater than Largest Then

Largest = the next numberRepeat this part until no more data

Note: Loop

Fundamental Programming Constructs

• Variables, expressions, and assignment

• Input/Output

• Sequence

• Decision

• Loop

A metaphor with cooking

• Programming is like writing down a recipe for someone else to use:– Ingredients– Cooking instructions– End product

Integrated Development Environment, IDE

• An integrated development environment (IDE) also known as integrated design environment is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of:– a source code editor– a compiler and/or an interpreter– build automation tools– a debugger

• Examples: Visual Studio for .Net, NetBeans for Java

Install Visual Studio 2008

• 1.  You need a blank DVD Recordable or Rewritable disk (typically with at least 4.7 GB of storage) , not CD. 2.  Go to Business Computing Lab.  On the right-hand side of the entrance find the workstation with a label: MSDN 3.  Turn on the computer.  When the computer prompt you for password please enter: setmefree 4.  With your DVD disk in the drive, find and click the VS 2008 icon on the desktop to start the process. 5.  Once done, install VS 2008 on your computer.  You don't need any key to install.

Visual Studio 2008• It supports VB.Net, J#, C#, and C++.• Demo:

– Start page: Recent projects– Starting project:

• File/New Project/Project types: Windows Application– View:

• Solution Explorer/Data Sources• Server Explorer• Property Window• ToolBox

– Form design view/Form code view– Tools/Option

– Environment– Projects and Solutions

» VB defaults

– Project/Add New Item– Project properties: Right-click project name and choose Properties

• Start Up form– Property window example

Introduction to Visual Basic .Net

• Event-driven programming– The interface for a VB program consists of one

or more forms, containing one or more controls (screen objects).

– Form and controls have events that can respond to. Typical events include clicking a mouse button, type a character on the keyboard, changing a value, etc.

– Event procedure

Typical Interface ControlsExample: WWW.United.Com

• Form• TextBox• Label• Button• CheckBox• RadioButton• ListBox• Etc.

Form

• Properties:– Name, FormBorderStyle, Text, BackColor,

BackImage, Opacity

• Events:– Load, FormClosing, FormClosed– GotFocus, LostFocus– MouseHover, Click, DoubleCLick

Text Box• Properties:

– BorderStyle, Text– PasswordChar– Multiline, ScrollBar

• To refer to a property: – ControlName.PropertyName– Ex. TextBox1.Text

• To assign the text property to a variable:– Ex. Num1= TextBox1.Text

Typical VB.Net Programming Tasks

• Creating the GUI elements that make up the application’s user interface.– Visualize the application.– Make a list of the controls needed.

• Setting the properties of the GUI elements

• Writing procedures that respond to events and perform other operations.

Demo

First Name

Last Name

Full Name

.Control properties

.Event: Click, MouseMove, Form Load, etc.

.Event proceduresSum: textBox3.text=TextBox1.text + TextBox2.text

Demo: Control text alignment using the TextAlign property

Demo

Num1

Num2

Sum =

.Control properties

.Event: Click, MouseMove, Form Load, etc.

.Event proceduresSum: textBox3.text=CStr(CDbl(textBox1.text)+CDbl(textBox2.text))Or (CDbl(textBox1.text)+CDbl(textBox2.text)).toString

.

Configure VB Project

• Project property page– Application– Compile– References

• Tools/Options– Environment

– Projects and Solutions

» VB defaults

VB Defaults

• Option Explicit: – On --- must declare variables before use

• Option Strict:– Off --- VB will convert the data

• Option Compare:– Binary --- case sensitive– Text --- case insensitive

• Option Infer– On --- When you set Option Infer to On, you can declare variables

without explicitly stating a data type. The compiler infers the data type of a variable from the type of its initialization expression.

Variable Declarations• Option Explicit• Dim variableName as DataType• Variable naming rules:

– The first character must be a letter or an underscore character.

– Use only letters, digits, and underscore.– Cannot contain spaces or periods.– No VB keywords

• Naming conventions:– Descriptive– Consistent lower and upper case characters.

• Ex. Camel casing: lowerUpper, employeeName

Control Naming Conventions

• The first three letters should be a lowercase prefix that indicates the control’s type.– frm, txt, lbl, btn.

• The first letter after the prefix should be uppercase.– txtSalary, lblMessage

• The part of the control name after the prefix should describe the control’s purpose in the application.

VB Data Types

• Boolean (True/False): 2 bytes• Byte: Holds a whole number from 0 to 255.• Char: single character• Date: date and time, 8 bytes.• Decimal: Real number up to 29 significant digits, 16 bytes• Double: real, 8 bytes• Single: real, 4 bytes• Integer: 4 bytes (int32, uint32)• Long: 8 bytes integer• Short: 2 bytes integer• String• Object: Holds a reference of an object

Variable Declaration Examples

• Dim empName as String

• Declare multiple variables with one Dim:– Dim empName, dependentName, empSSN as String

• Dim X As Integer, Y As Single

• Initiatialization– Dim interestRate as Double = 0.0715

Data Conversion• Implicit conversion: When you assign a value of

one data type to a variable of another data type, VB attempts to convert the value being assigned to the data type of the variable if the OptionStrict is set to Off.

• Explicit conversion:– VB.Net Functions: CStr, Ccur, CDbl, Cint, CLng,

CSng, Cdate,Val, etc.– .Net System.Convert

• Type class’s methods:– toString

Date Data Type

• Variables of the Date data type can hold both a date and a time. The smallest value is midnight (00:00:00) of Jan 1 of the year 1. The largest value is 11:59:59 PM of Dec. 31 of the year 9999.

• Date literals: A date literal may contain the date, the time, or both, and must be enclosed in # symbols:– #1/30/2003#, #1/31/2003 2:10:00 PM#– #6:30 PM#, #18:30:00#

• Note: ControlPanel/RegionalOptions/Date

• Date Literal Example:– Dim startDate as dateTime– startDate = #1/30/2003#

• Use the System.Convert.ToDateTime function to convert a string to a date value:– startDate = System.Convert.ToDateTime(“1/30/2003”)– If date string is entered in a text box:

• startDate = System.Convert.ToDateTime(txtDate.text)• Or startDate=Cdate(txtDate.text)

• Date data type format methods

Some Date Functions

• Now: Current date and time• Today: Current date• TimeOfDay• DateDiff:• Demo

– Days between two dates– Days to Christmas

• DateDiff(DateInterval.Day, Today(), #12/25/2009#)

– Date data type properties and methods

Using Online HelpExample:Search Help for DateDiff

• MSDN VB Developer Center– http://msdn.microsoft.com/en-us/vbasic/

default.aspx– Library/Visual Studio/Visual Basic

• Reference – Language reference

» Functions:

» DateDiff Function

» Statements

Arithmetic and String Operators

• +, -, *, /. \, ^, ( )

• String Concatenation: &, +

Arithmetic Operators

• +, -, *, /, ^, ( )

• Evaluating an expression– Examples:

• 4 + 2 – 1• 4 + 2 * 3• 3 – 4 / 2• 8 / 4 * 2• 2 * 3 ^ 2• (2 * 3) + 2 ^ 3

Order of Evaluation

• Operator Multiples

• 1 ( ) Inner to outer, left to right

• 2. ^ left to right

• 3. *, / left to right

• 4. +, - left to right

Formula to Expression

CD

BA

DC

ABCD

BA

ABAA

B

22

DC

AB

CD

BA

B

A

X

X

Y

X

Example: Create a form to compute the future value

• FutureValue = PresentValue x (1+intRate)Years

Variable Scope• Block-level scope: declared within a block of code

terminated by an end, loop or next statement.– If city = “Rome” then

• Dim message as string = “the city is in Italy”

• MessageBox.Show(message)

– End if

• Procedural-level scope: declared in a procedure• Class-level, module-level scope: declared in a

class or module but outside any procedure with either Dim or Private keyword.

• Project-level scope: a module variable declared with the Public keyword.