Visual Basic .Net

advertisement
Visual Basic .Net
Seminar June 7, 2004
Topics Today include





The .Net Framework
Studio .Net Environment
VB .Net
Introduction to VB .Net
Loops and Control Structures
The .Net Framework







What is the .Net Framework
Common Language Runtime
.Net Managed Assemblies
Compilation of .Net Code
.Net Framework Class Library
.Net Languages
The future of .Net
What is the .Net
Framework


Primarily a development platform
(mostly effects application
development)
Consists of two main parts
– Common Language Runtime
– .Net Framework Class Library
CLR (Common Language
Runtime)


Provides a “runtime environment” for
the execution of code written in ANY
.Net Language
“Manages”
– The overall execution of .Net code
– Inheritance, Memory, Debugging, and
application development across Multiple
languages
CLR (Continued)

Languages that utilizes the .Net framework must follow the
CLS
– Common Language Specification
– Outlines the rules and standards required or needed by the CLR
to execute .Net code.

The CLR will execute Managed Assemblies
Intermediate Languages managed by the CLR are called

The CLR will manage for the IL

Managed Code
– Garbage collection
– Object instantiation
– Memory allocation

Components written in Managed Code by the CLR are called
.Net Managed assemblies
.Net Managed Assemblies



Basic Unit of deployment of .Net
Applications
Similar to the Microsoft COM Objects
Each Managed Assembly contains a Manifest
– Hold definitions of other components required
for execution of the application
– Prevents the problems associated with
conflicting DLL’s
– Revision numbers and components are listed in
the manifest
– Two versions of the same DLL can then co-exist
Compilation of .Net code



All .Net languages are first compiled into an
intermediate language called MSIL or IL (Microsoft
Intermediate Language)
The MSIL is then “Just In Time” (JIT) compiled at
first execution of the code and managed by the CLR
All MSIL or IL code can be compiled directly to
native code with the following
– Compiling native code will provide faster startup execution
of the code
– Performance during execution of the code will then be
sacrificed
– During JIT Optimization is placed on the Managed Code in
order to improve performance at the time of execution
.Net Framework Class
Library


Provide a library of base classes that
developers can use in their own
applications written in ANY .Net
language
Because of the use of these base
classes, Inheritance can then be
extensively used in languages that use
the .Net Framework
.Net Languages


.Net applications can be written by any or a
combination of many .Net languages
Languages include
–
–
–
–

VB .Net
C#
J#
Managed C++ (default in studio is set for managed C++)
Applications can be developed without the use of
Studio .Net
– Applications can be developed still using a simple text
editor
– ASP .Net Applications as well as other apps can be still
developed using notepad
The future of the .Net
Framework



Standards were release in 2000 from Microsoft to the community
GNU .Net is a project in the Linux community that is presently developing a
CLR to run in the Linux environment
Languages planned to include the CLS are
–
–
–
–
–
–
–
–
–
–
–
–
–
APL
COBOL
Eifel
Fortran
Haskel
Mercury
Mondrian
Oberon
Pascal
Perl
Python
RPG
Scheme
Studio .Net Environment
The IDE



Studio .Net
The Start Page
Problems with Studio .Net in the lab
environment
Studio .Net

All present .Net languages are included under one
development environment
– When using any one of the languages, the studio must be
used (there is no longer an environment for each
language)
– You have to start a new solution/project



Projects are containers that contain the files associated with
a particular project
A solution is a container that contains one or more projects
or project containers
When the IDE is started you have the Projects tab,
online resources, and My Profile
The Start Page
Tab for
this view
Tab for
Additional Help
Profile and
environment settings
Previous Projects
Start a new
project
Open a project
in a diff. location
Problems with Studio .Net
environment in the lab



.Net security must allow access to the J: drive or
the local intra net in order to work properly
Problems arise when debugging apps on an ASP
.Net server that is not part of the current domain
(CLC) Basic apps will still work fine
These problems are not present if IIS and remote
debugging tools are installed on the client machine
– This solution is fine for students but is not practical for a
lab
– Due to security holes of IIS you can have hundreds of
security risks on a given network.
– In the Real world situation, all apps will be placed on a
server anyway
Visual Basic .Net



General Features
Additional Features of VB .Net
What has changed in VB .Net
General features


It is an object oriented language
–
–
–
Students do better with this subject when they have a firm understanding of objects
In the past VB had objects but focus was not placed on them
VB .Net use objects and inheritance with everything
–
New techniques are shown to the student for providing feedback from their
programs in order to help trouble shoot errors
Like with most OOP languages, exceptions that are not handled will cause programs
to abnormally end or exit
Uses exception handling extensively
–

Uses “generics” with creating classes and Sub programs
–
–


Can use the top data type on inheritance of type Object
Used for late binding of data types and is quite useful when implementing stacks or
queues of any data type (.Net 2.0)
Provides a quick way to develop heavy duty applications in a windowing
environment
It is a useful tool for developing event driven programs
Additional Feature of VB
.Net


Can create both windows applications and web applications at
the click of a mouse button
The interface between the Databases and applications
– Are the same between web apps and windows apps
– All data are transferred between the apps in XML format
regardless if it is a web application or a windows application
– A copy of the data from the database is loaded into a data set.
– Persistent connections between the application and the database
no longer exists


Every form is a class and new forms now have to be
instantiated
VB .Net is still not case sensitive
What is Not in VB .Net

Data Controls
– Because of the different way that the apps connect to the
database, there is no longer the support of data controls
in VB .Net
– Persistent connections with the database is no longer
present



Object instantiation is now done differently
Garbage collection for references that lose there
objects are done periodically not when the
reference lose its scope
Periodic changes throughout the language making
it vastly different from VB 6.0 in order to make it
compliant to the .Net Framework and the CLS
The Introduction to
VB .Net



Data Types in VB .Net
Functions and Sub Procedures
Practice Examples
Data Types in VB .Net

Data Types and possible values
– Integer -> -2,147,483,648 (4 bytes)
– Double -> floating point numbers with 14 digits of accuracy
–
–
–
–
–
–
–
–
–
–
(8
Bytes)
Decimal -> decimal values ( 16 bytes replaced currency in 6.0)
Date -> the date (8 Bytes)
Byte -> 0 to 255 (1 Byte)
Char -> Unicode character (2 Bytes)
Boolean ->True or false value (2 Bytes)
Single -> floating point number with 6 digits of accuracy (4 bytes)
Short -> 2 byte integer
Long -> 8 byte integer
String -> an arrangement of alpha-numeric characters varies in
length
Object -> any data type 4 bytes
Functions and Sub
procedures / Programs in
VB .Net (Methods)







Functions and sub procedures must either have a sub or
function keyword in the function or sub procedure heading
They can be either public or private (following the same
conventions in OOP languages such as C++ or Java)
All parameters are passed by value by default in VB .Net
Parameters of any data type may be passed by reference
All Objects and arrays are passed by reference not value
All events are handled by a sub procedure in VB .Net
Functions return a value and Sub Procedures do Not
Practice example


1
You can use the text book given to you to
get the basics needed in programming VB
starting on page 477
Getting familiar with VB .Net
– Write a program that will take text from one text
box and place that text in another text box with
a click of a button
– Take a look at the attributes of each control



A control can be a button text box etc.
Each control should contain names that follow VB
specifications i.e. txtBox1 txtBox2 btnMove
Each name as always should be meaningfull
Practice Example 2

Using Functions in VB .Net
– Use the text boxes from the previous example
– Add a text box
– Provide a function that adds the values of text
box 1 and text box 2
– Place the answer in text box3
– Use variables of type double and use the CDbl()
function to convert the string value of the text
box to a double page 485 in your text
Loops and control
structures




The
The
The
The
If Statement
Select Case Statement
Do While and Loop While loops
For Loop
The IF Statement


Every If statement must contain a then and an end if
Example:
– If Num1 < 2 Then
MessageBox.show(“The value is less than 2”)
End If
– If Num1 <> 4 then
MessageBox.Show(“The value is not 4”)
End If

The relational operators that can be used in VB are
–
–
–
–
–
–
>
<
=
>=
<=
<> (Not equal to)
The Select Case statement



The select case statement is a lot like the
switch statement in Java
It can use ANY data type in VB
Example
Select Case Num1
Case 1 DoSomeThing()
Case 2 Do SomethingElse()
End Select

Yes you can use strings as well
Do While Loops



Do while loops evaluate the expression
first then executes the Loop Body
Act much in the same as All While
loops in languages like C++ and Java
Example
Do While Num1 < 10
Num1 += 1
Loop
Loop While Loops




The Body of the Loop is executed first then
the expression is evaluated
The loop will execute at least once
This is a lot like the do while loop in Java
and students get confused with this subtle
change
Example
Do
Num1 += 1
Loop While Num1 < 10
End Of Day 1
VB .Net Seminar Day 2
June 8, 2004
Microsoft SQL Server






MS SQL Basics
The Enterprise Manager
Creating Databases
Connecting to Databases
The use of the Data Set
Exception Handling
Microsoft SQL Basics




Differences between MS SQL and
Access
Advantages of MS SQL
Quirky things with SQL
Connecting to the database with the
manager
Differences between MS
SQL and Access

SQL More Powerful than MS Access
– Larger data types are available
– Connection over a network provide
access to multiple clients
– Blob data type available

A client is required to connect to the
SQL Database
Advantages of MS SQL




Can handle multiple users at one time
Ideal for web sites and web
applications
Contain the BLOB (Binary Large
Object) which enables a user to store
images in a database
Larger numeric values can be stored in
each field
Quirky problems with SQL





No Auto Number Data Type in MS SQL
Auto number is an integer data type
Identity is set to yes
And the seed is set to one
This gives you the option to set the
starting number and the increment
you want the auto number to have
Enterprise Manager



Connecting to the database
Getting through the confusion of
locating databases
Defining the different containers in the
Manager
Connecting to the DBMS

Start the Enterprise Manager

Right Click SQL Server group
Connecting to the DBMS




Click on SQL Server
Registration
Click next
In the text box
labeled Available
Servers type
Sigma.vbnet.matcm
p.ncc.edu
Click Add then next
Connecting to the DBMS






Click on SQL Server login
Username is the first six letters of your
last name and your first initial
Password is vbnet
Click next and click next again
Click Finish
Click close on the next dialog box
Getting through the
concussion




MS SQL Enterprise Manager will display ALL
of the databases located on the server
Access to each database is controlled by the
DBMS
ALL tables are displayed in each database
This includes tables that are used by the
DBMS
The Containers in SQL


Each container
has a specific
role
Student
Databases are
in the
databases
container
Database Container



Every students
database is displayed
Only the database that
the student has
permissions for can be
opened
Click on the database
container with your
username
Student Database
container
Creating the Database



Creating tables
Properties of the data types
Entering and Displaying Data in
database Tables
Creating Tables




Right click on the table container
Click new table
This is a lot like the Access interface
with different data types.
At this points explore the interface and
notice the differences
Properties of Data Fields




Each Data Field has a length property
This sometimes causes a problem with
students who make this field too small
Each data Field can also allow null values
After defining each column and setting all
the properties set the primary key by right
clicking on the field that is to be the key
Entering and Displaying
Data




Right Click the table
Click Open Table
Click Return All Rows
From here you can view and enter
new data into the table
Practice Example 1



Creating a Table in VB .Net
Create a table of customers in a hardware store
Columns will include
–
–
–
–
–


Customer ID
First Name
Last Name
Address
Zip Code
Fill the table with data
Display that data
Connecting to the database
with VB .Net Applications



The Data Provider
The Data Set
The use of the wizard
The Data Adapter




Needed to connect to every DBMS
Must Use the Adapter that represents
the DBMS you are going to use
We are going to use the
SQLDataAdapter Object
Holds the information on how to
The Data Provider



Manipulates data through the use of SQL
statements or commands
Hold connection information to the
Database
Contain two Major components
– Data Adapter Object – updates data in the
dataset
– Connection Object – maintains the connection
The Data Set



Holds data returned from a procedure
or Query performed by the data
adapter
Provides a local “Copy” of the data
from the table on the local machine
Prevents persistent connections with
the database and reduces network
traffic
The use of the wizard

Give Demo on using the wizard
The Data Set




Generating the data set object
The use of the onload event handler
Binding controls
Creating navigation for a database
Generating the Data Set
Object




Once the Data Adapter and the
connection object is created click on
Data in the menu
Click generate dataset
Give a name to the dataset
Click OK
The use of the onLoad
event Handler




Double click the form
Place the fill method in the Load event
handler
This will fill the dataset object with the
data from the table when the form
loads
Example
SQLDataAdapter1.Fill(MyDataSet1)
Binding Controls



Once you create a data set you can then
bind the controls of the form to fields or
columns of the database table
This is done with in the Bindings property in
the text box properties sheet
Under the bindings property click text and
then the field you wish to bind to the text
box
Creating navigation for a
database




Managed by the BindingManagerBase Object
This object contains the Position Property the holds
the current position of the record in the dataset
This is done by adding one to the position attribute
of the object
Methods of this object includes
–
–
–
–
Addnew()
EndCurrentEdit()
CancelCurrentEdit() -> provides a role back feature
RemoveAt() -> deletes current row at given position
Exception Handling
Implementation
Use try catch block much the same in Java
 Can help trouble shoot connections to databases
 Very important to use when filling the dataset
 Example
Try
SqlDataAdapter1.Fill(DataSet11)
Catch ex As Exception
MessageBox.Show("Error will dataset fill")
End Try

Practice Example 2


Connecting a form to your Hardware customers
Create a form with text fields for each column
– Create the data Adapter and Data Connection with the use
of the wizard
– Create the data set object
– Create the onload event handler to fill the data set
– Bind your controls
– Be sure to use exception handling for filling the data set
– Create a Next button to navigate to the next record in the
database
End Of Day 2
VB .Net Seminar Day 3
June 9, 2004
Topics








Adding and Editing Records
Updates and deletes with Bound controls
Classes and Modules in VB .Net
Menus
From design
MDI (Multi Document Interfaces)
Bound and unbound controls in VB .net
Why Unbound controls are used
Adding and Editing
Records

When Adding records
– The Position of the dataset should be set to the
end
– The text boxes should be cleared



The AddNew() Method accomplishes all this
in one method call
The AddNew() Method is part of the
BindingManagerBase Object
When changes are being made, a cancel
button should be provided to “Roll Back”
changes made to the data set
Edit


When Navigating the database the Text
boxes should be disabled to prevent
changes
There should be an edit button that
– Disables the navigation button
– Enables the text boxes for editing
– Provide a cancel button that “Roles Back” the
changes that are made by calling the
CancelCurrentEdit() method of the
BindingManagerBase Object
Update and Deleting
Records

This is done by
– Ending the current edit by calling EndCurrentEdit() in the
BindingManagerBase class
– Updating the DB with Update() in the data adapter
– Cause the dataset to reflect the changes by calling the
AcceptChanges() method in the data set

Deleting the records
– First Ask the user if they are sure they want to delete
– Use the RemoveAt() method of the BindingManagerBase
object
– Update the database with the Update() method
– Have the dataset reflect the change by calling the
AcceptChanges() method of the dataset object
Practice Example 1

With the Example finished yesterday
– Add an Add Button
– Add an Edit Button
– Provide a Save and a delete feature
– Use exception handling for everything
Classes and Modules in
VB .Net



Creating classes in VB .Net
The constructor and Sub procedures in
VB .Net
The use of Modules
Creating Classes in VB
.Net

Classes can be added to a project by
– Right clicking the project container
– Click add
– Click add class

As in Most OOP Languages VB classes have
– Public and private attributes
– Public and private Methods (called functions or
sub procedures)
– Constructors and Destructors (destructors are
not really needed in VB because of garbage
collection)
Modules



Modules can also be added to a
project to provide a place for “Global”
Methods and Identifiers
These Identifiers and methods can be
either public or private
Public Identifiers and methods can be
accessed from all of the forms and
classes in the VB project
Practice Example

For this Example
– Create an Auto Class
– An Auto will have
Three Attributes
 Three Methods
 Two constructors

– Try instantiating the method in the form
Menus


When adding menus to a form you can
type in the choice right into the menu
object
Click event handlers can then be
added for each menu choice in the
same manner as the button
Form Design

Colors are important
– They should be contrast in color
– Ease to read
– Easy on the eyes for long periods of viewing


Should provide easy navigation
Should provide a way to enter the data
easily with just using the keyboard
Multi Document
Interfaces (MDI)





When creating a form in VB .Net you are creating a
class
Every class in vb must be instantiated before it is
used
This is accomplished by using the new keyword
When the instance of the form is created, the form
can then be visible by calling the forms show
method
Closing a form does not exit the program. To exit
the program the End method should be called
Practice Example 3

Create a Form that will be a switch
board
– Add a form to yesterdays example
– Place a button that will open the
customer form
– Place a button that will exit the program
Bound and Unbound
Controls





So far we have seen the benefits of bound controls
These are ok if you intend to use the same SQL
query for the whole application
Once controls are bound to a data set, changes to
the query in the data adapter can not be
accomplished
Unbounded controls give you the power to create
new data adapters with new queries.
Values can then be placed into the query to find
specific records in a database
End Day 3
VB .Net Seminar Day 4
June 10, 2004
Topics






ASP .Net basic applications
ASP .Net Database applications
Web Forms
Crystal Reports
Creating Help Files
Demo with Serial port programming and VB
.Net
 Handout Certificates
ASP .Net basic
Applications
End Day 4
Download