Académique Documents
Professionnel Documents
Culture Documents
Here are some general cases of semantic errors in languages like VB.NET:
Type mismatches There are a lot of ways to use the wrong type in C#. The
example above is one way. Here are some other possible ways:
Passing the wrong type of value to a method.
Returning the wrong type of value from a method.
Setting the wrong type of value on a property.
Setting the wrong type of value on a field.
Setting the wrong type of value on a local variable.
Using a type in a generic argument list which doesnt conform to the generic
type constraints.
Argument mismatch This means using the wrong number of arguments for
something and there are
many ways to do this as well:
Passing the wrong number of arguments to a method call.
Passing the wrong number of indexes to an array or indexer.
Using the wrong number of generic type arguments for a generic type or
method.
Using undefined names Using a namespace, type, method, field, variable, or
property name which doesnt exist.
Naming collisions Here are some ways names can collide:
Defining two types in the same namespace with the same name
Defining two methods in the same class with the same argument list and
name.
Defining a property, field, or nested type which has the same name as another
property, field, or nested type in the same owning type.
Defining two local variables in the same or descendant scopes with the same
names.
Namespace declaration
A class or module
One or more procedures
Variables
The Main procedure
Statements & Expressions
Comments
Let us look at a simple code that would print the words "Hello World":
Imports System
Module Module1
'This program will display Hello World
Sub Main()
Console.WriteLine("Hello World")
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
Hello, World!
Let us look various parts of the above program:
The first line of the program Imports System is used to include the System
namespace in the program.
The next line has a Module declaration, the module Module1. VB.Net is completely
object oriented, so every program must contain a module of a class that contains
the data and procedures that your program uses.
Classes or Modules generally would contain more than one procedure. Procedures
contain the executable code, or in other words, they define the behavior of the
class. A procedure could be any of the following:
Function
Sub
The next line( 'This program) will be ignored by the compiler and it has been put to
add additional comments in the program.
The next line defines the Main procedure, which is the entry point for all VB.Net
programs. The Main procedure states what the module or class will do when
executed.
The Main procedure specifies its behavior with the statement
Console.WriteLine("Hello World")
WriteLine is a method of the Console class defined in the System namespace. This
statement causes the message "Hello, World!" to be displayed on the screen.
The last line Console.ReadKey() is for the VS.NET Users. This will prevent the screen
from running and closing quickly when the program is launched from Visual
Studio .NET.
Variable
A variable is nothing but a name given to a storage area that our programs can
manipulate. Each variable in VB.Net has a specific type, which determines the size
and layout of the variable's memory; the range of values that can be stored within
that memory; and the set of operations that can be applied to the variable.
In another word, variable is a storage place in the memory with unique name. We
can store our data in the memory of the computer by using variables.
Variables can be of two types: Reference type which holds the address of another
variable or object and Value type which holds a value.
Type Example
Integral types SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong and Char
Floating point Single and Double
types
Decimal types Decimal
Boolean types True or False values, as assigned
Date types Date
VB.Net also allows defines other value types of variable like Enum and reference
types of variables like Class.
The Dim statement is used for variable declaration and storage allocation for one or more
variables. The Dim statement is used at module, class, structure, procedure or block level.
Syntax:
Examples
Data Types
The data type tells a VB.NET compiler what kind of value a variable can hold. Data
types refer to an extensive system used for declaring variables or functions of
different types. The type of a variable determines how much space it occupies in
storage.
Storage
Data Type Value Range
Allocation
Depends on
Boolean implementing True or False
platform
Byte 1 byte 0 through 255 (unsigned)
Char 2 bytes 0 through 65535 (unsigned)
Date 8 bytes 0:00:00 (midnight) on January 1, 0001 through 11:59:59 PM
on December 31, 9999
0 through +/-79,228,162,514,264,337,593,543,950,335 (+/-
7.9...E+28) with no decimal point; 0 through +/-
Decimal 16 bytes
7.9228162514264337593543950335 with 28 places to the right of
the decimal
-1.79769313486231570E+308 through
-4.94065645841246544E-324, for negative values
Double 8 bytes
4.94065645841246544E-324 through
1.79769313486231570E+308, for positive values
4 bytes on 32-bit
platform
Object Any type can be stored in a variable of type Object
8 bytes on 64-bit
platform
Depends on
String implementing 0 to approximately 2 billion Unicode characters
platform
UInteger 4 bytes 0 through 4,294,967,295 (unsigned)
ULong 8 bytes 0 through 18,446,744,073,709,551,615 (unsigned)
Depends on
Each member of the structure has a range determined by its
User-Defined implementing
data type and independent of the ranges of the other members
platform
UShort 2 bytes 0 through 65,535 (unsigned)
Console.ReadKey()
End Sub
End Module
Example
Module Module1
'this program takes two numbers then adds or substract and returns the result
Sub Main()
Dim x, y, z As Integer
Dim choice As Byte
Console.WriteLine("Welcome to Caawiye")
Console.WriteLine("=========================")
Console.WriteLine("Enter Number 1:")
x = Console.ReadLine()
If choice = 1 Then
z=x+y
ElseIf choice = 2 Then
z=x-y
End If
End Sub
End Module
Arrays
An array stores a fixed-size sequential collection of elements of the same type. An
array is used to store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type.
All arrays consist of contiguous memory locations. The lowest address corresponds
to the first element and the highest address to the last element
You can also initialize the array elements while declaring the array. For example,
Example
Imports System
Module Module1
Sub Main()
'creating an array
Dim x(3) As Byte
End Sub
End Module