Sunteți pe pagina 1din 54

V isu al Ba sic .

NE T
200 3
Mr. Greg A. dela Cruz, Jr., MSIT
Instructor
The His tory of Vi sual B asi c
 Visual Basic 1.0 appeared in 1991. Up to that point,
the only way to do the Windows programming was
to use C code, and lots of it – you had to write five
pages of C code to produce even a blank window.

 Now if you want a button, you just draw it in place.


It changed Windows programming forever.

 The original VB lived up to its name in the sense


that it was fairly basic. But that changed with new
versions, and with third-party vendors that
introduced Visual Basic eXtension (VBX) controls.
 Visual Basic 3.0 changed the picture again, adding
support for database access with Data Access
Objects (DAO). That turned out to be a major turning
point for VB. Up to that point, many programmers
had thought of it as a toy, but when it started to work
with databases, people took it more seriously.

 VB 4 and 5 were targeted at Windows 95, and


started making the way you write code in VB in more
modular, introducing some object-oriented
programming.

 VB 6 introduced many new features, including


ActiveX Data Objects (ADO), which let you work with
databases in a variety of sources, including on the
Internet.
 VB.Net made VB’s connection to the Web integral
to the whole package. The process of creating
applications changed radically.

 The biggest change in VB.Net 2002 was that Web


programming became as important as Windows
programming

 Now creating Web applications is just easy: You


just draw the user interface you want, add some
code, and run it. VB.Net handles uploading and
maintaining the Web application automatically,
making Web development as easy as Windows
development.
The current version, VB.Net 2003, introduces still more innovations.
Here is a list of what’s new:

 Nonrectangular windows

 Mobile Web applications

 Data providers for ODBC and Oracle

 New code in the .NET Framework

 A folder dialog for your applications to handle Windows folders

 Better support for migrating VB6 code to VB.Net

 The capability to track who has access to application’s coded in


the VB.Net Solution Explorer
 Improved security in the debugger

 The capability to allow side-by-side installations of different


versions of Visual Studio

 The capability to declare a loop variable as part of a For or For


Each loop

 Support for Smart devices (like Pocket PCs)

 Improved security, including automatically scanning user input in


Web applications for embedded (and possible malicious) HTML

 Improved access to Web services


Pu tting V B t o Wo rk
 Visual Studio.Net 2003 supports Visual
Basic, Visual C++, Visual C# and Visual
J#.

ACTIVITIES :
 Opening VB.Net
 Exploring the VB.Net IDE
 Creating a Windows Application
 Creating a Web Application
 Creating a Console Application
Openin g VB.Ne t 2 003
1. Click Start
2. Point to Programs
3. Point to Microsoft Visual
Studio.Net 2003
4. Microsoft Visual Studio.Net 2003
Cre ati ng a Wi nd ows
Ap pl ic ati on
 Click File, New, Project
 Click Visual Basic Projects in the Project
Types box
 Click Windows Application in the
Templates box
 Type the Name of the Project
 Specify the Location
 Type the Name of the Solution
 Click OK
Dif feren ce o f Solu tio n a nd
Pr oj ect
 Solution – It is composed of one or more
projects (e.g. Business Solution : Accounting,
Payroll, Inventory, etc)

 Project – It is composed of forms, references,


assemblies, user controls, classes, etc.

5. Adding Controls to a new Windows Application

7. Adding codes to a Windows application

9. Running a new Windows application


Creati ng a We b
Appl icat ion
 To create a Web application, you need a Web
server with Microsoft Internet Information Server
(IIS) as well as the .NET Framework

 Click File, New, Project


 Click Visual Basic Projects in the Project Types
box
 Click ASP.NET Web Application in the
Templates box
 Click OK
Cre ati ng a Co nso le
App lic at ion
 Click File, New, Project
 Click Visual Basic Projects in the Project
Types box
 Click Console Application in the Templates
box
 Click OK

 There’s no user interface in a console


application. The output appears directly in
a DOS window as text.
Te rms: Mo dule, Su b
Ma in ()
 Module – It is what VB uses to hold code
not directly associated with a user interface

 Sub Main() - The VB’s way of creating a


Sub procedure. A sub procedure Main() is
automatically run when a console
application starts.
Sa mp le Co de
Note: Numbers not included
1. Module Module1
2. Sub Main()
3. Console.WriteLine("Hello World!")
4. Console.WriteLine(“Press any key…")
5. Console.Readline()
6. End Sub
7. End Module

Lines 3 & 4 displays Hello World! and Press any key… respectively
Line 5 waits for the user input before exiting

Tip: Use Ctrl F5 in running the application to avoid


Console.Readline() statement
 Historyof VB
 What’s new in VB.Net 2003
 Compositions of VB.Net 2003
 The VB.Net IDE
 Created sample Windows, Web and
Console applications
VB.Net from a
Programmer’s Perspective
 In the early days of VB programming, a great deal of the
functionality built in to VB was stored in Windows
Dynamic Link Libraries (DLL).

 This way of doing things became a problem when you were


finished creating your applications and wanted to distribute
them to others. You may have the needed DLL files on your
computer, but the people who wanted to use your new
application may not.

 That meant that you would have to package the needed DLLs
in your installation package to be installed on the target
machine with your application.

 That wasn’t good enough because you had to include those


huge DLLs in your installation package, that package could
easily run to 40MB or more.
 The NET Framework solved this problem by
including all the support your application
needs and packaging it as part of Windows

 That meant the code in those DLLs that you


originally needed to distribute to target
system along with your application was
already installed in those target systems

 This made your installation package much


smaller
Two Major Parts of the
.NET Framework
 The .NET Framework Class Library – it holds an
immense amount of prewritten code that all the
applications you create with Visual Basic, Visual
C++, Visual C#, Visual J# and other Visual Studio
Languages build on

 The Common Language Runtime – it runs your


VB.Net application.
PROCESS

OUTPUT
VB.Net Application

Compile

MS Intermediate
Language

Common Language
Runtime (CLR) Binary Codes
 The CLR can run programs created not only in VB but also
in Visual C++, Visual C#, Visual J# and other Visual
Studio languages

 Applications written in those other languages are also


translated into IL.

 This means they all look alike to the Common Language


Runtime

 Visual Studio applications, however, are built using IL, and


they’re built to interact with the CLR runtime. For that
reason, they’re called ASSEMBLIES.

 ASSEMBLY – holds the IL modules for your application.


When you create an application in VB.NET and run it,
VB.NET creates one or more assemblies, which are run by
the CLR. That is, assemblies are the way your
applications interact with the .NET Framework instead of
the .EXE or DLL files of VB6.
More about ASSEMBLIES….
What’s in a .NET assembly?

 The core of the assembly is made of


modules, which are internal files of IL
code, ready to run.

 That’s how VB.NET stores the IL it creates –


in modules inside assemblies
ASSEMBLY

MODULE/S

MSIL Codes

An illustration….
DLL
– acronym for Dynamic Link
Libraries
- a set of Windows functions
designed to perform a
specific class of operations.

<Back
MASTERING THE Visual Basic Language:
DATA, OPERATORS, CONDITIONALS &
LOOPS

• Keywords and Statements


•Commenting Code
•Constants, Enumerations and Variables
•Data Types
•Data Type Conversion
•Conditionals
•Loops
Visual Basic Keywords
• KEYWORDS are reserved words that you can
use in your code and that have special
meaning in VB.

Examples:
And Ansi As
Byte ByVal Boolean
ByRef Catch CBool
CByte CChar CDate
Visual Basic.Net Statements
A VB.NET statement is a complete VB instruction. Statement can
contain:

• Keywords – words reserved for VB’s use

• Operators – Symbols used to perform operations.

• Variables – Symbolic names given to values stored in memory


and declared using the Dim keyword.

• Literal Values – Simple or numeric text values, such as 14 or


“Greg”

• Constants – The same as variables, except that you can’t


change the value stored in a constant.

• Expressions – Combinations of any of the items in this list that


VB can evaluate to yield a value. For example, the expression
10 + 10 yields the value 20
Data Types
Type Storage Size Value Range
Boolen 2 bytes True or False
Byte 1 byte 0 to 255 (unsigned)
Char 2 bytes 0 to 65, 535 (unsigned)
Date 8 bytes January 1, 0001 to December 31, 9999
Decimal 16 bytes
Double 8 bytes Double precision floating-point
Integer 4 bytes -2,147,483,648 through 2,147,483,647
Long 8 bytes -9,223,372,036,854,775,808 through
9,223,372,036,854,775,807
Short 2 bytes -32,768 through 32,767
Single 4 bytes Single precision floating-point
String Depends on 0 to approximately 2 billion Unicode
implementing characters.
platform
Efficient Use of Data Types
• Undeclared variables and variables declared without a data
type are given the Object data type. This makes it easy to
write programs quickly, but it can cause them to execute
more slowly.

• Specifying data types for all your variables is known as


strong typing. Using strong typing has several advantages:
• It enables IntelliSense™ support for your variables. This
allows you to see their properties and other members as
you type in the code.

• It allows the compiler to perform type checking. This


catches statements that can fail at run time due to errors
such as overflow. It also catches calls to methods on
objects that do not support them.

• It results in faster execution of your code.


• For variables that never contain fractions, the integral data
types are more efficient than the nonintegral types. In
Visual Basic .NET, Integer is the most efficient numeric
type.

• Use the Dim statement to declare a variable of a specific


type. You can simultaneously specify its accessibility by
using the Public, Protected, Friend, or Private keyword, as
in the following example:

Private X As Double ' X is a double-precision floating point.

Protected S As String ' S is a character string.


Declaring a Variable
Dim ID as Integer
Dim Name as String

Dim ID as Integer = 1234


Dim Name as String = “Greg”

Dim Name, Address as String

Dim ID as Integer = 1234, Name as String = “Greg”


Variable Prefixes
If you come across a variable in the middle of
someone else’s code, a prefix makes it easy to know
what data type the variable can hold.

Type Prefix Type Prefix


Boolean bln Object obj
Byte byt Single sng
Date(Time) dtm String str
Double dbl User-defined udf
Error err
Integer int
Long lng
Examples
Dim blnFinished as Boolean

Dim strAddress as String

Dim dblAccount as Double


Working with Strings
Some Useful String Handling Functions and Methods

String Conversion: StrConv, CStr, String.ToString()


Example (CStr):

REM Declare and initialize variable of type Byte


Dim bytNo As Byte = 10
REM Declare and initialize variable of type String
Dim strNo As String
REM Convert Byte to String
strNo = CStr(bytNo)
String Conversion: StrConv, CStr, String.ToString()
Example (CStr):

REM Declare and initialize variable of type Byte


Dim bytNo As Byte = 10
REM Declare and initialize variable of type String
Dim strNo As String
REM Convert Byte to String
strNo = CStr(bytNo)
Declaring Constants
• Example:

Const Pi = 3.14159
Creating Enumerations
• You can make use of Enumerations when you want to
group a number of constants.

Example:
Enum Days
Sunday = 0
Monday = 1
Tuesday = 2
Wednesday = 3
End Enum

To use the enumeration,

txtDay.text = “Monday is day “ & Days.Monday


Declaring Static and Dynamic Arrays
• Array is a single variable with
several elements

• It allows you to group your data into


a single programming construct

Example:

Dim bytScores(20) as Byte


bytScores(0) = 25
bytScores(1) = 30
bytScores(2) = 15
To access the first content/element of
bytScores

txtScore1.text = bytScores(0)
Creating a Dynamic Array
• Dynamic array is useful if you
don’t know how much data you
want to store ub ab array before
your program runs.

• Dynamic array can be


redimensioned at runtime.
Example

Dim bytScores() as Byte


Redim bytScores(20)
bytScores(0) = 25
bytScores(1) = 30
Preserving the Array Contents
If you want to add new array contents without
erasing the existing ones, use the PRESERVE
keyword

Example:
Dim bytScores() as Byte
Redim bytScores(20)
bytScores(0) = 25
bytScores(1) = 30
bytScores(20) = 77
Redim Preserve bytScores(25)
bytScores(21) = 80
Making Decision with If and Select
Statements
 IF Statement Example

If bytScores(0)<75 then
txtMark.text = “Failed”
ElseIf bytScores(0)>=75 and bytScores(0)<80 Then
txtMark.text = “Passed”
ElseIf bytScores(0)>=80 and bytScores(0)<86 Then
txtMark.text = “Very Good”
ElseIf bytScores(0)>=86 and bytScores(0)<96 Then
txtMark.text = “Excellent”
ElseIf bytScores(0)>=96 and bytScores(0)<=100 Then
txtMark.text = “Extra Ordinary”
Else
txtMark.text =“Invalid”
End If
Select Statement Example
Select Case bytScores(0)
Case Is <75
txtMark.text = “Failed”
Case 75 to 79
txtMark.text = “Passed”
Case Else
txtMark.text = “Invalid”
End Select
For, For Each, While and Do loops
Use use LOOPS to execute a series of
statements repeatedly.

Example : For
Dim bytCounter as Byte
For bytCounter = 1 to 100
cboScore.Items.Add bytCounter
Next bytCounter
Example: For Each

Dim bytScores(2) as Byte, bytItem as Byte


Dim bytTotal as Byte
Scores(0) = 10
Scores(1)=20
Scores(2) = 30
For Each bytItem in Scores
bytTotal = bytTotal + Score
Next bytItem
Example: While

Dim sngAccount as Single


sngAccount = 50000
While sngAccount>0
sngAccount = sngAccount-10000
End While
Example: Do While …. Loop

Dim sngAccount as Single


sngAccount = 50000
Do While sngAccount>0
sngAccount = sngAccount-10000
Loop
Example: Do Until …. Loop

Dim sngAccount as Single


sngAccount = 50000
Do Until sngAccount = 0
sngAccount = sngAccount-10000
Loop
Example: Do …. Loop While

Dim sngAccount as Single


sngAccount = 50000
Do
sngAccount = sngAccount-10000
Loop While sngAccount > 0
Example: Do …. Loop Until

Dim sngAccount as Single


sngAccount = 50000
Do
sngAccount = sngAccount-10000
Loop While sngAccount = 0
Using the With Statement
 You use a With statement to make a Visual Basic object the
default object for a set of enclosed Visual Basic Statements

Programmatically setting the Properties of a textbox without


using the WITH statement:
txtName.Height=30
txtName.Width=100
txtName.Text=“Greg”

Programmatically setting the Properties of a textbox using the


WITH statement:
With txtName
.Height=30
.Width=100
.Text=“Greg”
End With
• Keywords and Statements
• Commenting Code
• Constants, Enumerations and Variables
• Data Types
• Data Type Conversion
• Conditionals
• Loops

S-ar putea să vă placă și