VB.Net Language Fundamentals
If you are new to VB.Net School
This is the 3rd lesson in VB.Net School series. The VB.Net School is a kind of interactive learning platform where those who want to learn .Net with VB can find help and support with an issue a week describing some areas of the VB.Net Programming Language with the Microsoft .Net Platform. This is not the same traditional passive tutorial where author only writes and reader only reads. There will be exercise problems at the end of each issue in which the reader is supposed to solve after reading the issue. The solutions to these problems will be provided at the end of the next issue. There is also a dedicated message board coupled with the school where you can ask any questions regarding the contents of the school. The author will respond to your questions within 2/3 days. You can send your suggestions, feedback or ideas on how these lessons can be improved, to either farazrasheed@acm.org or info@programmersheaven.com
For previous lessons click here
Lesson Plan
This lesson is about learning the language fundamentals of VB.Net. We will explore the data types in VB.Net, using variables, different kinds of operators in VB.Net, flow control statements like If...Then...Else, looping structures and how to use arrays.
Basic Data Types and their mapping to the CTS (Common Type System)
There are two kinds of data types in VB.Net
- Value type (implicit data types, Structure and Enumeration)
- Reference Type (objects, delegates)
As we saw in the first lesson, implicit data types in .net compliant languages are mapped to types in Common Type System (CTS) and CLS (Common Language Specification). Hence, each implicit data type in VB.Net has its corresponding .Net type. The implicit data types in VB.Net are:
| VB.Net type | Corresponding .Net type | Size in bytes | Description |
|---|---|---|---|
| Boolean | Boolean | 1 | Contains either True or False |
| Char | Char | 2 | Contains any single Unicode character enclosed in double quotation marks followed by a c, for example "x"c |
| Integral types | |||
| Byte | Byte | 1 | May contain integers from 0-255 |
| Short | Int16 | 2 | Ranges from -32,768 to 32,767 |
| Integer(default) | Int32 | 4 | Ranges from -2,147,483,648 to 2,147,483,647 |
| Long | Int64 | 8 | Ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. |
| Floating point types | |||
| Single | Single | 4 | Ranges from ±1.5 × 10-45 to ±3.4 × 1038 with 7 digits precision. Requires the suffix 'f' or 'F' |
| Double(default) | Double | 8 | Ranges from ±5.0 × 10-324 to ±1.7 × 10308 with 15-16 digits precision. |
| Decimal | Decimal | 12 | Ranges from 1.0 × 10-28 to 7.9 × 1028 with 28-29 digits precision. Requires the suffix 'm' or 'M' |
Implicit data types are represented in language using 'keywords'; so each of above is a keyword in VB.Net (Keyword are the words defined by the language and can not be used as identifiers). It is worth-noting that string is also an implicit data type in VB.Net, so String is a keyword in VB.Net. Last point about implicit data types is that they are value types and thus stored at the stack, while user defined types or referenced types are stored at heap. Stack is a data structure that store items in last in first out (LIFO) fashion. It is an area of memory supported by the processor and its size is determined at the compile time. Heap is the total memory available at run time. Reference types are allocated at heap dynamically (during the execution of program). Garbage collector searches for non-referenced data in heap during the execution of program and returns that space to Operating System.
Variables
During the execution of program, data is temporarily stored in memory. A variable is the name given to a memory location holding particular type of data. So, each variable has associated with it a data type and value. In VB.Net, a variables is declared as:
Dim <variable> as <data type>
e.g.,
Dim i As Integer
The above line will reserve an area of 4 bytes in memory to store integer type values, which will be referred in the rest of program by identifier 'i'. You can initialize the variable as you declare it (on the fly) and can also declare/initialize multiple variables of same type in a single statement, e.g.,
Dim isReady As Boolean = True
Dim percentage = 87.88, average = 43.9 As Single
Dim digit As Char = "7"cVB.Net Option Strict and Option Explicit Settings
There are two 'bad' features in VB.Net, which are inherent from earlier versions (VB5 and VB6):
- You can declare a variable without specifying its type. VB.Net, in this case, assumes the type of the variable as System.Object class
- You can convert values (or objects) to incompatible types, e.g., String to Integer.
Option Explicit Statement
Option Explicit, when turned on, do not allow to use any variable without proper declaration.
There are two methods to apply the Option Explicit Statement.
- To apply the Option Explicit settings to the complete project in Visual Studio.Net, right click the project name in the solution explorer and select Properties. It will open the Property Pages window. Now in the Common Properties tree at left, select Build, it will show the following window

From here, you can turn the Option Explicit (as well as Option Strict) on or off.
- To apply the Option Explicit settings to the current file, use the Option Explicit statement before any statement as,
Option Explicit On
When Option Explicit is on, it will cause the compile time error to write
myName = "Faraz" ' compile time error with Option Explicit On
Rather, you would have to write,
Dim myName As String = "Faraz"
School Home
|
|
Homer Thomas
From Maxville , Florida (Report as abusive) |
GREAT PIECE OF WORK Sir your lessons are in a word GREAT, Ive bought several books in the forty to fifty dollar range that doesnt do as good of a job explaining things in a way it can be understood the way you do. Thank you Very much for taking your time to help people like me. |
|
Anonymous
(Report as abusive) |
Excellent work! Faraz: I would like to compliment you, not only on the excellence of your tutoring, but also on the quality of your English. There are some minor errors that indicate English is not your first language, but this only emphasizes how far you have come in mastering a foreign langauge. If some of your generation go into politics with the same dedication and capability you show, Pakistan will have a bright future! Thank you. Ken |
| View all Rate and comment this article |
Sponsored links
.Net Application Updating
One easy to use component adds safe and reliable updating features. Download today for a free trial.
One easy to use component adds safe and reliable updating features. Download today for a free trial.
Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Villanova University Six Sigma & IT Certificate Programs
100% Online programs in Six Sigma, IS Security, CISSP Prep, Business Analysis, Proj. Mgmt. and more!
100% Online programs in Six Sigma, IS Security, CISSP Prep, Business Analysis, Proj. Mgmt. and more!
.Net Localization in three simple steps (WYSIWYG)
Localize .Net, C#/C/C++ & Delphi apps visually. HTML, HTML Help, XML & databases. Try Sisulizer now!
Localize .Net, C#/C/C++ & Delphi apps visually. HTML, HTML Help, XML & databases. Try Sisulizer now!