Visual Basic.net L3
Visual Basic.net L3
Net
(Introduction to VB.NET)
For
BCA Students
ANANT KUMAR
Faculty Member
Department of Computer Science
J. D. Women’s College, Patna
Introduction to VB.NET
Identifiers
An identifier is a name used to identify a class, variable, function, or any other user-
defined item. The basic rules for naming classes in VB.Net are as follows –
A name must begin with a letter that could be followed by a sequence of letters,
digits (0 - 9) or underscore. The first character in an identifier cannot be a digit.
VB.Net Keywords
A keyword is a reserved word with special meanings in the compiler, whose meaning
cannot be changed. Therefore, these keywords cannot be used as an identifier
in VB.NET programming such as class name, variable, function, module, etc.
VB.NET Comments
A comment is used to explain the various steps that we have taken in our programming.
The compiler ignores these comment statements because the compiler is not executed or
processed in VB.NET. Therefore, it does not take any place in your compilation code.
Example
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 = Today
c = "U"c
s = "Me"
1 CBool(expression)
Converts the expression to Boolean data type.
2 CByte(expression)
Converts the expression to Byte data type.
3 CChar(expression)
Converts the expression to Char data type.
4 CDate(expression)
Converts the expression to Date data type
5 CDbl(expression)
Converts the expression to Double data type.
6 CDec(expression)
Converts the expression to Decimal data type.
7 CInt(expression)
Converts the expression to Integer data type.
8 CLng(expression)
Converts the expression to Long data type.
9 CObj(expression)
Converts the expression to Object type.
10 CSByte(expression)
Converts the expression to SByte data type.
11 CShort(expression)
Converts the expression to Short data type.
12 CSng(expression)
Converts the expression to Single data type.
13 CStr(expression)
Converts the expression to String data type.
14 CUInt(expression)
Converts the expression to UInt data type.
15 CULng(expression)
Converts the expression to ULng data type.
16 CUShort(expression)
Converts the expression to UShort data type.
Option Strict On
Module DB_Conversion
Sub Main()
'defining the Data type conversion
Dim dblData As Double
dblData = 5.78
Dim A, B As Char
Dim bool As Boolean = True
Dim x, Z, B_int As Integer
A = "A"
B = "B"
B_int = AscW(B)
x=1
Z = AscW(A)
Z=Z+x
Console.WriteLine("String to integer {0}", Z)
Console.WriteLine("Boolean value is : {0}", CStr(bool))
Dim num, intData As Integer
num = CInt(dblData)
intData = CType(dblData, Integer)
Console.WriteLine(" Explicit conversion of Data type " & Str(intData))
Console.WriteLine(" Value of Double is: {0}", dblData)
Console.WriteLine("Double to Integer: {0}", num)
Console.ReadKey()
End Sub
End Module
Output:
Ascii value of B is 66
String to integer 66
Boolean value is: True
Explicit conversion of Data type 6
Value of Double is: 5.78
Double to Integer: 6
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 Variable_Name as DataType
VariableName: It defines the name of the variable that you assign to store values.
DataType: It represents the name of the data type that you assign to a variable.
Each variable in the variable list has the following syntax and parts –
variable_name = value;
Example
Dim pi As Double
pi = 3.14159
Example
Module variablesNdataypes
Sub Main()
Dim a As Short
Dim b As Integer
Dim c As Double
a = 10
b = 20
c=a+b
Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c)
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following result −
a = 10, b = 20, c = 30
Accepting Values from User
The Console class in the System namespace provides a function ReadLine() for
accepting input from the user and store it into a variable.
Module variablesNdataypes
Sub Main()
Dim message As String
Console.Write("Enter message: ")
message = Console.ReadLine()
Console.WriteLine()
Console.WriteLine("Your Message: {0}", message)
Console.ReadLine()
End Sub
End Module
Variables are lvalues and so may appear on the left-hand side of an assignment.
Numeric literals are rvalues and so may not be assigned and cannot appear on the left-
hand side. Valid statement is −
Dim g As Integer = 20
But following is not a valid statement and would generate compile-time error –
20 = g