Introduction To VB
Introduction To VB
• Implicit Declarions
Dim var1, var2
Variable name conventions
• Must begin with a letter
• Must not exceed 255 character
• Must be unique within its scope
• Cannot contain a period or any other
type declaration character
Types of variables
• Numeric
• String
• Boolean
• Date
• Object
• Variant
Numeric Data Types
• Byte ( 0 – 255 )
• Integer ( -32,768 to 32,767 )
• Long ( -2,147,483,648 to 2,147,483,647 )
• Single ( -3.402823e38 to –1.401298e-45 )
• Double
(-1.79769313486232e308 to –4.94065645841247e-324)
( 4.94065645841247e-324 to 1.79769313486232e308 )
• Currency
(-922,337,203,685,477.5808 to 922,337,203,685,477.5807)
String Data Type
• A string variable can hold up to 2GB of
data
• Fixed Length Strings
Dim msg as string * 1000
Test = “24/06/1977”
Test = “12:40:34 AM”
Test = “24/06/1977 12:40:34 AM”
Test = #24/06/1977 12:40:34 AM#
Test = Date( )
Object Data Type
• An object variable refers to one of the
Visual Basic’s many objects.
Dim Var1
Dim var1 as variant
Data Type definition characters
Symbol Data Type Example
$ String A$, msg$
% Integer Phone%
& Long Color&
! Single Distance!
# Double ExactDistance#
Converting variable types
• CBool - Boolean • Clng - Long
• CByte - Byte • Ssng - Single
• CCur - Currency • CStr - String
• CDate - Date • Cvar - Variant
• CDbl – Double • CVerr - Error
• CInt - Integer
User Defined Data Type
Type Student
RollNo as integer
Name as string
End Type
Dim S as Student
S.RollNo = 1001
S.Name = “sumit”
Set A = CreateObject(Excel.Application)
if (A is nothing) then
Statements
End if
The Error value
• This value allows you to write function that
return variant types or errors. If the function
carries out its operation successfully, the result
is returned otherwise it can return an error
value.
Result = myFunction(arguments)
If isError(result) then
{Handle Error}
Else
{Use Result}
End if
Arrays
• A standard structure for storing data in
any programming language is an Array.
Whereas individual variables can hold
single values, arrays can hold a sets of
related data. An array has a name, as
does a variable and the values stored in
it can be accessed by an index.
Declaring Arrays
• Dim salary(10) as integer
• Dim salary(1 to 10) as integer
• Dim salary(-10 to 10) as integer
• Dim marks(1 to 5, 1 to 4) as integer
Arrays of Arrays
• One of the possibilities introduced to the
Visual Basic language with the variant
data type if that of creating complex
structures, such as arrays of arrays. If
an array is declared as variant, you can
assign other data types including
arrays.
Example: Arrays of Arrays
• Dim intArray(1 to 10) as integer
• Dim strArray(1 to 10) as string
• Dim BigArray(1 to 2) as variant
• BigArray(1) = intArray ( )
• BigArray(2) = strArray ( )
• T1 = Temperatures.item(“Delhi”)
• T1 = Temperatures.item(6)
• C = Temperatures.count
Processing collection items
For each city in temperatures
total = total + city
Next
Sub Name(arguments )
statements
End Sub
Event Handlers
• An Event Handler is a short segment of code that is
executed each time an external condition triggers the
event. When the user clicks a control, the control’s
click event handler executes. This handler is nothing
more than a sub-routine, which performs all actions
you want to perform when the control is clicked. It is
separate from the rest of code and does not have to
know what would happen if another control was
clicked or if the same control was double clicked.
• By Value
Function Add(ByVal A as integer, ByVal B
as integer) …
Optional Arguments
Function Test(a as string, optional b as double) as double
If IsMissing(b) Then
statements
Else
statements
End if
End Function
Contact(Email:=“[email protected]
m”, name:=“Pardeep”)
Returning arrays & Custom data type