Q....Variable...
A variable is temporary storage space for numbers, text, and objects. Variables are constantly being created and destroyed and will not hold any values after your program has ended.....Variables play an important role in computer programming because they enable programmers to write large programs. Rather than entering data directly into a program, a programmer can use variables to represent the data. ....If a programmer want to calculate the addition of two number he will use variable to store two values and third variable is used to store the sum of both the variable...
Variables are declared using the Visual Basic Dim keyword. The syntax for a simple declaration of a variable is as follows:
Dim variableName As variableType
Dim is the keyword means dimension which indicates to Visual Basic that a variable is being declared. variableName is the name assigned to the variable... The As keyword precedes the declaration of the variable type (String, Date, Integer etc). Now the variable can be declare as
Dim a,b,c As Integer
Here a,b,c are the variable and integer is the datatype of the variable...
Variables are declared using the Visual Basic Dim keyword. The syntax for a simple declaration of a variable is as follows:
Dim variableName As variableType
Dim is the keyword means dimension which indicates to Visual Basic that a variable is being declared. variableName is the name assigned to the variable... The As keyword precedes the declaration of the variable type (String, Date, Integer etc). Now the variable can be declare as
Dim a,b,c As Integer
Here a,b,c are the variable and integer is the datatype of the variable...
Q...Data types
The data type of a programming element refers to what kind of data it can hold and how it stores that data. Data types apply to all values that can be stored in computer memory or participate in the evaluation of an expression. Every variable, constant, property, procedure parameter, procedure argument, and procedure return value has a data type...Some data types are:
Byte: Stores an integer in the range 0 to 255 (8 bits).
Boolean Stores a value which is true or false...
Integer Stores an integer (whole number) in the range -32768 to + 32767 (16 bits). In VB.Net, the Integer data type is 32 bits..
Single: Stores a single-precision floating point number, accurate to 8 significant digits (32 bits).
Double: Stores a double-precision floating point number, accurate to 16 significant digits (64 bits).
Date Stores a Date.
String Stores a sequence of up to 63,000 characters
Variant The data type will change depending on what value is assigned to the variable. If not data type is specified, this is the data type that will be used.
Byte: Stores an integer in the range 0 to 255 (8 bits).
Boolean Stores a value which is true or false...
Integer Stores an integer (whole number) in the range -32768 to + 32767 (16 bits). In VB.Net, the Integer data type is 32 bits..
Single: Stores a single-precision floating point number, accurate to 8 significant digits (32 bits).
Double: Stores a double-precision floating point number, accurate to 16 significant digits (64 bits).
Date Stores a Date.
String Stores a sequence of up to 63,000 characters
Variant The data type will change depending on what value is assigned to the variable. If not data type is specified, this is the data type that will be used.
Q.......Scope and lifetime of variable?
The scope and duration of a variable is determined by the keywords Dim, Redim, Static, Public or Private. Whilst declarations can appear anywhere within the body of code, it is good programming practice to declare all variables at the beginning.
DIM: Used to declare variables in procedures. Each time the procedure is called, the data stored in variables declared with this keyword are reset (eg. an Integer data type will be reset to zero).
Static: Used to declare variables in procedures. Each time the procedure is called, the data stored in variables declared with this keyword are retained.
Public: Used to declare variables within a module. The variable is available to all procedures in the project.
Private: Used to declare variables within a module. The variable is only available to procedures within the project.
Redim : The ReDim keyword is used to re-dimension an array.
DIM: Used to declare variables in procedures. Each time the procedure is called, the data stored in variables declared with this keyword are reset (eg. an Integer data type will be reset to zero).
Static: Used to declare variables in procedures. Each time the procedure is called, the data stored in variables declared with this keyword are retained.
Public: Used to declare variables within a module. The variable is available to all procedures in the project.
Private: Used to declare variables within a module. The variable is only available to procedures within the project.
Redim : The ReDim keyword is used to re-dimension an array.
Q......Array???
Array is a data type that can hold more than one value of same data type. ....In programming, a series of objects all of which are the same size and type. Each object in an array is called an array element. For example, you could have an array of integers or an array of characters or an array of anything that has a defined data type....The important characteristics of an array are Each element has the same data type although they may have different values.......
Types of array:
Single dimensional Array: If an array is represented by a single index, it is called single dimensional array...
Single-dimensional arrays are the simplest form of arrays. These types of arrays are used to store number of items of a predefined type. All items in a single dimension array are stored in a row starting from 0...
Syntax:
Dim arrayname (size) As datatype
Example:
Two Dimentional array: The array which is used to represent and store data in a tabular form is called as 'two dimensional array.' Such type of array specially used to represent data in a matrix form....
Syntax: Dim arrayname(size 1, size 2)As data type
Example"
This shows the array with more than one dimension....It shows the result in the matrix form on the console application...
Multi dimensional array : When array is declared with using more than one dimension it is called as multi dimensional array....Two dimensional
Types of array:
Single dimensional Array: If an array is represented by a single index, it is called single dimensional array...
Single-dimensional arrays are the simplest form of arrays. These types of arrays are used to store number of items of a predefined type. All items in a single dimension array are stored in a row starting from 0...
Syntax:
Dim arrayname (size) As datatype
Example:
- Sub Main()
- Dim a(2) As Integer
- Dim i As Integer
- Console.WriteLine("ENTER ANY 2 ELEMENT:")
- For i = 0 To 1
- a(i) = Console.ReadLine()
- Next
- Console.WriteLine("YOUR MATRIX IS :")
- For i = 0 To 1
- Console.WriteLine(a(i))
- Next
- Console.Read()
- End Sub
Two Dimentional array: The array which is used to represent and store data in a tabular form is called as 'two dimensional array.' Such type of array specially used to represent data in a matrix form....
Syntax: Dim arrayname(size 1, size 2)As data type
Example"
- Sub Main()
- Dim a(2, 2) As Integer
- Dim i, j As Integer
- Console.WriteLine("ENTER THE ELEMENTS FOR 2X2 MATRIX:")
- For i = 0 To 1
- For j = 0 To 1
- a(i, j) = Console.ReadLine()
- Next
- Next
- Console.WriteLine("YOUR MATRIX IS :")
- For i = 0 To 1
- For j = 0 To 1
- Console.Write(a(i, j))
- Next
- Console.WriteLine()
- Next
- Console.Read()
- End Sub
This shows the array with more than one dimension....It shows the result in the matrix form on the console application...
Multi dimensional array : When array is declared with using more than one dimension it is called as multi dimensional array....Two dimensional
Conditional statement ?
Condition statement are also called the decision statement which verify the condition...Control Statements are used to control the flow of program's execution. Visual Basic supports control structures such as if... Then, if...Then ...Else, Select...Case, and Loop structures such as Do While...Loop, While...Wend, For...Next etc method....
IF condition: The If...Then selection structure performs an indicated action only when the condition is True; otherwise the action is skipped....When the compiler founds the if condition it verifies the condition , is the condition is true.....If the condition is found true then the following statement is executed which is there in the body of the statement....Other wise it action is skipped...
Syntax:
if (condition ) then
code to be executed
end if...
Example:
Dim i As Double
i = InputBox("Enter the percentage", "hello")
If i > 75 Then
MessageBox.Show("FIRST")
End If
IF then......Else :The If...Then...Else selection structure allows the programmer to specify that a different action is to be performed when the condition is True than when the condition is False. ...In this first compiler checks the condition if found true, then the statement inside it executes ...If found false it execute the else part ....
Syntax:
if (condition) then
statement
else
statament
end if..
Example:
Dim i As Double
i = InputBox("Enter the percentage", "hello")
If i > 40 Then
MessageBox.Show("PASS")
Else
MessageBox.Show("FAIL")
End If
If then elseif ..then else: Nested If...Then...Else selection structures test for multiple cases by placing If...Then...Else selection structures inside If...Then...Else structures......In this structure multiple condition are tested if the compiler identifies the the condition then that part of the program is executed.....
Syntax:
If (condition) then
statement
elseif (condition) then
statement
elseif (condition) then
statement
else
statement
end if...
Example:
Dim i As Double
i = InputBox("Enter the percentage", "hello")
If i > 75 Then
MessageBox.Show("FIRST")
ElseIf i >= 60 And i < 75 Then
MessageBox.Show("second")
ElseIf i < 60 And i >= 40 Then
MessageBox.Show("Third")
Else
MessageBox.Show("FAIL")
End If
Select case: In Visual Basic .NET, the switch statement is called "Select Case". This statement evaluates a single expression and compares it with a list of values. If a match found, then the corresponding block is executed. If no match found then the Else block is executed.
Syntax:
Select case (variablename or toolname)
case 1
statement
case 2
statement
case Else
statement
end select....
Example:
Dim i As Integer
i = InputBox("Enter the percentage", "hello")
Select Case i
Case 1
MessageBox.Show("MONDAY")
Case 2
MessageBox.Show("TUESDAY")
Case 3
MessageBox.Show("WEDNESDAY")
Case 4
MessageBox.Show("THURSDAY")
Case 5
MessageBox.Show("FRIDAY")
Case 6
MessageBox.Show("SATURDAY")
Case 7
MessageBox.Show("SUNDAY")
Case Else
MessageBox.Show("PLEASE TRY AGAIN")
End Select
IF condition: The If...Then selection structure performs an indicated action only when the condition is True; otherwise the action is skipped....When the compiler founds the if condition it verifies the condition , is the condition is true.....If the condition is found true then the following statement is executed which is there in the body of the statement....Other wise it action is skipped...
Syntax:
if (condition ) then
code to be executed
end if...
Example:
Dim i As Double
i = InputBox("Enter the percentage", "hello")
If i > 75 Then
MessageBox.Show("FIRST")
End If
IF then......Else :The If...Then...Else selection structure allows the programmer to specify that a different action is to be performed when the condition is True than when the condition is False. ...In this first compiler checks the condition if found true, then the statement inside it executes ...If found false it execute the else part ....
Syntax:
if (condition) then
statement
else
statament
end if..
Example:
Dim i As Double
i = InputBox("Enter the percentage", "hello")
If i > 40 Then
MessageBox.Show("PASS")
Else
MessageBox.Show("FAIL")
End If
If then elseif ..then else: Nested If...Then...Else selection structures test for multiple cases by placing If...Then...Else selection structures inside If...Then...Else structures......In this structure multiple condition are tested if the compiler identifies the the condition then that part of the program is executed.....
Syntax:
If (condition) then
statement
elseif (condition) then
statement
elseif (condition) then
statement
else
statement
end if...
Example:
Dim i As Double
i = InputBox("Enter the percentage", "hello")
If i > 75 Then
MessageBox.Show("FIRST")
ElseIf i >= 60 And i < 75 Then
MessageBox.Show("second")
ElseIf i < 60 And i >= 40 Then
MessageBox.Show("Third")
Else
MessageBox.Show("FAIL")
End If
Select case: In Visual Basic .NET, the switch statement is called "Select Case". This statement evaluates a single expression and compares it with a list of values. If a match found, then the corresponding block is executed. If no match found then the Else block is executed.
Syntax:
Select case (variablename or toolname)
case 1
statement
case 2
statement
case Else
statement
end select....
Example:
Dim i As Integer
i = InputBox("Enter the percentage", "hello")
Select Case i
Case 1
MessageBox.Show("MONDAY")
Case 2
MessageBox.Show("TUESDAY")
Case 3
MessageBox.Show("WEDNESDAY")
Case 4
MessageBox.Show("THURSDAY")
Case 5
MessageBox.Show("FRIDAY")
Case 6
MessageBox.Show("SATURDAY")
Case 7
MessageBox.Show("SUNDAY")
Case Else
MessageBox.Show("PLEASE TRY AGAIN")
End Select
Loop statement...
Visual Basic allows a procedure to be repeated many times as long as the processor until a condition or a set of conditions is fulfilled. This is generally called looping . Looping is a very useful feature of Visual Basic because it makes repetitive works easier...A loop is a fundamental programming idea that is commonly used in writing programs...An infinite loop is one that lacks a functioning exit routine . The result is that the loop repeats continually until the operating system senses it and terminates the program with an error or until some other event occurs.....
In visual basic there are three types of loop: For loop , while loop , Do until loop..
For loop:The FOR NEXT Loop , execute the loop body (the source code within For ..Next code block) to a fixed number of times..
Syntax:
Following program prints the table in list box...
Dim i, num, a As Integer
num = InputBox("ENTER ANY NUMBER", "HELLO")
For i = 1 To 10 Step +1
a = num * i
ListBox1.Items.Add(a)
Next
While loop: This loop executes a set of statements when a condition is True. The statement block is executed repeatedly until the condition becomes false. AS soon as the condition becomes false it terminates an jump out from the loop.... A while loop doesn't initialize or increment any fields automatically as part of the command, it just tests a condition and executes the loop for as long as the condition remains true. ..
syntax for while loop:
WHILE( condition)
body of loop
end while...
Following program print the table using the while loop...
Dim i, num, a As Integer
num = InputBox("ENTER ANY NUMBEr", "HELLO")
i = 1
While i <= 10
a = num * i
ListBox1.Items.Add(a)
i = i + 1
End While
when you execute this code it will ask for a number in inputbox after entering the number it gives the table of the number...
Do...Loop Until loop:A Do...Loop Until loop is a loop that runs until the loop's condition is true, the condition being checked after each iteration of the loop.
Syntax:
Do
' Loop code here
Loop Until condition
Example:
Do Until...Loop =A Do Until...Loop loop is almost the same as a Do...Loop Until loop. The only difference is that it has the condition's check at the beginning of each iteration of the loop.
Syntax:
Do Until condition
'Loop code here
Loop
Example:
Do...Loop While=A Do...Loop While loop runs until the loop's condition becomes false. Its condition is checked after each iteration of the loop...The Do...Loop While statement first executes the statements and then test the condition after each execution. ...
Syntax:
Do
Loop code here
Loop While condition
Example:
Do While...Loop =a Do While...Loop loop is almost the same as a Do...Loop While loop. And like the Do Until...Loop loop, the condition's check is at the beginning of each iteration...
Syntax:
Do While condition
' Loop code here
Loop
example:
In visual basic there are three types of loop: For loop , while loop , Do until loop..
For loop:The FOR NEXT Loop , execute the loop body (the source code within For ..Next code block) to a fixed number of times..
Syntax:
- For variable=[startValue] To [endValue] [Step]
- [loopBody]
- Next [variable]
Following program prints the table in list box...
Dim i, num, a As Integer
num = InputBox("ENTER ANY NUMBER", "HELLO")
For i = 1 To 10 Step +1
a = num * i
ListBox1.Items.Add(a)
Next
While loop: This loop executes a set of statements when a condition is True. The statement block is executed repeatedly until the condition becomes false. AS soon as the condition becomes false it terminates an jump out from the loop.... A while loop doesn't initialize or increment any fields automatically as part of the command, it just tests a condition and executes the loop for as long as the condition remains true. ..
syntax for while loop:
WHILE( condition)
body of loop
end while...
Following program print the table using the while loop...
Dim i, num, a As Integer
num = InputBox("ENTER ANY NUMBEr", "HELLO")
i = 1
While i <= 10
a = num * i
ListBox1.Items.Add(a)
i = i + 1
End While
when you execute this code it will ask for a number in inputbox after entering the number it gives the table of the number...
Do...Loop Until loop:A Do...Loop Until loop is a loop that runs until the loop's condition is true, the condition being checked after each iteration of the loop.
Syntax:
Do
' Loop code here
Loop Until condition
Example:
- Dim n As Integer
- n=10
- Do
- n=n+10
- Loop until n>5
- MsgBox (n)
Do Until...Loop =A Do Until...Loop loop is almost the same as a Do...Loop Until loop. The only difference is that it has the condition's check at the beginning of each iteration of the loop.
Syntax:
Do Until condition
'Loop code here
Loop
Example:
- Dim number As Long
- number = 0
- Do Until number > 1000
- number = number + 1
- ListBox1.Items.Add(number)
- Loop
Do...Loop While=A Do...Loop While loop runs until the loop's condition becomes false. Its condition is checked after each iteration of the loop...The Do...Loop While statement first executes the statements and then test the condition after each execution. ...
Syntax:
Do
Loop code here
Loop While condition
Example:
- Dim number As Integer
- number = 0
- Do
- number = number + 1
- ListBox1.Items.Add(number)
- Loop While number < 10
Do While...Loop =a Do While...Loop loop is almost the same as a Do...Loop While loop. And like the Do Until...Loop loop, the condition's check is at the beginning of each iteration...
Syntax:
Do While condition
' Loop code here
Loop
example:
- Dim number As Integer
- Do While number <= 100
- umber = number + 1
- ListBox1.Items.Add(number)
- Loop
Q......Procedure and function?
Procedure are made up of series of visual basic statement that , when called are executed....After the call the call is finished control return to the statement that is called the procedure....It can be used many time when you required..In other words we can say that A procedure is a block of code that performs some operation. The events we have been using so far are a special form of procedure known as an event procedure....
SUB procedure:
SYntax for procedure:
[Private | Public] Sub procName ([arglist])
Word public Indicates that the procedure is available to all modules. If Option Private is used in the module, the procedure is not available to modules outside of the project.
arglist: A list of variables passed to the procedure as arguments, and their data types. Multiple arguments are separated by commas. Arguments may be Optional, and may be Read Only.......
Example:
Public Sub sum(ByVal a As Integer, ByVal b As Integer)
Dim i As Integer
i = a + b
MessageBox.Show("SUM OF TWO NUMBER IS:=" & i)
End Sub
To call it just only write the sum(10,10) in the form load...
Function:
The basic Syntax for a function is:
[Private | Public] Function funcName ([arglist]) As Data Type
' Procedure body here
[funcName = expression]
End Function
Example:
Public Function sum(ByVal a As Integer, ByVal b As Integer)
Dim add, mul, i, j As Integer
add = a + b
i = MessageBox.Show("SUM OF TWO NUMBER IS:=" & add)
If i = DialogResult.OK Then
mul = a * b
MessageBox.Show("MULTIPLY OF TWO NUMBER IS:=" & mul)
End If
Return i
End Function
OPTIONAL ARGUMENTS: If an argument has the Optional keyword in front of it, then that argument does not have to be provided to the procedure. MsgBox is an example of a procedure that takes optional arguments. ...
Public Sub sum(Optional ByVal him As String = ("hello"))
MessageBox.Show("" & him)
End Sub
at runtime we dont need to pass any any argument since the argument is optional...
SUB procedure:
- A sub procedure is one kind of general procedure which is called from an event..
- A sub procedure can define a list of argument that must be passed to it
- To call the sub procedure you must pass the number of argument in sequence of data type....
- To call procedure you just only have to to type the name of procedure...
SYntax for procedure:
[Private | Public] Sub procName ([arglist])
Word public Indicates that the procedure is available to all modules. If Option Private is used in the module, the procedure is not available to modules outside of the project.
arglist: A list of variables passed to the procedure as arguments, and their data types. Multiple arguments are separated by commas. Arguments may be Optional, and may be Read Only.......
Example:
Public Sub sum(ByVal a As Integer, ByVal b As Integer)
Dim i As Integer
i = a + b
MessageBox.Show("SUM OF TWO NUMBER IS:=" & i)
End Sub
To call it just only write the sum(10,10) in the form load...
Function:
- Function procedure or just function is a general procedure that is similar to sub procedure except it returns the value...
- To code function you use the function statement specify the data type value that returns the value by the function....
- To return the value from the calling you assign the value to function name or you can code a retrn keyword followed by expression....
The basic Syntax for a function is:
[Private | Public] Function funcName ([arglist]) As Data Type
' Procedure body here
[funcName = expression]
End Function
Example:
Public Function sum(ByVal a As Integer, ByVal b As Integer)
Dim add, mul, i, j As Integer
add = a + b
i = MessageBox.Show("SUM OF TWO NUMBER IS:=" & add)
If i = DialogResult.OK Then
mul = a * b
MessageBox.Show("MULTIPLY OF TWO NUMBER IS:=" & mul)
End If
Return i
End Function
OPTIONAL ARGUMENTS: If an argument has the Optional keyword in front of it, then that argument does not have to be provided to the procedure. MsgBox is an example of a procedure that takes optional arguments. ...
Public Sub sum(Optional ByVal him As String = ("hello"))
MessageBox.Show("" & him)
End Sub
at runtime we dont need to pass any any argument since the argument is optional...
BY val and BY REF
The optional parts ByRef and ByVal are used to determine whether an argument is a copy of the variable, or the actual variable. ByRef indicates the variable is passed by reference, and any changes made within the procedure to the variable will be reflected to where the procedure was called.
ByRef is the default in Visual Basic 6, but this is changed in VB.Net. ByVal indicates the variable was passed by value, and any changes made within the procedure to the variable will not be reflected to where the procedure was called as it's only a copy.
ByRef is the default in Visual Basic 6, but this is changed in VB.Net. ByVal indicates the variable was passed by value, and any changes made within the procedure to the variable will not be reflected to where the procedure was called as it's only a copy.
Q......Return from function
The return statement is used to return the control from the calling function to the next statement of the called portion of the program. The return statement also causes the program logically to return to the point from where the function is accessed(called) . the return statement returns one value per call. The return statement can be any one of the type as shown below,
- return ;
- return ();
- return (constant);
- return(variable);
- return(expression);
- return(conditional expression);
- return(function);