ISYS 573 Special Topic – VB.Net David Chao. The History of VB Early 1960s:BASIC-Beginner’s...

35
ISYS 573 Special Topic – VB.Net David Chao
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of ISYS 573 Special Topic – VB.Net David Chao. The History of VB Early 1960s:BASIC-Beginner’s...

ISYS 573Special Topic – VB.Net

David Chao

The History of VB• Early 1960s:BASIC-Beginner’s All-Purpose

Symbolic Instruction Code– Teaching– Simple syntax, easy-to-use– Interpreted language – slow

• Microsoft’s long history of commitment to BASIC– In the 1980s: QuickBasic, QBasic (shipped with DOS)– In the early 1990s, VB 1.0

• Visual interface design and creation• Even-driven programming• Easy database access

– 2002: VB .Net• Challenges: Java, Internet application development

Portability Java: Write Once Run Anywhere

Java Source Code

Java Byte Code(Intermediate Code)

Java Byte Code

Java Virtual Machine(JVM)

Executable Code

.NET Framework

• .NET Framework class libraries: A large set of classes that forms the basis for objects that can be used programmatically.

• Creating Internet applications and Windows applications

Microsoft’s .Net

• Language must compliance with Common Language Specification, CLS.

• Compile the language into Microsoft Intermediate Language (MSIL) code.

• The MSIL code is then executed in the Common Language Runtime (CLR), which conceptually is same as the JVM, where it is translated into machine code by a compiler.

.Net Architecture

Common Language Runtime

Base Class Library

Data and XML

ASP.Net Windows Forms

Common Language Specification

VB.Net C# C++

• Common Language Runtime:– Manages execution of compiled .NET program.– Provides .Net basic services, such as memory

management, garbage collection, etc.

• Base Class library: define all the basic data types such as system.object, numeric, date, etc.

• Data and XML: Classes work with database (ADO.NET) and XML document.

• ASP.Net and Forms: Classes that generate user interface.

• CLS: CLS dictates the minimum group of features that a .Net language must have.

.Net Advantages

• It is independence from a specific language. Developers can create a .Net application in any .Net compatible language.– .Net moves most of the functionality from the language

to the .Net Framework. All .Net language can use these classes.

• It can exist on multiple platforms, further extending the portability of .Net programs.

• Facilitate internet application development:– ASP.Net: Web Forms and XML Web services.

• Universal data access: Data can be accessed by any Internet-connected device.

Programming in the .Net Framework

• Programming in the .Net Framework means making use of the classes, objects, and members exposed by the Framework, building your own classes on top of these and manipulating the resulting objects using a .Net language.

• Ex. VB form is derived from System.Windows.Forms.Form class.

Major Topics in ISYS 573• VB.Net language

– Event-driven programming

– Component programming

– Object-oriented programming

– Database access

• .Net Framework classes• Internet applications

– Web forms

– Web services

– Web controls

Class and Object

• A class is a blueprint from which objects are made. Every object created from a class is an instance of the class.

• Properties and methods: An object’s interface consists of properties and methods. A property is an attribute associated with an object, while a method is an action that the object can carry out.

VB.NET is Object-Oriented

• Everything from the simplest data types provided by VB to the complex windows is a class.– Ex:

• Dim iNum as Integer

• iNum=10

• Debug.WriteLine (“The number is: “ & iNum.ToString)

Visual Studio .NET

• It is the development environment for creating applications based on the .NET Framework.

• It supports VB, C#, and C++.• Demo:

– Start page: MyProfile

– Starting project: Project types, name and location,

– Solutions and projects, renaming a project, property page, AutoHide, Dock/Float

– Configure start up environment: Tools/Option

– View/Solution, View/Class, Project/Add Windows Form, Project/Add New Item

– Form, Code view, File Properties and Object properties

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 control has a number of events that it can respond to. Typical events include clicking a mouse button, type a character on the keyboard, changing a value, etc.

– Event procedure

Typical VB.Net Controls

• Form design view: ToolBox/Windows Forms– TextBox– Label– Button– CheckBox– RadioButton– ListBox– ComboBox– PictureBox– Etc.

Form

• Properties:– Name, FormBorder, Text, BackColor, BackImage,

Opacity?

• Events:– Load, Closed

• Rename a form:– Change the name and text property.

– Rename the form in the Solution Explorer.

– Change the StartUp in the Project Property.

Text Box• Properties:

– AutoSize, BorderStyle, CauseValidation, Enabled, Locked, Multiline, PasswordChar, ReadOnly, ScrollBar, TabIndex, Text, Visible, etc.

• Events:– CauseValidation, TextChanged, etc.

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

• Properties can be set at the design time or at the run time with code.– Demo: TextAlignment

• Label – Display message, output– Property:– Text, TextAlign– Autosize– Visible

• Button– Click event

Typical VB.Net Programming Tasks

• Creatin g 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

Num1

Num2

Sum =

Control propertiesEvent: Click, MouseMove, FormLoad, etc.Event proceduresSum: textBox3.text=CStr(CDbl(textBox1.text)+CDbl(textBox2.text))Or (CDbl(textBox1.text)+CDbl(textBox2.text)).toStringChallenge: How to draw a horizontal line?

Demo

• Chap 2, KiloConverter.

• Add buttons to change text alignment in the label.– TextAlign property and enumeration

VB Projects

• A VB project consists of several files. Visual Studio .Net automatically creates a project folder to keep all project files in the folder.– Solution file

– Project file

– Form file

– Modules

– Class file

– Etc.

Configure VB Project

• Project property page– General– Build: Option Explicit– Imports

• Debug– Debug/Windows

Variable Declarations

• 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

Variable Default Value

• Variables with a numeric data type: 0

• Boolean variables: False

• Date variables: 12:00:00 AM, January 1 of the year 1.

• String variables: Nothing

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(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 class or module variable declared with the Public keyword.

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

• Ex. System.Convert.ToDateTime(var)

• Type class’s methods:– toString

• Demo: Val

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#

• 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)

Some Date Functions

• Now: Current date and time

• Today: Current date

• TimeOfDay

• DateDiff

• Demo– Days to Christmas– Date data type properties and methods

Arithmetic and String Operators• +, -, *, / , ^, ( )• \:Integer division

– Return integer result– If the operands are real numbers, they are rounded to the

nearest whole number before the division takes place.

• Mod:– Remainder = 17 mod 5

• Order of evaluation:– 1. ( ), 2. ^, 3.*, /, 4.\, 5.Mod, 6.+, -

• String Concatenation: &, +• Compound operator:

: X= X+1 or X +=1

Demo

• Chap 3, simple calculator