Você está na página 1de 8

Chapter 4: Language Concepts

Syntax and Semantic


Syntax refers to the spelling and grammar of a programming language. Computers
are inflexible machines that understand what you type only if you type it in the
exact form that the computer expects. The expected form is called the syntax.
Semantics is about the meaning of the sentence. It answers the questions: is this
sentence valid? If so, what does the sentence mean?
Examples
Dim x As Integer ''the syntax is correct
X Dim As Integer ''the syntax is incorrect

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.

Components of VB.NET Program


A VB.Net program basically consists of the following parts:

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.

The basic value types provided in VB.Net can be categorized as:

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.

Variable Declaration in VB.Net

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:

Dim [variablename] As DataType

Examples

Dim StudentID As Integer


Dim StudentName As String
Dim Salary As Double
Dim count1, count2 As Integer
Dim status As Boolean
Dim lastTime, nextTime As Date

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

Integer 4 bytes -2,147,483,648 through 2,147,483,647 (signed)


-9,223,372,036,854,775,808 through
Long 8 bytes
9,223,372,036,854,775,807(signed)

4 bytes on 32-bit
platform
Object Any type can be stored in a variable of type Object
8 bytes on 64-bit
platform

SByte 1 byte -128 through 127 (signed)


Short 2 bytes -32,768 through 32,767 (signed)

-3.4028235E+38 through -1.401298E-45 for negative values;


Single 4 bytes
1.401298E-45 through 3.4028235E+38 for positive values

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)

The following example demonstrates use of some of the types:


Module DataTypes
Sub Main()
Dim b As Byte
Dim n As Integer
Dim si As Single
Dim d As Double
Dim da As Date
Dim c As Char
Dim s As String
Dim bl As Boolean
b = 1
n = 1234567
si = 0.12345678901234566
d = 0.12345678901234566
da = 0999-02-02
c = "U"c
s = "Me"
If ScriptEngine = "VB" Then
bl = True
Else
bl = False
End If

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()

Console.WriteLine("Enter Number 2:")


y = Console.ReadLine()

Console.WriteLine("Select the Operation==>")


Console.WriteLine("1.Addition")
Console.WriteLine("2.Substraction")
Console.WriteLine() 'it just prints new line
choice = Console.ReadLine()

If choice = 1 Then
z=x+y
ElseIf choice = 2 Then
z=x-y
End If

Console.WriteLine("Thanks, The result is: {0}", z)

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

Creating Arrays in VB.Net


To declare an array in VB.Net, you use the Dim statement. For example,

Dim intData(30) As Integer ' an array of 31 elements


Dim strData(20) As String ' an array of 21 strings

You can also initialize the array elements while declaring the array. For example,

Dim intData() As Integer = {12, 16, 20, 24, 28, 32}


Dim names() As String = {"Karthik", "Sandhya", _
"Shivangi", "Ashwitha", "Somnath"}
Dim miscData() As Object = {"Hello World", 12d, 16ui, "A"c}

Example
Imports System

Module Module1

Sub Main()
'creating an array
Dim x(3) As Byte

'assigning value to the array elements


x(0) = 122
x(1) = 33
x(2) = 250
x(3) = 55

'accessing the elements


Console.WriteLine(x(0))
Console.WriteLine(x(2))

End Sub

End Module

Você também pode gostar