Top Banner
VB.Net Introduction
40

VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

Jan 01, 2016

Download

Documents

Margery Daniel
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

VB.Net Introduction

Page 2: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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.

Page 3: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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.

Page 4: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 5: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 6: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 7: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

Assignment Statement

• Example:Sum=Num1 + Num2

Tax = Salary * TaxRate

Page 8: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 9: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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?

Page 10: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 11: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 12: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

Fundamental Programming Constructs

• Variables, expressions, and assignment

• Input/Output

• Sequence

• Decision

• Loop

Page 13: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

A metaphor with cooking

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

Page 14: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 15: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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.

Page 16: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 17: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 18: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

Typical Interface ControlsExample: WWW.United.Com

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

Page 19: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

Form

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

BackImage, Opacity

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

Page 20: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 21: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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.

Page 22: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 23: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

.

Page 24: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

Configure VB Project

• Project property page– Application– Compile– References

• Tools/Options– Environment

– Projects and Solutions

» VB defaults

Page 25: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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.

Page 26: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 27: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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.

Page 28: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 29: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 30: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 31: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 32: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

• 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

Page 33: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 34: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 35: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

Arithmetic and String Operators

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

• String Concatenation: &, +

Page 36: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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

Page 37: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

Order of Evaluation

• Operator Multiples

• 1 ( ) Inner to outer, left to right

• 2. ^ left to right

• 3. *, / left to right

• 4. +, - left to right

Page 38: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

Formula to Expression

CD

BA

DC

ABCD

BA

ABAA

B

22

DC

AB

CD

BA

B

A

X

X

Y

X

Page 39: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

Example: Create a form to compute the future value

• FutureValue = PresentValue x (1+intRate)Years

Page 40: VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.

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.