Data Type Conversions
When you are assigning a value to a variable or performing some operation on two values, all the types involved in the operation must be the same. For example, it is not possible to store a String value into an Integer variable, because the two types are not the same. When you are working with many different data types of information, however, it is often necessary to convert one type of information to another. While it is not possible to store a String value into an Integer variable, it is possible to take a String value and convert it into an Integer value (which can then be stored into an Integer variable).
By default, values in Visual Basic.Net will be converted from one type to another as needed. The compiler can do this because the types of all values and variables in Visual Basic.Net are always known and any needed conversions can be deducted from context. In the following example, the value in the String variables is implicitly converted to Integer when “a”is assigned to “b”.
Dim a as String
Dim b as Integer
a = “10”
b = a
It is also possible to explicitly convert a value from one type to another by using the conversion operators, listed in table.
Function | Converts Its Arguments To |
CBool | Boolean |
CByte | Byte |
CChar | Unicode character |
CDate | Date |
CDbl | Double |
CDec | Decimal |
CInt | Integer (4-byte integer, Int32) |
CLng | Long (8-byte integer, Int64) |
CObj | Object |
CShort | Short (2-byte integer, Int16) |
CSng | Single |
CStr | String |
Example:
To convert the variable initialized as
Dim A as Integer
to a Double, use the function:
Dim B as Double
B = CDbl(A)
Suppose you have declared two integers, as follows:
Dim A as Integer, B as Integer
A = 23
B = 7
The result of the operation A/B will be a double value. The following statement:
Console.Write (A/B)
displays the value 3.28571428571429. The result is a double, which provides the greatest possible accuracy. If you attempt to assign the result to a variable that hasn’t been declared as Double, and the Strict option is On, then VB.NET will generate an error message. No other data type can accept this value without loss of accuracy.
You can also use the type CType() function to convert a variable or expression from one type to another. Let’s say the variable A has been declared as String and holds the value “34.56”. The following statements converts the value of the A variable to a Decimal value and uses it in a calculation:
Dim A as String = “34.56”
Dim B as Double
B = CType(A,Double) / 1.14
The conversion is necessary only if the Strict option is On, but it’s good practice to perform your conversions explicitly.
Widening and Narrowing Conversions
Conversions between some types are always guaranteed to succeed, while others are not. In the following example, the first conversion may fail if the Short variable contains a value that will not fit into a Byte variable, whereas the second conversion will always succeed because any value that fits into a Byte variable will fit into a Short variable.
Dim a as Byte
Dim b as Short
a = b
b = a
A conversion that is always guaranteed to succeed is called a widening conversion because the set of values that the destination type can hold is wider than the set of values that the source type can hold. In the preceding example, a conversion from Byte to Short is widening because every value of Byte is a valid value of Short as well. A conversion that might fail, however, is called a narrowing conversion because the set of values that the destination type can hold is narrower than the set of values that the source type can hold. Converting a Short value to Byte may or may not succeed, depending on whether the value is in the range of the Byte type.
Using Procedure
The one thing you should have learned about programming in Visual Basic so far is that an application is made up of small, self-contained segments. The code you write is not a monolithic listing; it is made up of small segments called procedures, and you work on one procedure at a time.
For example, when you write code for control’s Click event, you concentrate on the event at hand – namely, how the program should react to the Click event. What happens when the control is double clicked, or when another control is clicked, is something you will worry about later, in another control’s event handler. This divide and conquer approach isn’t unique to programming events. It permeates the Visual Basic language, and even the longest applications are written by breaking them into small, well-defined tasks. Each task is performed by a separate procedure that is written and tested separately from the others. The benefit of the procedure is that code is cleaner and easier to maintain.
The two types of procedures supported by Visual Basic are:
1. A subprocedure performs actions
2. A function procedure performs actions and returns a value ( the return value)
Subroutine
A subroutine is a block of statements that carries out a well-defined task.
The block of statements is placed within a set of Sub . . .End Sub.Sub statements can be invoked by name. The following subroutine displays the current date in a message box and can be called by its name, ShowDate();
Sub ShowDate()
MsgBox(Now())
End Sub
Normally, the task a subroutine performs is more complicated than this; nevertheless, even this is a block of code isolated from the rest of the application. All the event handlers in Visual Basic, for example, are coded as subroutines. The actions that must be performed each time a button is clicked are coded in the button’s Click procedure.
The statements in a subroutine are executed, and when the End Sub statement is reached, control returns to the calling program. It’s possible to exit a subroutine prematurely, with the Exit Sub statement. For example, some condition may stop the subroutine from successfully completing its task.
All variables declared within a subroutine are local to that subroutine. When the subroutine exits, all variables declared in it cease to exist.
Most procedures also accept and act upon arguments. The ShowDate() subroutine displays the current date on a message box. If you want to display any other date, you’d have to pass an argument to the subroutine telling it to act on a different value, like this:
When we use user define subroutine, we can call it by “call” or simply by sub routine name.
Example:
Private Sub Button1_click ( . . . . )
call Amit()
End Sub
Sub Amit()
MsgBox(“ Best of Luck for Exam ”)
End Sub
Types
· No argument
· Argument (By value)
· Argument (By reference)
At process level it has two types:
· Predefine sub routine (event handlers)
· User defined sub routine
Function
A function is similar to a subroutine, but a function returns a result. Subroutines performs a task and don’t report anything to the calling program; functions commonly carry out calculations and report the result. Because they return values, functions – like variables – have types. The value you pass back to the calling program from a function is called the return value, and its type must match the type of the function. Function accepts arguments, just like subroutines. The statements that make up a function are placed in a set of Function . . . End function statements.
Functions are of four types:
1. No Argument No Return Value (NANR)
2. No Argument with Return Value (NAWR)
3. With Argument No Return Value (WANR)
4. With Argument With Return Value (WAWR)
Examples left
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim c AsInteger
c = amit(10)
MsgBox(c)
EndSub
Function amit(ByVal a AsInteger) AsInteger
Dim f AsInteger
f = a * 2
Return f
EndFunction
Compiled By: Chaudhary Amit V.
Comments
Post a Comment