| |
|
|
| |
|
|
| |
| |
|
|
BA 371 - VB.Net Introduction
The material presented in these and the following labs represents a
short course in VB 2005 for people with previous (C or Java, mostly)
programming experience. It only covers the basic aspects of VB 2005 and
leaves many issues undiscussed.
The labs are self guided in that both questions and their answers are
provided. It is really up to you to learn this material and get the
basic aspects of this language under control.
Although you could 'run' through these labs quickly by cutting the
various segments of code from these pages and pasting them into the VS
2005 IDE, you would learn very little doing so. We therefore strongly recommend that you study
this material while exercising it.
Carefully read the examples, questions and answers.
Cut and paste code, but carefully read and try understanding the code before you compile and run.
Try answering the various questions you encounter yourself before checking out the answers.
Experiment with the codes! Don't take them for granted. Play and
experiment with them and try to understand the resultant behavior.
Some Basic History:
1964: J. Kemeny and T. Kurtz (Dartmouth College) designed BASIC as a stepping-stone to languages such as FORTRAN or ALGOL.
1970s: Bill Gates and Paul Allen develop a BASIC interpreter for the Altair.
1970s - 1990s: Quick BASIC --> Visual BASIC (VB) --> compiled language.
.NET 2005 by Microsoft Corporation: Unification through layering:
.NET 2005 Framework:
Common Language Runtime (CLR).
Foundation classes.
.NET 2005 servers.
.NET 2005 devices.
Similar approaches:
CORBA (Common Object Request Broker Architecture) by Open Management Group.
J2EE (Java 2 Enterprise Edition) by Sun Microsystems.
First VB application: Hello, World...:
Public Class Hello
Shared Sub Main()
System.Console.WriteLine("Hello, World...")
End Sub
End Class
Store as ASCII (text) in file; e.g., foo.vb
Start a command-line (DOS) window off of the Start
--> All Programs --> Microsoft Visual Studio 2005 --> Visual
Studio Tools --> Visual Studio 2005 Command Prompt.
(NOTE!!! a regular 'vanilla' command window may not work).
Compile the source into an executable:
>vbc foo.vb
run the executable:
>foo
Visual Studio 2005 Integrated Development Environment
(IDE)
Start VS 2005. (NOTE!!! If this is the first time that you
start VS 2005, then you will need to select Visual Basic as the primary
area of work.)
Using File --> New Project..., set up a new Console Application.
To do this,click or expand the Visual Basic area of the tree on
the left side of the New Project
dialog box.
On the right side of the screen select Console Application.
Name the application or leave the default name of ConsoleApplication1.
Select OK.
(NOTE!!! If you intend to save your lab, then select File --> Save All, pick a
location and click Save.
Notice that in the Solutions Explorer window, a
project is part of a solution. In VS 2005. solutions are complete,
integrated software systems which may contain several executable files,
the source codes of which are managed in projects.
Erase the dummy source code from the edit window, and replace it with the code from the previous exercise. Save all.
Close the project and exit VS 2005. Then startup VS 2005
again and make sure all components are still in place (reload your foo.vbproj
project file project from File --> Recent projects.
Double-click on the foo.vb source file entry in the Solutions
Explorer window to load the file into the source code editor
window. If this does not work, carefully go over steps 2-5 again.
Compile the project and build the executable (Build --> Build "Application Name"). In the Error List there will be an error (if you do not run into this error, make sure that you started a Console Application, not a Windows Application):
Error 1 'Sub Main' was not found in 'ConsoleApplication1.foo'.
Reason: no top class known. In Solution Explorer
window set ConsoleApplication1 --> Properties -->
Application Tab --> Startup object to Hello or Sub
Main).
Recompile and notice how ConsoleApplication1.exe
is created in ...\foo\bin\release\ directory.
Start a (normal) command-line (DOS) window, cd to the ...\foo\binrelease\ directory and run the executable:
>foo
You may run the executable directly from the IDE (Debug
--> Start without Debugging). Do you notice anything different?
Can you explain this difference?
Calling a subroutine (function in C):
Replace the code in foo.vb with the following:
Public Class Hello
Shared Sub Main()
System.Console.WriteLine("Hello World...")
System.Console.WriteLine()
do_this()
System.Console.WriteLine()
End Sub
Shared Sub do_this()
Dim first As Int16 = 32767
Dim second As Int16 = 0
Dim result As Int16
result = first + second
System.Console.WriteLine(CStr(result))
System.Console.WriteLine()
System.Console.WriteLine("Press Enter to Exit")
System.Console.ReadLine()
End Sub
End Class
Code contains a subroutine (a 'function' in C) do_this().
do_this() declares three 16-bit (2-byte)
integers. (Overview of VB data types).
first is set to 32767.
second is set to 0.
result is used to store the result of first + second.
do_this() is called from Main().
For C/C++/Java programmers: notice how do_this() is not declared void for either its parameter list and its return value.
Compile and run from the IDE.
Now modify the program so that second is initialized to 1, rather than 0.
Compile and run. What do you observe? (What behavior would you expect in
C?).
Change result into an Int32, recompile and run.
Also change either second or first into an Int32. Recompile and run. Can you explain what just happened?
Functions and Subroutines in VB.NET:
Notice how in the above code do_this() is called from inside Main().
Subroutines: execute pieces of code, then 'go away':
Sub my_sub()
some code
some more code
End Sub
Function my_function()
some code
some more code
return some_value
End Function
Example:
Public Class Hello
Shared Sub Main()
dim my_var as int16
System.Console.WriteLine("Hello World...")
System.Console.WriteLine()
my_var = do_this()
System.Console.WriteLine(CStr(my_var))
System.Console.WriteLine()
System.Console.WriteLine("Press Enter to Exit")
System.Console.ReadLine()
End Sub
Shared Function do_this() as int16
Dim first As Int16 = -99
Return first
End Function
End Class
This begs the question of why to use subroutines at
all??
Answer: BASIC has always been a 'permissive' language; i.e., lenient on
type checking. The new .NET approach requires strict type checking (as
do C/C++/Java). To enforce this:
From now on, !!! ALWAYS have the following lines at the top of your code !!!
Option
Strict On
Option Explicit On
With these options set, modify the above program such that the function do_this() returns an Int32. What do you observe? Try the code without the options set. Do you understand the resulting behavior?
Passing parameters by value (ByVal) or by Reference (ByRef):
Suppose we have a variable Short foo in Main() and we want the function or subroutine do_this() to multiply its value.
Public Class Hello
Shared Sub Main()
Dim my_var As Short = 10
System.Console.WriteLine("Hello World...")
System.Console.WriteLine()
do_this(my_var)
System.Console.WriteLine(CStr(my_var))
System.Console.WriteLine()
System.Console.WriteLine("Press Enter to Exit")
System.Console.ReadLine()
End Sub
Shared Sub do_this(ByVal foo As Short)
foo = foo * CShort(10)
End Sub
End Class
What do you observe?
Notice how strict type checking forces you to convert '10' into a short using Cshort(10).
- foo is not multiplied by 10 when printed out in Main(). Cause: When passing a variable by value (ByVal), only the value of the variable is passed, not the variable itself (a memory copy local to the function/subroutine is created) (default in C and Java).
To access the passed variable itself, pass by reference (ByRef) (pointer/address in C).
- Notice that regardless how passed (ByVal or ByRef), the variable names in the caller and the function/subroutine can be different. ByRef implies that memory is not copied (only the address (pointer) is passed); the name of the local reference to that memory is immaterial.