Unit-2 .Net Final
Unit-2 .Net Final
Integer
It occupies 4 bytes
The range of it -2,147,483,648 to 2,147,483,647.
Prefix we can use ‘int’ and store only whole number
Example: Dim int RollNo As Short
intRollno=5
Long
It occupies 8 bytes.
The range of it -9,223,372,036,854,775,808 to 9, 223, 372, 036, 854, 775,807.
Prefix we can use 'Ing' and store only whole number.
Example: Dim IngBankIncome As Long
IngBankIncome = 50000000
Single
It occupies 4 bytes.
The range of it -3.4028235E+38 to -1.401298E-45 for negative value-
1.401298E-45 to 3.4028235E+38 for positive values.
Prefix we can use 'int' and store whole
Example: Dim sngSal As Single
sngSal 5000.5555
Double
It occupies 8 bytes.
The range of it -1.79769313486231570E+308 to -4.94065645841246544E-
324 for negative values; 4.94065645841246544E-324 to
1.79769313486231570E+308 for positive values.
Date
It occupies 8 bytes.
Minimum value is 0:00:00 on January 1, 0001 and Maximum value.
11:59:59 PM on December 31, 9999.
Prefix we can use 'dt' and we need to write (# #)
The format of Date variable is #mm/dd/yyyy#
Example: Dim dtDob As Date
dtDob= #12/21/1982#
Object
It occupies 4 bytes.
It can store any type of data. Prefix we can use ' obj'.
stack heap
num1=10
Address=1002 obj1=10
Address=2004
obj1(ref)
Address=2004
num2=10
figure of Unboxing
stack heap
num1=10
obj1=10
obj1(ref)
figure of Boxing
2.2.2 Enumeration
It is used to declare set of constants. You can declare an Enumeration and defines the values
of its member.
Enum is a keyword known as Enumeration. Enumeration is a user-defined data type
used to define a related set of constants as a list using the keyword enum statement. It can be
used with module, structure, class, and procedure. For example, month names can be
grouped using Enumeration.
Syntax
[<Attribute List>][access modifier]
Enum enumeration_name [As Data Type]
Member1 [=initial expression]
……
MemberN [=initial expression]
End Enum
Here Access modifier is optional. It specifies that which section can access this
enumeration.
It can be Public or Private or Protected or Friend. You can specify Protected Friend to
allow access from code within the enumeration's class, a derived class or the same
assembly.
If you do not use initial expression then member gets initial value 0, member 2 gets
initial value 1 and so on. Even you can also partially assign the initial value to the
members of enumeration.
CTYPE Function
It uses to convert one type to another type.
Instead of remember all conversion functions, remember only CTYPE function.
Example:
Dim no1 As Integer
Dim no2 As Double
no2 = 66.77
no1 = CType (no2, Integer)
MsgBox (nol)
Execution is faster because there is no call to a procedure to accomplish the
conversion.
Example:2
PublicClassForm1
value1 = TextBox1.Text
value2 = TextBox2.Text
'Integer to double
'iint = TextBox4.Text
'dbl = iint
'TextBox5.Text = dbl
EndSub
EndClass
Output:
Date Convertion
PrivateSub btnisdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnisdate.Click
Dim dt As Date
Dim strtemp As String
Dim boole AsBoolean
dt = Now.ToShortDateString
boole = IsDate(dt)
MsgBox(boole) 'true
1. Option Explicit:
It ensures that variables are declared before referring in code. In other words
Explicit forces to declare variable before it is used in the program. The general syntax of
option explicit statement is as follow:
Syntax:
Option Explicit (On / Off}
There are two modes of Option Explicit: On and Off mode. If Option Explicit is
on, variable must be declared before used in the program. Otherwise, it will issue a
compile-time error. By default the Option Explicit is On.
Example:
Option Explicit Off
PublicClassForm1
PrivateSub btnExplicit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnExplicit.Click
'Dim ans As Integer
ans = 10
EndSub
EndClass
Option Explicit On
PublicClassForm1
PrivateSub btnExplicit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnExplicit.Click
'Dim ans As Integer
ans = 10//give an error
EndSub
EndClass
(ii)Option Strict
It ensures that conversion errors are avoided in the code. Option Strict On allows
widening.conversion statements, but a compiler error occurs if a narrowing conversion is
attempt that will cause data loss. The general syntax of option strict statement is as follow:
Syntax:
Option Strict (On / Off" }
It can be set at the top line of code of class files, or on a project level by modifying the
project's properties. By default Option Strict is off.
Example:
Option Strict On
PublicClassForm1
PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
Dim n1 AsInteger
Dim n2 AsDouble
n2 = 9992.36
n1 = n2
MsgBox(n1)
EndSub
EndClass
Syntax
Option Compare (Binary | Text }
Example:
Option Compare Text
PublicClassForm1
PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
Dim a As String = "Hi"
Dim b As String = "good"
‘Dim b As String = "Hi"
If a = b Then
MsgBox(True)
Else
MsgBox(False)
EndIf
EndSub
EndClass
Compare
It's use for comparing 2 strings.
Example:
Int Compare = str. Compare("abc", "ABC", True)
MsgBox(Int Compare, MsgBoxStyle. Information, "Comparison")
Int Compare = str. Compare("ABC", "abc")
MsgBox(Int Compare, MsgBoxStyle. Information, "Comparison")
Int Compare = str.Compare("ABC", "ABC")
MsgBox(Int Compare, MsgBoxStyle. Information, "Comparison")
Output of 1st is 0,2nd -1,3rd is 0.
Concat:
Combined 2 strings. Places them one after the other and forms a new string.
We can use &,+ operators also instead of this.
Copy:
Copy string, From source to destination.
Instead of this we can use directly operator like a=b means copy from b to a
also.
Equals
Determines whether two String objects have the same value.
Trim:
Removes starting and ending spaces from the string and ending of the string default.
We can also pass parameter and remove particular character from starting and ending
of the string.
Example:
str = "### Ajit is good boy. ####"
MsgBox(str.Length)
Example:2
PublicClassForm1
EndSub
EndSub
'MsgBox(String.Equals("ABC", "ABC"))
EndSub
EndSub
EndClass
Output:
Example:
result = test.EndsWith("e")
TextBox1.Text = (CStr(result))
End Sub
End Class
output:
Example:
result = test.StartWith("a")
MessageBox.Show = (CStr(result))
End Sub
End Class
output:
Indexof
Reports the index of the first occurrence of a String.roda.
Example:
result = CStr(staff.IndexOf("s"))
TextBox1.Text = result
End Sub
End Class
output
str = UCase(str)
MsgBox(str, MsgBoxStyle.Information, "To Lower")
Insert
Inserts a specified instance of String at a specified index position in this instance.
Two arguments it (Start Index, Value). The startIndex means the index position of
the insertion and the Value means The String to insert.
Example:
str = "KABHI ALVIDAA NA KEHNA"
str = str.Insert(5, "abc")
MsgBox(str)
Output: KABHIabc ALVIDAA NA KEHNA
PadLeft, PadRight
Both are used for alignment purpose see the below example.
Example:
str = "I have $ 5000"
str = str.PadLeft(25, "0")
MsgBox(str)
Output: 000000000000I have $ 5000
Example:
str = "I have $ 5000"
str = str.PadRight(25, "0")
MsgBox(str)
Remove
Deletes a specified number of characters from this instance beginning at
specified position.
Two arguments of it (StartIndex,Count).
StartIndex means the position in this instance to begin deleting characters and count
means the number of characters to delete.
Example:
Public Class Form1
Replace
Replaces all occurrences of a specified character or String.
There are 2 types of arguments of it (Char, Char) and (String,String). It means
overloaded function
Example:
str="KABHI ALVIDAA NA KEHNA"
TextBox1.Text = str.Replace("KABHI", "BHABHI")
Output: BHABHI ALVIDAA NA KEHNA
Substring(IMP)
Retrieves a substring from this instance.
There are 2 types of arguments it. (Start Index) and (Start Index,Count).
StartIndex means from where to start and Count means up to how many
character.
Example:
Public Class substring
Private Sub btnresult_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles result.Click
Dim str As String
str = "hello good
ood morning every one"
str = str.Substring(7)
MsgBox(str)
Output:
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim str As String
str = "hello good morning every one"
str = str.Substring(7, 13)
MsgBox(str)
End Sub
End Class
Output:
Str.substring(1,1) will give us single digit every time from starting to ending of the
string.
Date-Time Functions
Dim dt As Date
REM: Simple store date
dt = #12/21/1982# 'MM/DD/YYYY'
Get the current date or time
Return a date
txtDate.Text = DateSerial(2009, 2, 1) 'Returns 2/1/2009
txtDate.Text = DateValue("10/26/1995") 'Returns 10/26/1995
Return Time
txtDate.Text = TimeSerial(10, 5, 45) 'Returns 10/26/1995
txtDate.Text = TimeValue("4:35:17 PM") ‘Returns 10/26/199
txtDate.Text = TimeValue(#4:35:17 PM#) 'Returns 4:35:17 PM
Format Function
Example:
Imports System.Char
PublicClassForm1
MsgBox(ToLower("D"))
EndSub
Module
It is a file to store group of functions in a single unit. User can write several procedures in the module and
that can be used on different forms and classes in the application.
Event-handling procedures are Sub procedures that execute in response to an event triggered by user
action. Like button1_Click
Modular Coding
Console.WriteLine(res)
Console.ReadLine()
End Sub
End Module
Syntax:
Logic...
End Sub
PublicClassprocedure
'procedure without arguments
Sub hii()
MsgBox("goood morning")
End Sub
We can call sub procedure using Call keyword or without Call keyword.
When we declare procedure it will automatically appear in the context menu as given below.
End Sub
End Class
Function :
A Function procedure is similar to a sub procedure, but it returns a value to the calling program.
Function procedure may or may not have arguments.
Here statements enclosed by the Function and End Function.
By Val is by default argument type.
As clause is written after the parameter list is set.
Because function can return the value we can function using:
Somvariable=functioname(arguments)
Function runs the logical and returns the value in that variable.
By default functions are public.
Syntax:
Logic…
[Return value]
End Function
Console.WriteLine(res)
Console.ReadLine()
End Sub
End Module
End Sub
Return type
Function Name
Argument
Syntax:
Sub subProcName (para1 as< type>, optional para2 as< type> = value)
End Sub
Function funcName (para1 as< type>, optional para2 as< type> = value) as
< type>
End Function
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnans.Click
add(5)
End Sub
End Class
Calling Syntax:
add(5)
Or
Call add(5,2)
Syntax:
Example:
Private Sub btnArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnArray.Click
lstarr.Items.Clear()
Dim num(4) As Integer
num(0) = 10
num(1) = 20
num(2) = 30
num(3) = 40
For i = 0 To 3
lstarr.Items.Add(num(i))
Next
End Sub
There is another way to create an array. A user can initialize array elements at the time of declaration. The
general syntax to initialize an array at the time of declaration is as follow:
• Multidimensional Array: User can also create multidimensional array. The general syntax
of a multidimensional array declaration is as follow:
Syntax
Example
After declaring an array, user can assigned a value to an array element. The general for
assigned a value to an array element is as follow:
Syntax
Num (0) =5
Mat (0,0) =5
Dim i, j As Integer
ar(0, 0) = 1
ar(0, 1) = 2
ar(0, 2) = 3
ar(1, 0) = 4
ar(1, 1) = 5
ar(1, 2) = 6
MsgBox(ar.Length)
MsgBox(ar.Rank)
MsgBox(ar.GetLowerBound(0))
MsgBox(ar.GetLowerBound(1))
For i = 0 To ar.GetUpperBound(0)
For j = 0 To ar.GetUpperBound(1)
txtans.Text &= Space(5) & ar(i, j)
Next
txtans.Text &= vbNewLine
Next
End Sub
Syntax
Example
When you use ReDim Statement, the existing contents are erased. You can use Preserve
keyword to ensure that the existing contents of an array are not lost.
Example
If you use Preserve keyword while resizing an array, only the last dimension of the array can
be resized.
Example
Private Sub btndeclare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btndeclare.Click
Dim Rollno(2) As Integer
Rollno(0) = 1
Rollno(1) = 2
Rollno(2) = 3
Dim i As Integer
ReDim Rollno(10)
It is used to release array variables and deallocate the memory used for their elements.
Syntax
Erase Num
Example
Dim Num (3) As Integer
Num (0) = 1
Num(1) = 2
Num (2) = 3
Num (3) = 4
Next
Output
Functions Description
GetUpperBound It gets the upper bound of the specified dimension in the Array.
GetLowerBound It gets the lower bound of the specified dimension in the Array.
GetLength It gets the number of elements in the specified dimension of the Array.
GetType It gets the data type of the Array.
First It gets the first element of an Array.
Last It gets the last element of an Array.
Max It gets the maximum element of an Array.
Min It gets the minimum element of an Array.
IndexOf It gets the index of the specified element of Array.
Example:
Dim Num () As Integer = {1, 2, 3}
Dim Res As Integer
Res = Num. Get Upper Bound (0)
Message Box.Show (" GetUpper Bound =" + Res.ToString)
Res=Num.GetLength(0)
Message Box.Show (" GetLength = " + Res. ToString)
Res-Num. First
Message Box.Show(" First Element=" + Res. ToString)
Res-Num. Last
Message Box.Show ("Last Element =" + Res.ToString)
Res-Num. Max
Message Box.Show ("Max-" + Res. ToString)
Res-Num. Min
Message Box.Show ("Min =" + Res.ToString)
• Array of Array
It is possible to create more complicated structures for storing data, such as array of arrays.
If an array is declared as Object, it is possible to assign other types to its elements, including arrays.
Suppose there are two arrays, one with integers and another with strings, then an Object type array
can be declared with two elements and populate it with the two arrays i.e.of integer and string.
Example:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnarr.Click
Dim sArr(9) As String
Dim iArr(4) As Integer
Dim oArr(1) As Object
Dim i As Integer
For i = 0 To 4
' assign value to iArr array
iArr(i) = i
Next ' assign value to sArr array
For i = 0 To 9
sArr(i) = " Str " & i.ToString(" 0000 ")
Next
oArr(0) = iArr
oArr(1) = sArr
MessageBox.Show(oArr(0)(3))
''fourth element of iArr in oArr
MessageBox.Show(oArr(1)(6))
' seventh element of sArr in oArr
End Sub
Output: The following output is displayed in the message box.
Str 0006
In the above example, oArr was declared as a one-dimensional array, but because each of its elements is an
array, you must use two indices to access it. To access the fourth element of iArr in oArr, the use indices 0
and 3. Likewise, the seventh element of the sArr in oArr is oArr (1) (6).
name(0) = "Elisha"
name(1) = "Shivansh"
name(2) = "Devang"
name(3) = "Joshina"
name(4) = "ABC"
name(5) = "XYZ"
name(6) = "PQR"
'set value
'name.SetValue("Aelisha", 0)
'name.SetValue("Shiv", 1)
Dim i As Integer
For i = 0 To name.GetUpperBound(0)
lstarr.Items.Add(name.GetValue(i))
'lstarr .Items .Add (name.SetValue ("a",i)
Next
'sorting
lstarr.Items.AddRange(name)
System.Array.Sort(name)
ListBox1.Items.AddRange(name)
For i = 0 To name.GetUpperBound(0)
nameLen(i) = Len(name(i))
ListBox1.Items.Add(name(i) & "" & Len(name(i)))
Next
'Reverse
lstarr.Items.AddRange(name)
Array.Reverse(name)
ListBox1.Items.AddRange(name)
End Sub
End Class
Collections
A collection is a one-dimensional storage area which holds value of mixed data types.
Collections are increased in size simply by adding items to it. A collection is declared with a Dim statement
that creates a new Collection object.
Syntax:
Syntax:
Cname.Add(Value,Key)
Syntax:
Cname.Item(Index) or Cname.Item(Key)
Class Description
ArrayList Size is dynamically increased as required.
Hashtable Represents a collection of key/value pairs.
SortedList Represents a collection of key/value pairs that are sorted by the keys and are accessible
by key and by index.
A collection is declared with a Dim statement that creates a new Collection object.
End Sub
Sub AddArrayRange()
' Dim ar As New ArrayList
Dim str(4) As String
str(0) = "a"
str(1) = "b"
str(2) = "c"
str(3) = "d"
str(4) = "e"
ar.AddRange(str)
End Sub
Private Sub Btnarraylistcount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Btnarraylistcount.Click
Dim ar As New ArrayList
ar.Add("Surat")
ar.Add("Vadodra")
End Sub
Next
MsgBox(ar.Count())
ar.Clear()
MsgBox(ar.Count())
End Sub
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnadd.Click
Dim ar As New ArrayList
Dim str As String
ar.Add("Surat")
ar.Add("Vadodra")
ar.Add("Bharuch")
ar.Add("kim")
Dim ar2 As New ArrayList
ar2 = ar.GetRange(0, 3)
For Each str In ar
ListBox1.Items.Add(str)
Next
For Each str In ar2
ListBox2.Items.Add(str)
Next
End Sub
Next
End Sub
strarr(0) = "a"
strarr(1) = "b"
strarr(2) = "c"
strarr(3) = "d"
strarr(4) = "e"
ar.InsertRange(2, strarr)
For Each str In ar
ListBox2.Items.Add(str)
Next
End Sub
Next
ar.Reverse()
For Each str In ar
ListBox2.Items.Add(str)
Next
End Sub
Private Sub Btnremove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Btnremove.Click
Dim ar As New ArrayList
Dim str As String
ar.Add("A")
ar.Add("B")
ar.Add("C")
ar.Add("D")
Next
ar.Remove("D")
Next
End Sub
Private Sub btnsort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnsort.Click
Dim ar As New ArrayList
Dim str As String
ar.Add("Aelisha")
ar.Add("Joshina")
ar.Add("Nency")
ar.Add("pooja")
ar.Add("Komal")
ar.Add("Himani")
Next
ar.Sort()
Next
End Sub
EndClass
Output:
HashTable:
Hashtable is storing data in key-value pair,where key field will remain unique.
Elements can be searched using key value.
HashTable allows you to access the items by a key.Each item has a value and a key.
Example:
PublicClasshash_table
Sorted List:
Example:
PublicClasssorted_list
'Dim s1 As New sortedlist
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim s1 AsNewSortedList
Dim i AsInteger
s1.Add(1, "ab")
s1.Add(21, "cdd")
s1.Add(12, "nn")
s1.Add(5, "mm")
For i = 0 To s1.Count - 1
ListBox1.Items.Add(s1.GetKey(i))
Next
For i = 0 To s1.Count - 1
ListBox2.Items.Add(s1.GetByIndex(i))
Next
EndSub
EndClass
Output:
Sr.
Key Arrays Collection
No.
Arrays are fixed in size i.e once the array
The collection is dynamic in size i.e based
1 Size with the specific size is declared then we
on requirement size could be get altered
can't alter its size afterward. even after its declaration.
Collections, on the other hand, consume
Memory Arrays due to fast execution consumes
2 less memory but also have low
Consumption more memory and has better performance.
performance as compared to Arrays.
Arrays can hold the only the same type of
data i.e only homogeneous (data structures Collection, on the other hand, can hold
3 Data type are those in which data of same type can be both homogeneous and heterogeneous
stored) data types elements are allowed in elements.
case of arrays.
4 Primitives Arrays can hold both object and primitive On the other hand, collection can hold
condition.
> The condition is evaluated runtime and depending on the result of condition
> Control Structure also known as Branching statements or Test Structure or Decision Structure.
1.If…Then..Else
2.Select case
If...Then...Else
There are cases when we would like to execute some logic when a condition TRUE, and some other logic
when the condition is FALSE.
If the condition is TRUE then the control goes to between IF and Else block, that is the program will execute
the code between IF and ELSE statements.
If the condition is FALSE then the control goes to between ELSE and END IF block, that is the program will
execute the code between ELSE and END IF statements.
We can write only If block. The ElseIf and Else clauses are both optional.
Syntax:
If condition Then
Logic…
Created By: Aelisha Joshina
ElseIf condition-n Then Page 41
Example: Single If block
Private Sub btnAns_Click(ByVal sender As System.
End If
End Sub
End Class
Example:If…else block
Public class
Private Sub if_else_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
If TextBox1.Text Mod 2 = 0 Then
Label1.Text = "No is even"
Else
Label1.Text = "No is odd"
End If
End Sub
End Class
Example:1
Private Sub btnresult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnresult.Click
Example:2
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim pct As Double
'CDbl (Function) Converts any expression to a Double.
' This function accepts any expression convertible to a Double, including strings. A runtime error
is generated if expression is Null.
EndSub
EndClass
Output:
Example:
Private Sub btnifthenelseif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnifthenelseif.Click
If txtname.Text = "" Then
MsgBox("Enter your Name")
txtname.Focus()
ElseIf txtadd.Text = "" Then
MsgBox("Enter your Address")
txtadd.Focus()
ElseIf txtage.Text = "" Then
MsgBox("Enter your Age")
txtage.Focus()
Else
MsgBox(txtname.Text & txtadd.Text & txtage.Text)
End If
End Sub
Select....Case
>When you are comparing the same expression with several different values, we can use the Select...Case
statements as an alternative to the If...Then...Else statements.
>It executes one of several groups of statements depending on the value of an expression.
> If. Then..Else block executes different condition or expression in each statement, the Select statement
evaluates a single expression and compares it for every comparison.
>It's possible in If..Then.. Else block to satisfy multiple If and ElseIf conditions then multiple logic will run,
but If multiple Case statements are True, only the statements belonging to the first true Case statement
are executed.
>If any Case statement is satisfied logic will run that only and then control will come out of Select block.
This one will not happen with If...Then..Else block.
.> We can add any number of Case statements, and we can include or remove a Case Else statement
> Case Else is an optional statement. If used, the Case Else must be the last Case clause. Otherwise it will
execute all the time.
>We can also use Is keyword. The Is keyword is used with any comparison
Syntax:
Case expressionist-n
Logic...
Case Else
Logic….
Output:
Loop Statements
Looping allows user to run one or more lines of code repetitively.
You can repeat the statements in a loop until a condition is True.
Once a condition is False execution of those statements stops.
VB .NET supports
1. While...End While
2. Do ....Loop
3. For..Next
4. for-each loops.
While...... Loop
While Loop runs set of statements as long as the condition specified with While loop is True.
Syntax:
While condition
[Statements]
End While
Example
Private Sub btnwhileloop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnwhileloop.Click
Dim num1 As Integer = 1
While num1 < 10
MessageBox.Show("The value of num1 is:" & num1)
num1 = num1 + 1
End While
End Sub
Do..... Loop
Do Loop allows you to test a condition at either the beginning or at the end of a loop.
You can also specify whether to repeat the loop while the condition remains True or False.
There are two variations of Do loop: entry-control loop and exit-control loop.
Syntax
Do {While/Until} Condition
Logic....
Loop
Example 1:
Loop
'While repeat the loop until condition is false
End Sub
Example 2:
Private Sub btndoloop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btndoloop.Click
Dim num1 As Integer = 1
Do Until num1 = 5
MessageBox.Show("The value of num1 is:" & num1)
num1 = num1 + 1
Loop
'Until:-repeat the loop untill condition is true
End Sub
Here counter variable is initialized when the loop starts and runs up to the end values.
We can set increment and decrement by step keyword.
Step keyword is optionally to write.
By default loop will increment by 1.
Example
Private Sub btnfornext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnfornext.Click
Dim i As Integer
For i = 0 To 10
MessageBox.Show("The value of i is:" & i)
Next
End Sub
For each Loop
We will discuss in topic 2.5 Array and Collection
MsgBox(),, MessageBox.Show
MessageBox.Show() and InputBox()
MsgBox()
Displays a message in a dialog box.
It interrupts the user that means it immediately blocks further interactions.
It requires only one function call.
W specify Button , waits for the user to click a button, and returns an Integer
indicating which button the user clicked.
Syntax
Ans=MsgBox (prompt, [ bbuttons+Icon ] ,[ title])
Example: 1- MsgBox("MsgBox
"MsgBox Prompt"
Prompt", MsgBoxStyle.YesNo, "MsgBox Title")
Title"
Example: 2-
Private Sub Button1_Click(ByVal
ByVal sender As System.Object, ByVal e As System.EventArgs)
System.
Handles Button1.Click
'MsgBox("Hi Welcome msgbox")
Dim ans As Integer
ans = MsgBox("can
"can you give me answer?"
answer?", MsgBoxStyle.Information
.Information +
MsgBoxStyle.YesNoCancel)
If ans = 6 Then
MsgBox("thanks")
ElseIf ans = 7 Then
MsgBox("why No..?"))
ElseIf ans = 2 Then
MsgBox("why
"why cancel"
cancel")
End If
End Sub
Constant Description
OKOnly Display OK button only.
OKCancel Display OK and Cancel buttons.
AbortRetryIgnore Display Abort, Retry, and Ignore buttons.
YesNoCancel Display Yes, No, and Cancel buttons.
YesNo Display Yes and No buttons.
RetryCancel Display Retry and Cancel buttons.
Critical Display Critical Message icon.
Question Display Warning Query icon.
Exclamation Display Warning Message icon.
Information Display Information Message icon.
DefaultButton1 First button is default.
DefaultButton2 Second button is default.
DefaultButton3 Third button is default.
DefaultButton4 Fourth button is default.
MessageBox.Show
It's an advance version of MsgBox function.
MessageBox is the class and Show is the method of it.
Show method has more arguments then MsgBox function. It contains separate arguments for
icon and button.
It also provides alignment option, helpbutton and default button options.
Syntax:
MessageBox.Show(Text,caption,button,icon,defaultbutton, options,help)
Output:
InputBox
InputBox function is to accept data from th
the user.
An InputBox function will display a prompt box where the user can enter a value or a
message in the
form of text.
Instead of taking Textbox on the form we can get the user value using InputBox.
The Inputbox function with return a va
value.
lue. We need to declare the datatype for values to be
accepted
in the Inputbox
Syntax:
Ans=InputBox(Prompt, Title, default_text, xx-position, y-position)
The prompt argument will be displayed as message when a question is asked.
aske
Thetitle argument is the title of the Input Box.
The default_text argument appearsthe Input field where users can use it as his intended
input or he may change
to the message he wish to key in and xx-position and y-position
position are the position or the
coordinate of the
Inputbox.
OutPut:
Example: 2:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
Dim result As String
result = InputBox("Enter name", "Info", "Hrere")
MsgBox(result, , "Result")
End Sub