Syca&It VB - Net Unit 1
Syca&It VB - Net Unit 1
What is VB.Net?
• Visual Basic .NET (VB.NET), is an object-oriented computer programming language that can be
viewed as an evolution of the classic Visual Basic (VB), which is implemented on the .NET
Framework.
• It is the next generation of the visual Basic language.
• It supports OOPs concepts such as abstraction, inheritance, polymorphism, and aggregation.
2) Common Language Runtime ( CLR ): The .NET Framework provides a runtime environment called
the Common Language Runtime or CLR (similar to the Java Virtual Machine or JVM in Java), which
handles the execution of code and provides useful services for the implementation of the program.
• The CLR is the execution engine for .NET applications and serves as the interface between .NET
applications and the operating system. The CLR provides many services such as:
1) Loads and executes code
2) Converts intermediate language to native machine code
3) Framework Class Library ( FCL ): It is also known as a base class library. The FCL is a collection of
over 7000 reusable classes, interfaces, and value types that enable .NET applications to :
a) read and write files,
b) access databases,
c) process XML,
d) display a graphical user interface,
e) draw graphics,
f) use Web services, etc.
• The .Net Framework class library (FCL) organized in a hierarchical tree structure and it is divided
into Namespaces. Namespaces is a logical grouping of types for the purpose of identification.
Framework class library (FCL) provides the consistent base types that are used across all .NET
enabled languages. The Classes are accessed by namespaces, which reside within Assemblies.
• Other name of FCL is BCL – Base Class Library
.NET Languages
• .Net languages are CLI computer programming languages that may also optionally use the .NET
Framework Base Class Library and which produce programs that execute within the Microsoft .NET
Framework. Microsoft provides several such languages, including C#, F#, Visual Basic .NET, and
Managed C++.
• Generally .NET languages call into two main categories, TypeSafe Languages (such as C#) and
Dynamic Languages (Such as Python). Type Safe Languages are built on the .NET Common
Language Runtime and Dynamic Languages are built on top of the .NET Dynamic Language
Runtime. The .NET Framework is unique in its ability to provide this flexibility.
• Regardless of which .NET language is used, the output of the language compiler is a representation
of the same logic in an intermediate language named Common Intermediate Language (CIL).
• As the program is being executed by the CLR, the CLI code is compiled and cached, just in time, to
the machine code appropriate for the architecture on which the program is running. This last
compilation step is usually performed by the Common Language Runtime component of the
framework “just in time” (JIT) at the moment the program is first invoked, though it can be manually
performed at an earlier stage.
.NET Assembly
• Whatever .NET language you create applications with, compilers generate an assembly, which is a
file containing .NET executable code and is composed essentially by two kinds of elements: MSIL
code and metadata.
• The .NET assembly is the standard for components developed with the Microsoft.NET. Dot NET
assemblies may or may not be executable, i.e., they might exist as the executable (.exe) file or
dynamic link library (DLL) file.
• All the .NET assemblies contain the definition of types, versioning information for the type, meta-
data, and manifest. The designers of .NET have worked a lot on the component (assembly)
resolution.
The structure of an assembly: Assemblies contain code that is executed by the Common Language
Runtime.
a) Private
b) Shared
a) Private assemblies – It is the assembly which is used by application only, normally it resides in your
application folder directory.
• An assembly can be a single file or it may consist of the multiple files. In case of multi-file, there is
one master module containing the manifest while other assemblies exist as non-manifest modules. A
module in .NET is a sub part of a multi-file .NET assembly.
Why .NET?
• Interoperability between language and execution environment.
• Uniformity in schema or formats for Data exchange using XML, XSL (Extensible Style Sheet
Language)
• Extend or use existing code that is valid.
• Programming complexity of environment is reduced
Types of projects:
The following list provides a comparison of Visual Basic 6.0 and Visual Basic .NET project types.
Visual Basic 6.0 Visual Basic .NET
File: With this menu you can create a new project, open existing one, save the current project ,exit form the
vb.net etc
Edit: The Edit menu provides access to the items you would expect: Undo, Redo, Cut, Copy, Paste, and
Delete.
View: The View menu provides quick access to the windows that make up the IDE, such as the Solution
Explorer, Properties window, Output window, Toolbox, etc.
Project: The Project menu allows you to add various extra files to your application.
Build: The Build menu becomes important when you have completed your application and want to be able
to run it without the use of the Visual Basic .NET environment.
Debug: The Debug menu allows you to start and stop running your application within the Visual Basic .NET
IDE. It also gives you access to the Visual Studio .NET debugger.
Data: The Data menu helps you use information that comes from a database. It only appears when you are
working with the visual part of your application ,not when you are writing code.
Format: The Format menu also only appears when you are working with the visual part of your application.
Items on the Format menu allow you to manipulate how the windows you create will appear to the users of
your application.
Tools : The Tools menu has commands to configure the Visual Studio .NET IDE, as well as links to other
external tools that may have been installed.
Window: The commands on this menu allow you to change the physical layout of the windows in the IDE.
Help: The Help menu provides access to the Visual Studio .NET documentation.
Code Designers
• You can use the tabs at the top center of the IDE to switch between graphical designer and code
designer
• From view menu, you can also switch between by using code (F7) and Designer (Shift+F7) items.
• From solution Explorer, from the left side you can use top two buttons.
• At the top of code designer two drop down list boxes are available. The two drop-down list boxes at
the top of the code designer; the one on the left lets you select what object's code you're working with,
Figure 8 : Intellisense
Component Trays
Following table shows storage size in memory for the data type.
Type Storage Size
String 2 bytes
Char 2 bytes
Integer 4 bytes
Long 8 bytes
Boolean 2 bytes
Byte 1 byte
Short 2 bytes
Single 4 bytes
Double 8 bytes
Decimal 16 bytes
Date 8 bytes
Object 4 bytes
Variablename : It is required. It specifies the name of variable which user wants to create.
Subscript : It is optional. Subscript is used to specify the size of array when user declares an array.
New: New keyboard enables creation of new object. If you use new when declaring the object variable, a
new instance of the object is created.
Type : The type specify the data type of the variables.
Ex: Dim a as integer, s1 as string
• In VB.NET each variable contains default value depends on its data type.
• Default value for Numeric and Byte data type is 0(zero).
• Default value for Char data type is Binary 0(zero).
• Default value for all reference types like object, string, and arrays is Nothing.
• Default value for Boolean data type is False.
• Default value for Date data type is 12:00 AM of 1,1,0001.
Constant Declaration:
Declares and defines one or more constants.
constantlist : Required. List of constants being declared in this statement. Constant [ , constant ... ]
Constantname: It is required. It specifies the name of the constant.
Initialize: it is required. The value which is assigned to the constant. Once you initialize the constant
variable with a value it can never change.
Operators
• Visual Basic comes with many built-in operators that allow us to manipulate data. An operator
performs a function on one or more operands.
• For example, we add two variables with the "+" addition operator and store the result in a third
variable with the "=" assignment operator like this: int x + int y = int z. The two variables (x ,y) are
called operands. There are different types of operators in Visual Basic and they are described below
in the order of their precedence.
• Operators may me Unary or Binary
• Unary used with a single operand for example Ans= -10
• Binary used with two operands for example Ans= 10 / 5
• Operators also categorized in following categories:
Arithmetic Operators: Arithmetic operators are used to perform arithmetic operations that involve
calculation of numeric values. The table below summarizes them:
Operator Use
+ Addition
- Subtraction
* Multiplication
/ Division
\ Integer Division
Assignment Operators: An assignment operator is used to assign a value to an operands. The table below
summarizes them:
Operator Use
= Assignment
+= Addition followed by assignment
-= Subtraction followed by assignment
*= Multiplication followed by assignment
/= Division followed by assignment
\= Integer Division followed by assignment
^= Exponentiation followed by assignment
&= Concatenation followed by assignment
Comparison Operators : A comparison operator compares operands and returns a logical value based on
whether the comparison is true or not. The table below summarizes them:
Operator Use
< Less than
<= Less than or Equal to
> Greater than
>= Greater than or Equal to
= Equal to
<> Not Equal to
Is True if two object references refer to the same object
Like Performs string pattern matching
Operator Use
And Conjunction
Or Disjunction
Not Negation
Concatenation Operators: Concatenation operators join multiple strings into a single string. There are two
concatenation operators, + and & as summarized below:
Operator Use
+ String Concatenation
& String Concatenation
Option Explicit: This statement ensures whether the compiler requires all variables to be explicitly
declared or not before it use in the program.
The Option Explicit has two modes. On and Off mode. If Option Explicit mode in ON, you have to
declare all variables before you use it in the program. If not, it will generate a compile-time error whenever
a variable that has not been declared is encountered. If the Option Explicit mode is OFF, Vb.Net
automatically create a variable whenever it sees a variable without proper declaration.
With the Option Explicit On, you can reduce the possible errors that result from misspelled variable names.
Because, in Option Explicit On mode you have to declare each variable in the program for storing data.
Option Strict: Option Strict is prevents program from automatic variable conversions, that is implicit data
type conversions.
By default Option Strict is Off . We can set both options by Tools→ Options→projects and solutions
VarName : The Varname specify the name of variable which you want to create.
Subscript : Subscript is used when you declare an array.
Type : The type specify the data type of the array variables. User can also include “To” keyword in array
declaration.
Ex : Dim n(5) As Integer.
Dim s(3) As String.
Dim n(0 to 4) As Integer.
Rank: It is optional. Use 1 for the first dimension, 2 for the second, and so on. If rank is omitted, 1 is
assumed.
Example:
Dim A(0 To 100, 0 To 3, -3 To 4)
Statement Return Value
LBound(A, 1) 1
LBound(A, 2) 0
LBound(A, 3) -3
Ubound Function : It returns a Long containing the largest available subscript for the indicated dimension
of an array.
Rank: It is optional. Use 1 for the first dimension, 2 for the second, and so on. If rank is omitted, 1 is
assumed.
Dynamic Array:
• In any C or C++ programming language user can not modify the size of the array. Once we declared
the size of array then it becomes fixed.
• In VB .NET we can increase the size of array.
• In some cases we may not know exactly the size of array at declaration time. We may need to
change the size of the array at runtime. So we can resize the array at any time by using Redim
statement.
• But with dynamic array we cannot change the dimension of the array.
Preserve: It is optional. It is used to preserve the data in an existing array when user changes the size of the
last dimension.
Example:
Next
End Sub
msgbox a(i)
Next
End Sub
String Functions:
1) Len: This function returns an integer containing the number of characters in a given string.
Syntax : Len(string)
String: Any valid string expression. If string contains Null, Null is returned.
Example:
S1=”hello”
Msgbox len(s1)
2) Mid: It returns a Variant (String) containing a specified number of characters from a string.
Examples:
Dim MyString, FirstWord, LastWord, MidWords
MyString = "Mid Function Demo" ' Create text string.
FirstWord = Mid(MyString, 1, 3) ' Returns "Mid".
LastWord = Mid(MyString, 14, 4) ' Returns "Demo".
MidWords = Mid(MyString, 5) ' Returns "Function Demo".
3) Trim , Rtrim, Ltrim: It Returns a string that contains a copy of a specified string without leading
spaces (LTrim), without trailing spaces (RTrim), or without leading or trailing spaces (Trim).
Syntax: Trim / Rtrim / Ltrim (string)
String: It is requires any valid String expression. If string equals Nothing, the function returns an empty
string.
Example:
S1= “ This is test “
Msgbox Trim(s1)
Msgbox Rtrim(s1)
Msgbox Ltrim(s1)
4) Instr: It returns a Variant (Long) specifying the position of the first occurrence of one string within
another.
Return Value
If InStr returns
Examples:
Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".
' A textual comparison starting at position 4. Returns 6.
MyPos = Instr(4, SearchString, SearchChar, 1)
' A binary comparison starting at position 1. Returns 9.
MyPos = Instr(1, SearchString, SearchChar, 0)
' Comparison is binary by default (last argument is omitted).
Syntax: LCase(string)
The required string argument is any valid string expression. If string contains Null, Null is returned. Only
uppercase letters are converted to lowercase; all lowercase letters and nonletter characters remain unchanged.
Example:
Dim UpperCase, LowerCase
Uppercase = "Hello World 1234" ' String to convert.
Msgbox Lcase(UpperCase) ' Returns "hello world 1234"
6) Ucase: It returns a Variant (String) containing the specified string, converted to uppercase.
Syntax: UCase(string)
The required string argument is any valid string expression. If string contains Null, Null is returned.
Only lowercase letters are converted to uppercase; all uppercase letters and nonletter characters remain
unchanged.
Example:
Dim LowerCase, UpperCase
LowerCase = "Hello World 1234" ' String to convert.
Msgbox UCase(LowerCase) ' Returns "HELLO WORLD 1234".
7) Asc: It returns an Integer representing the character code corresponding to the first letter in a string.
Syntax: Asc(string)
The required string argument is any valid string expression. If String is a String expression, only the first
character of the string is used for input. If String is Nothing or contains no characters, an error occurs.
8) Chr: It returns a String containing the character associated with the specified character code.
Syntax: Chr(charcode)
An Integer expression representing the code point, or character code, for the character. If CharCode is
outside the valid range, an error occur. The valid range for Chr is 0 through 255.
Examples:
Dim MyChar
MyChar = Chr(65) ' Returns A.
Syntax: Space(number)
Number : It is required Integer expression. The number of spaces you want in the string.
Example: Msgbox “ Hi” & space(5) & “ How r u?”
10) Format: This function returns a string formatted according to instructions contained in a format
String expression.
Constant Description
Binary Performs a binary comparison, based on a sort order derived from the internal binary
representations of the characters.
Text Performs a text comparison, based on a case-insensitive text sort order determined by your
application's current culture information.
If StrComp returns
Example:
Dim TestStr1 As String = "ABCD"
Dim TestStr2 As String = "abcd"
Dim TestComp As Integer
' The two strings sort equally. Returns 0.
TestComp = StrComp(TestStr1, TestStr2, CompareMethod.Text)
' TestStr1 sorts after TestStr2. Returns -1.
TestComp = StrComp(TestStr1, TestStr2, CompareMethod.Binary)
' TestStr2 sorts before TestStr1. Returns 1.
TestComp = StrComp(TestStr2, TestStr1)
12) Left: It returns a Variant (String) containing a specified number of characters from the left side of a
string.
str :it is required String expression from which the rightmost characters are returned.
Examples:
Dim AnyString, MyStr
AnyString = "Hello World" ' Define string.
MyStr = Microsoft.VisualBasic.Left(AnyString, 1)
Msgbox Mystr ' Returns "H".
13) Right: It returns a string containing a specified number of characters from the right side of a string.
Syntax: Right(str,length)
str :it is required String expression from which the rightmost characters are returned.
Length: It is required Numeric expression indicating how many characters to return. If 0, a zero-length
string ("") is returned. If greater than or equal to the number of characters in str, the entire string is
returned.
Example:
S1=”this is test”
Msgbox Micosoft.VisualBasic.Right(s1,3)
14) Replace: It returns a string in which a specified substring has been replaced with another substring a
specified number of times.
Syntax : System.String.Concat(str1,str2)
Example:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
End Sub
End Class
2) String.copy : This method creates a new instance of String with the same value as a specified String.
Syntax: System.String.Copy(str)
Example:
MsgBox(str2)
End Sub
End Class
3) String.Indexof: It returns the index of the first occurrence of the specified substring.
If the parameter String occurred as a substring in the specified String then it returns position of the first
character of the substring. If it does not occur as a substring, -1 is returned.
Example:
4) String.substring: It returns a new string that is a substring of this string. The substring begins at the
specified given index and extended up to the given length.
Syntax: Substring(startIndex,length)
Example:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
End Sub
End Class
5) String.format: VB.NET String Format method replace the argument Object into a text equivalent
System.Striing.
Examples:
The currency symbol ($) displayed depends on the global locale settings.
6) String.ToUpper: This method uses the casing rules of the current culture to convert each character in
the current instance to its uppercase equivalent. If a character does not have an uppercase equivalent, it
is included unchanged in the returned string.
The ToUpper method is often used to convert a string to uppercase so that it can be used in a case-
insensitive comparison.
Syntax: string.ToUpper()
Example:
S1= “This is Test”
Msgbox s1.Toupper()
7) String.ToLower: This method does not modify the value of the current instance. Instead, it returns a
new string in which all characters in the current instance are converted to lowercase.
Syntax: string.tolower()
Syntax: String.Remove(startIndex)
startIndex : The position to begin deleting characters.
Example:
S1=”abc---def”
Msgbox s1.remove(3)