Visual Basic .NET
Mi Gyeong Gwak
CSC 415 – Programming Languages
Dr. William F. Lyle
November 4, 2014
General Information of Visual Basic .NET
Visual Basic is an object-oriented and event-driven programming language for developing applications designed to run in Microsoft Windows and teaching purposes that derives from BASIC (Beginner's All-purpose Symbolic Instruction Code). It is tightly integrated with the .NET initiative which is “a collection of Web services developed by Microsoft Corporation that are intended to reposition the company as a provider of Internet-distributed services, including software maintenance and upgrades and transparent access to one’s data, files, and software from any type of device at any location” (Webster’s New World & Trade).
BASIC was invented by two mathematicians, John Kemeny and Thomas Kurtsz, at Dartmouth College in 1964 in order to provide a simple programming language for all students. When it was first developed, computers were expensive and difficult to use, and only a few computer languages were in existence, such as FORTRAN and ALGOL (Encyclopedia of Computer Science). Moreover some computers required the use of small strips of paper or punched cards in order to program. The first BASIC programs ran on the General Electric 225, but then it started to spread by G.E. to other machines around 1970. The committee of American National Standard Institute (ANSI) started working on two standards: minimal BASIC and Standard BASIC. In 1975, Bill Gates and Paul Allen created interpreted BASIC in order to run in limited memory situations and for making debugging easier (Marconi 1). In the late 1980s, graphical user interfaces (GUIs) and Microsoft Windows became popular, but it was difficult to create Window applications. Microsoft revived BASIC in 1991 using its simplicity and general syntax by introducing Visual Basic. “In the years since, Microsoft has continued to improve Visual Basic…These renovations include making BASIC object oriented and fully event driven, and overcoming the limitations of being interpreted, allowing programmers to generate a compiled executable code” (encyclopedia.com). Visual Basic supports most constructions of common programming languages currently in use, and some essential features will be introduced in this report: names, bindings, scopes, data types, operators, control flow, objects, and error types.
Overview of Visual Basic .NET Design, Syntax, and Semantics
Names
Visual Basic .NET uses a similar name format with other programming languages. A name must begin with an alphabetical character or an underscore and must be less than 1023 characters long. If it starts with an underscore, it must include at least one alphabetical character or decimal digit. Also, it is case-insensitive which means that two names that differ only in alphabetic case usage and they are treated as the same name. There are two kind of keywords: reserved and unreserved. Reserved keywords cannot be used as names for elements such as variables or procedures. However, they can be enclosed by brackets ([ ]) and used as escaped names. Unreserved keywords can be used as names for elements. However, using any keywords as names is not recommended, “because it can make your code hard to read and can lead to subtle errors that can be difficult to find” (Microsoft Developer Network).
Bindings
A binding in Visual Basic is an association between two object variables. Early binding means that an object is assigned to a specific object type. Late binding means that an object is assigned to an object that can hold references to any object. Early bound objects have more advantages because they “allow the compiler to allocate memory and perform other optimizations before an application executes… it enables useful features such as automatic code completion and Dynamic Help… reduces the number and severity of run-time errors” (Microsoft Developer Network). The following code is an example of an early bound object and a line starting with the comment symbol (‘), designating that line as a comment:
' Create a variable to hold a new object.
Dim FS As System.IO.FileStream
' Assign a new object to the variable.
FS = New System.IO.FileStream("C:\tmp.txt",
System.IO.FileMode.Open)
Scopes
Visual Basic includes block scope, procedure scope, module scope, and namespace scope. “A block is a set of statements enclosed within initiating and terminating declaration statements, such as the following” (Microsoft Developer Network):
-
Do and Loop
-
For [Each] and Next
-
If and End If
-
Select and End Select
-
SyncLock and End SyncLock
-
Try and End Try
-
While and End While
-
With and End With
The following block of code is an example of using If and End If statements, and the integer variable cube cannot be referred to out of the block.
If n < 1291 Then
Dim cube As Integer
cube = n ^ 3
End If
A variable declared within a procedure cannot be referred outside of the procedure, and it is also known as local variable. The module scope means that a declared variable within modules, classes, and structures cannot be referred to outside of those structures. The Private modifier makes a variable accessible to every procedure in that module. For example, the string variable strMsg defined in the module can be used in all the procedures in the module:
' Put the following declaration at module level (not in any procedure).
Private strMsg As String
' Put the following Sub procedure in the same module.
Sub initializePrivateVariable()
strMsg = "This variable cannot be used outside this module."
End Sub
' Put the following Sub procedure in the same module.
Sub usePrivateVariable()
MsgBox(strMsg)
End Sub
Any variable declared using the Friend or Public keyword in a module scope is available throughout the namespace in which the variable is declared. Using a local variable would be good for a temporary calculation since it can avoid name conflicts. Minimizing scope helps reduce memory consumption and erroneously referring to the wrong variable (Microsoft Developer Network).
Data Types
Visual Basic provides numeric, character, miscellaneous, and composite data types. Numeric data types can be divided as integral and non-integral types. Integral types representing integers like whole numbers include signed integral types and unsigned integral types. The signed integral types can represent negative integers and they are SByte (8-bit), Short (16-bit), Integer (32-bit), and Long (64-bit). The unsigned integral types represent only non-negative integers and they are Byte (8-bit), UShort (16-bit), UInteger (32-bit), and ULong (64-bit). Non-integral types represent numbers that have both integer and fractional parts and includes Decimal (128-bit fixed point), Single (32-bit floating point), and Double (64-bit floating point).
Character data types include Char type which holds a single character (16-bit Unicode) and String type which holds indefinite number of characters. The default value of Char is the 0 code point and Char() allows holding multiple characters as an array. The default value of String is null. Char and String can be converted to an Integer using the Asc or AscW function, and an Integer value can be converted to a Char using the Chr or ChrW function. Unicode Characters defined as “The first 128 code points (0–127) of Unicode correspond to the letters and symbols on a standard U.S. keyboard. These first 128 code points are the same as those the ASCII character set defines. The second 128 code points (128–255) represent special characters, such as Latin-based alphabet letters, accents, currency symbols, and fractions” (Microsoft Developer Network).
Miscellaneous data types which are not oriented numbers or characters include Boolean, Date, and Object type. Boolean type is an unsigned value that contains two-state values either True or False. Date type “is a 64-bit value that holds both date and time information. Each increment represents 100 nanoseconds of elapsed time since the beginning (12:00 AM) of January 1 of the year 1 in the Gregorian calendar” (Microsoft Developer Network). Object type (32-bit) points to an object instance which includes value types (Integer, Boolean, and structure instances) and reference types (String, Form, and array instances). Object type allows the programmers to store data of any data type, even though it causes the application to take more execution time (Microsoft Developer Network).
Composite data type assembles items of different types such as structures, arrays, and classes. A structure is user-defined type (UDT) that was supported in the previous versions of Visual Basic. Moreover, “structures can expose properties, methods, and events. A structure can implement one or more interfaces, and you can declare individual access levels for each field. Structures are useful when you want a single variable to hold several related pieces of information” (Microsoft Developer Network). This is an outline of the declaration of a structure:
[Public | Protected | Friend | Protected Friend | Private] Structure structname
{Dim | Public | Friend | Private} member1 As datatype1
' ...
{Dim | Public | Friend | Private} memberN As datatypeN
End Structure
Array is a set of data that has a same name, but are separated by index values. Array in Visual Basic can have up to 32 dimensions, and multiple dimensions can be represented by separating the highest index value of each dimension by commas. The following example is declaring a three-dimensional array:
Dim airTemperatures(99, 99, 24) As Single
Programmers can represent nest values using braces ({}) within braces. The following example is initializing a multidimensional array variable by using array literals:
Dim numbers = {{1, 2}, {3, 4}, {5, 6}}
Dim customerData = {{"City Power & Light", "http://www.cpandl.com/"},
{"Wide World Importers", "http://wideworldimporters.com"},
{"Lucerne Publishing", "http://www.lucernepublishing.com"}}
' You can nest array literals to create arrays that have more than two
' dimensions.
Dim twoSidedCube = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}
All classes cannot be composed with a single data type. If one class instance variable is assigned to another, they have to have the same data type and same class instance in memory. More information will be available in Objects section.
Constants and Enumerations which are a set of related constants are available in Visual Basic. Declaring several constants with the Const statement is shown below:
Const conPi = 3.14159265358979
Public Const conMaxPlanets As Integer = 9
Const conReleaseDate = #1/1/1995#
Const conPi2 = conPi * 2
The following example is declaring an enumeration:
Public Enum MyEnum As Byte
Zero
One
Two
End Enum
Type characters and type conversion keywords in Visual Basic are shown in Appendix I and II.
Operators
Visual Basic provides arithmetic, comparison, concatenation, and logical operators. Arithmetic operations use addition, subtraction, negation, multiplication, division, exponentiation operator just like other common programming languages. A bit-shift operation achieves arithmetic shift on a bit pattern of a data using the >> operator or << operator. Shifting an Integer value is shown below:
Dim lResult, rResult As Integer
Dim pattern As Integer = 12
' The low-order bits of pattern are 0000 1100.
lResult = pattern << 3
' A left shift of 3 bits produces a value of 96.
rResult = pattern >> 2
' A right shift of 2 bits produces value of 3.
Comparison Operators for numeric values are the same as in other programming languages, except for using the < > operator for expressing the inequality of two expressions. The Like operator is used to compare strings and an example is shown below:
testCheck = "F" Like "[A-Z]"
' The following statement returns False (does "F" NOT occur in the
' set of characters from "A" through "Z"?)
Comparing objects requires the Is operator or IsNot operator and an example is shown below:
Dim a As New classA()
Dim b As New classB()
If a IsNot b Then
' Insert code to run if a and b point to different instances.
End If
Comparing object type uses the Type…Is expression, and the syntax is as follows:
TypeOf Is
Two concatenation operators, the + and &, combine multiple strings into a single string. However, the + operator has the complex functions of adding numeric operands, concatenating, signaling a compiler error, or throwing a run-time exception. Logical operators includes the Not operator that performs logical negation, And operator that performs logical conjunction, Or operator that performs logical disjunction, and Xor operator that performs logical exclusion. For example,
Dim a, b, c, d, e, f, g As Boolean
a = 23 > 14 And 11 > 8
b = 14 > 23 And 11 > 8
' The preceding statements set a to True and b to False.
c = 23 > 14 Or 8 > 11
d = 23 > 67 Or 8 > 11
' The preceding statements set c to True and d to False.
e = 23 > 67 Xor 11 > 8
f = 23 > 14 Xor 11 > 8
g = 14 > 23 Xor 8 > 11
' The preceding statements set e to True, f to False, and g to False.
These logical operators are expressed as a letter instead of using special characters as is done in other programming languages. Short-circuiting logical operations contain the AndAlso operator and the OrElse operator. Bitwise operations compare each binary bit of two integral values. The And, Or, and Xor operators also used for bitwise operations (Microsoft Developer Network).
Control Flow
Control structures regulate the flow of a program’s execution. There are several types of control structures. Decision Structures includes the If…Then…Else construction, Select…Case construction, and Try…Catch…Finally construction. The following illustration expresses the flow of decision structure.
Here is an example of If…Then…Else statement:
Dim count As Integer = 0
Dim message As String
If count = 0 Then
message = "There are no items."
ElseIf count = 1 Then
message = "There is 1 item."
Else
message = "There are " & count & " items."
End If
Visual Basic loop structures make the program run a block of code repetitively. The following illustration show the flow of a loop structure:
There are the While…End While construction, Do…Loop construction, For…Next construction, and For Each…Next construction for the loop structures. Here is an example of a For Each…Next statement:
' Create a list of strings by using a
' collection initializer.
Dim lst As New List(Of String) _
From {"abc", "def", "ghi"}
' Iterate through the list.
For Each item As String In lst
Debug.Write(item & " ")
Next
Debug.WriteLine("")
'Output: abc def ghi
In addition, the Using…End Using construction allows programmers to acquire a resource such as an SQL connection, and the With…End With construction allows programmers to specify an object reference once and then be accessible in a series of statements. Nested control structures are also available in Visual Basic (Microsoft Developer Network).
Objects
Visual Basic .NET is object-oriented programming language. An object can be considered as a unit or prefabricated building block of data combination. Objects in Visual Basic can be used as controls, forms, and objects from other applications. Each object is defined by a class and an instance of the class. Multiple instances can be created within a class, but their variables and properties can be changed independently.
A member of an object is accessed by separating the name of the object variable and the name of the member by a period (.) just like:
warningLabel.Text = "Data not saved"
Fields which is a member variable and properties are information of an object. An object can perform an action by a method such as Add to add a new entry or Start to begin a timer object. An object can recognize an action by an event such as clicking the mouse or pressing a key. An instance member belongs to a particular instance, while a shared member is able to belong to the class as whole (Microsoft Developer Network).
Objects can be related to each other either in a hierarchical relationship or containment relationship. A hierarchical relationship is built when classes are derived from the class that they are based on. The Containment relationship means that a container object logically encapsulates other objects, such as collections (Microsoft Developer Network).
Visual Basic allows a derived class to inherit the fields, properties, methods, events, and constants defined in the base class. The Inheritance Modifier includes the Inherits statement, NotInheritable modifier that is prevented from using a class as a base class, and MustInherit modifier that allows a class as a base class only. An inherited property or method that behaves differently in the derived class is overridden. The Overridable, Overrides, NotOverridable, and MustOverride modifiers are used to control overrides. The MyBase keyword with a variable is used to refer to the base class, and the MyClass keyword with a variable is used to refer to the current instance of a class (Microsoft Developer Network).
Error Types
Errors or exceptions happen within three categories: syntax errors, run-time errors, and logic errors. Syntax errors are the most common type of errors, but the code editor helps reduce syntax errors in Visual Basic. Run-time errors occur after compiling the program and arise mostly when carrying out the Open function. Logic errors appear often when unwanted or unexpected results are received by the user (Microsoft Developer Network).
Evaluation of Visual Basic .NET
Readability
Visual Basic .NET programs are very readable and understandable. Most of the code examples shown above have an English-like appearance. Particularly, keywords that control structures and data types are using a clear language and this helps the readers determine the role of a variable easily. Also, the initial and terminal statements of control structures are simple enough to facilitate easy understanding of when a block starts and ends and any repetition defined in the program.
Writability
Visual Basic .NET programs are also writable. It is a visual programming language and is used in an Integrated Development Environment (IDE). It is very simple to write names and controls using the property box of the IDE and to create a GUI Window form applications by using drag-and-drop. Visual Basic’s control structure is intuitive, but the programmer needs to be careful to type a correct termination for each initialization. Variables can be declared by using the Dim statement, a name and a data type and can be as simple as:
Dim My_Counter As Integer
Visual Basic .NET code is expressed like English, and programmers can easily write and understand a program.
Reliability
Visual Basic .NET includes integer overflow checking by default. Programmers can control type checking by using the Option Strict statement at the beginning of the code. Setting Option Strict will force early binding and perform effectively (Microsoft Developer Network). Structured exception handling tests specific piece of the code and will be excepted if any exceptions occur. It can be done by the Try…Catch…Finally control structure and is more reliable than unstructured exception which can be done by either of the On Error Go To statement, the Resume statement, or the Error statement.
Cost
The Visual Studio IDE is used for developing Visual Basic .NET. Two main editions of IDE are currently Microsoft Visual Studio 2013, which costs around $300 to purchase, and Visual Studio Express Edition 2013, which is available for free. Migration from Visual Basic 6.0 to .NET also can be done by Microsoft’s partners for free. ArtinSoft’s Visual Basic Upgrade Companion, Great Migrations Studio, and VB Migration Partner tool are available in order to do migration at no cost.
Appendix I. Type Characters in Visual Basic
-
Identifier Type Characters
Identifier type character
|
Data type
|
Example
|
%
|
Integer
|
Dim L%
|
&
|
Long
|
Dim M&
|
@
|
Decimal
|
Const W@ = 37.5
|
!
|
Single
|
Dim Q!
|
#
|
Double
|
Dim X#
|
$
|
String
|
Dim V$ = "Secret"
|
-
Literal Type Characters
-
Default Literal Types
Textual form of literal
|
Default data type
|
Example
|
|
Numeric, no fractional part
|
Integer
|
2147483647
|
|
Numeric, no fractional part, too large for Integer
|
Long
|
2147483648
|
|
Numeric, fractional part
|
Double
|
1.2
|
|
Enclosed in double quotation marks
|
String
|
"A"
|
|
Enclosed within number signs
|
Date
|
#5/17/1993 9:32 AM#
|
|
-
Forced Literal Types
Literal type character
|
Data type
|
Example
|
S
|
Short
|
I = 347S
|
I
|
Integer
|
J = 347I
|
L
|
Long
|
K = 347L
|
D
|
Decimal
|
X = 347D
|
F
|
Single
|
Y = 347F
|
R
|
Double
|
Z = 347R
|
US
|
UShort
|
L = 347US
|
UI
|
UInteger
|
M = 347UI
|
UL
|
ULong
|
N = 347UL
|
C
|
Char
|
Q = "."C
|
-
Hexadecimal and Octal Literals
Number base
|
Prefix
|
Valid digit values
|
Example
|
|
Hexadecimal (base 16)
|
&H
|
0-9 and A-F
|
&HFFFF
|
|
Octal (base 8)
|
&O
|
0-7
|
&O77
|
|
Appendix II. Type Conversion Keywords in Visual Basic
Type conversion keyword
|
Converts an expression to data type
|
Allowable data types of expression to be converted
|
|
CBool
|
Boolean Data Type (Visual Basic)
|
Any numeric type (including Byte, SByte, and enumerated types), String, Object
|
|
CByte
|
Byte Data Type (Visual Basic)
|
Any numeric type (including SByte and enumerated types), Boolean, String,Object
|
|
CChar
|
Char Data Type (Visual Basic)
|
String , Object
|
|
CDate
|
Date Data Type (Visual Basic)
|
String , Object
|
|
CDbl
|
Double Data Type (Visual Basic)
|
Any numeric type (including Byte, SByte, and enumerated types), Boolean, String,Object
|
|
CDec
|
Decimal Data Type (Visual Basic)
|
Any numeric type (including Byte, SByte, and enumerated types), Boolean, String,Object
|
|
CInt
|
Integer Data Type (Visual Basic)
|
Any numeric type (including Byte, SByte, and enumerated types), Boolean, String,Object
|
|
CLng
|
Long Data Type (Visual Basic)
|
Any numeric type (including Byte, SByte, and enumerated types), Boolean, String,Object
|
|
CObj
|
Object Data Type
|
Any type
|
|
CSByte
|
SByte Data Type (Visual Basic)
|
Any numeric type (including Byte and enumerated types), Boolean, String,Object
|
|
CShort
|
Short Data Type (Visual Basic)
|
Any numeric type (including Byte, SByte, and enumerated types), Boolean, String,Object
|
|
CSng
|
Single Data Type (Visual Basic)
|
Any numeric type (including Byte, SByte, and enumerated types), Boolean, String,Object
|
|
CStr
|
String Data Type (Visual Basic)
|
Any numeric type (including Byte, SByte, and enumerated types), Boolean, Char,Char array, Date, Object
|
|
CType
|
Type specified following the comma (,)
|
When converting to an elementary data type (including an array of an elementary type), the same types as allowed for the corresponding conversion keyword
When converting to a composite data type, the interfaces it implements and the classes from which it inherits
When converting to a class or structure on which you have overloaded CType, that class or structure
|
|
CUInt
|
UInteger Data Type
|
Any numeric type (including Byte, SByte, and enumerated types), Boolean, String,Object
|
|
CULng
|
ULong Data Type (Visual Basic)
|
Any numeric type (including Byte, SByte, and enumerated types), Boolean, String,Object
|
|
CUShort
|
UShort Data Type (Visual Basic)
|
Any numeric type (including Byte, SByte, and enumerated types), Boolean, String,Object
|
|
Works Cited
"Visual Basic." Webster's New World&Trade; Computer Dictionary. Hoboken: Wiley, 2003. Credo Reference. Web. 22 September 2014.
".Net." Webster's New World&Trade; Computer Dictionary. Hoboken: Wiley, 2003. Credo Reference. Web. 13 October 2014.
"Basic." Encyclopedia of Computer Science. Eds. Edwin D. Reilly, Anthony Ralston, and David Hemmendinger. Hoboken: Wiley, 2003. Credo Reference. Web. 13 October 2014.
Marconi, Andrea. "History of BASIC." Http://www.q7basic.org/. Web. 21 Oct. 2014. .
Hughes, Stephen, and John Daintith. "Visual Basic." Encyclopedia.com. HighBeam Research, 1 Jan. 2002. Web. 21 Oct. 2014.
"Visual Basic Language Features." Visual Basic Language Features. Web. 21 Oct. 2014. .
"Vb.net:anevaluation - Kirablowing." Vb.net:anevaluation - Kirablowing. Web. 21 Oct. 2014. .
Share with your friends: |