Monday, 10 March 2014

Types of Variables ( Data Types ) In Visual Basic

Types of Variables ( Data Types )



In Visual Basic Variables are categorized in to six main types.


  1. Numeric
  2. String
  3. Boolean
  4. Date
  5. Variant
  6. Object


Numeric Data Type:


Numeric Variables are used to sore numbers. In Visual Basic following numeric data types exists.

  • Byte Data Type:

         The Byte Data Type holds integer value in range 0 to 255. Bytes Data Type stores data in one byte memory. Some window API ( Application Programming Interface ) provide arrangement of Byte type. e.g.

                                   Dim var1 As Byte

  • Integer Data Type:

          The Integer Data Type consumes two bytes of memory and holds integer value in range -32,768 to +32,767. Variables of integer data type are declared with intgere type.

                                  Dim var1 As Integer

  • Long:

         The Long Data Type consumes four bytes memory and holds value in range -21,474,836,48 to +-21,474,836,47. Variable of Long type is declared with Long type.

                                 Dim var As Long

  • Single:

          The Single Data Type stores single precision floating-point numbers. The range for positive values is 1.401298 E-45 to 3.402823 E38 and range for negative values is -3.402823 E38 to -1.401298 E-45. Single data type consumes 4 bytes memory. Accuracy and precision or single data type is not better, e.g. the value 0 cannot be represented precisely ( it is very small number, but not exactly 0 ). The Single type variable is declared with Single data type.

                               Dim a As Single
                               a = 1/3
                               print a                         { Output 0.333333}

  • Double:

          Double Data Type stores double precision floating-point numbers. The range for positive value is from 4.94065645841247 E-324 to 1.79769312486232 E308 and range for the negative value is from 1.79769312486232 to -4.94065645841247 E-324. The result produced by the Double precision is more accurate and precise. Double Data Type consumes eight bytes of memory. The Double type variable is declared with reserve word Double.

                              Dim var1 As Double
                              var1 = 1/3
                              Print var1                        { Output 0.333333333333333 }

  • Currency:

          Currency Data Type stores fixed point numbers with four decimal digits.The currency Data type can represent numbers in the range from -922,337,203,685,477.5808 to 922,337,203,685,477.5807. Currency data type is for financial calculations, which do not need more accuracy. Currency variables are declared with the currency type.

                             Dim Price As Currency

String Data Type:


String Data Type stores only text type of data and can hold 2GB data. String Variable's declaration of two types.

  • Variable Length Strings:

          This type of String variable is declared as.

                          Dim var1 As String

            The Variable var1 can grow and shrink according to the length of text, which you assign to it e.g. if var1 hold value "Maqbool" then length of Var1 will be 7.

  • Fixed-Length Strings:

         This type of variable is declared as:

                            Dim Var1 As String * n

          Where n represents length for var1 e.g.

                           Dim var1 As String * 5

In this case var1 can hold 5 characters. But if we try to store less or more character in var1 this variable can only and only stores five character data like mentioned in the following examples.

                         Dim var1 As String * 5
                         Var1 = " Ali "
                         Print var1                             { Output Ali-- }
                         var1 = " Minhas "
                         Print var1                             { Output Minha }

Values assigned to String variables in double quotes as shown above.

Note: If length of String Constant increases then you can break the string and continue it from another line by placing _ Symbol at the end of breaking line e.g.

                         Print " Hellow Mr " _
                               "Hanan"

                                                           Output is 
                                                                             Hello Mr Hanan

Boolean Data Type:


This type of variable stores either True or False value. Although one byte is enough for true and false values. But Visual Basic considers True and False values as integer i.e. any non-zero value is consider 1 as True and 0 as False. Boolean type of variable is declared with word Boolean like following.

                        Dim Check As Boolean

Value can be assigned to variable named as check like the following.
                       Check = True or Check = 5

Both statement hold True value in Check Variable.

Date Data Type:


The Date Data Type stores Double precision number. The integer part represents the date and the fractional part represents the time. Date variable can store both date and time. Date Type variable is declared with the reserved word date like following.

                             Dim dob As Date

The value that are assigned to a date type variable, Must be enclosed in double quotes or hash symbol (#) e.g.

  1. dob = "01/01/1997"
  2. dob = "13.03.05 am"
  3. dob = "20/02/1995 13:00:05 am"
  4. dob = #20/02/1995 13:00:05 am#
The Date type variable can hold date in range January 1,0100 to December 31,9999. Visual Basic stores date in United State Date format (i.e. mm/dd./yy). If date is entered in any format Visual Basic automatically swaps date into above format mm/dd./yy.

Variant Data Type:


As discussed above the most flexible data type of Visual Basic is Variant. Because it can accommodate other data types. Variant type variable can be declared as 

                                  Dim var2

If integer data is entered into variant variable then Visual Basic treats it as Integer data type and similarly if string data is stored in a variant variable then Visual Basic considers it as string variable. A variant type variable can hold number data upto the range of double data type and string data upto the range of variable's length. It is Important to note that when a variable is used without declaring it, then default type of this variable will be variant. Similarly in the following statement.

                                   Dim x, y, z As Integer

Type of variable x and y will be considered as Variant and type z will be integer.

Object Data Type:


An object data type variable refers to any one of Visual Basic object. Object variable is declared with the reference of an object e.g.

                                   Dim var1 As CommandButton

Now variable var1 is type of Object variable and can be referred to any CommandButton like the following.

                                  set var1 = Command1

Now the following statements have the same purpose.

                                  Command1.Caption = "Save".        or
                                  var1.Caption = "Save"


Me : If you need more help, want to ask feel free to use below comment box. It will be my pleasure to help you. THANKS for reading.

0 comments:

 
Search Engine Submission - AddMe url submit Casino us org Seo Packages http://blogsiteslist.com