1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Data types in Visual Basic

Discussion in 'Visual Basic [VB]' started by Sanskruti, Mar 26, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    When you decide to use a variable, you are in fact asking the computer to use a certain amount of space to hold that variable. Since different variables will be used for different purposes, you should specify the kind of variable you intend to use, then the computer will figure out how much space is needed for a particular variable. Each variable you use will utilize a certain amount of space in the computer's memory.

    Before declaring or using a variable, first decide what kind of role that variable will play in your program. Different variables are meant for different situations. The kind of variable you want to use is referred to as a data type. To specify the kind of variable you want to use, you type the As keyword on the right side of the variable's name.

    The formula to declare such a variable is:

    Dim VariableName As DataType

    Once you know what kind of variable you will need, choose the appropriate data type. Data types are organized in categories such as numbers, characters, or other objects.

    String



    A string is an empty text, a letter, a word or a group of words considered. To declare a string variable, use the String data type.

    Here is an example:
    Code:
    Private Sub Form_Load()
        Dim City As String
    End Sub
    After declaring the variable, you can initialize. If you want its area of memory to be empty, you can assign it two double-quotes.

    Here is an example:

    Code:
    Private Sub Form_Load()
        Dim City As String
        
        City = ""
    End Sub
    If you want to store something in the memory space allocated to the variable, assign it a word or group of words included between double-quotes.

    Here is an example:

    Code:
    Private Sub Form_Load()
        Dim City As String
        City = "Mumbai"
    End Sub 
    You can also initialize a string variable with another.

    Boolean



    A Boolean variable is one whose value can be only either True or False. To declare such a variable, use the Boolean keyword.

    Here is an example:
    Code:
    Private Sub Form_Load()
        Dim Married As Boolean
    End Sub
    After declaring a Boolean variable, you can initialize by assigning it either True or False.

    Here is an example:

    Code:
    Private Sub Form_Load()
        Dim Married As Boolean
        Married = False
    End Sub
    Like any other variable, after initializing the variable, it keeps its value until you change its value again.

    Numeric Data Types



    A natural number is one that contains only one digit or a combination of digits and no other character, except those added to make it easier to read. Examples of natural numbers are 122, 8, and 2864347. When a natural number is too long, such 3253754343, to make it easier to read, the thousands are separated by a special character. This character depends on the language or group of language and it is called the thousands separator. For US English, this character is the comma. The thousands separator symbol is mainly used only to make the number easier to read.

    Byte



    A byte is a small natural positive number that ranges from 0 to 255. A variable of byte type can be used to hold small values such as a person's age.

    To declare a variable for a small number, use the Byte keyword.

    Here is an example:
    Code:
    Private Sub Form_Load()
        Dim Student As Byte
    End Sub

    Integer



    An integer is a natural number larger than the Byte. It can hold a value between
    -32,768 and 32,767.
    To declare a variable of type integer, use the Integer keyword.

    Here is an example:
    Code:
    Private Sub Form_Load()
        Dim Tracks As Integer
    End Sub

    Long Integer



    A long integer is a natural number whose value is between –2,147,483,648 and 2,147,483,642. To declare a variable that can hold a very large natural number, use the Long keyword.

    Here is an example:

    Code:
    Private Sub Form_Load()
        Dim Population As Long
    End Sub

    Decimal Data Types



    A real number is one that displays a decimal part. This means that the number can be made of two sections separated by a symbol that is referred to as the Decimal Separator or Decimal Symbol. This symbol is different by language, country, group of languages, or group of countries. On both sides of the Decimal Symbol, digits are used to specify the value of the number. The number of digits on the right side of the symbol determines how much precision the number offers.

    Single



    A single is a decimal number whose value can range from –3.402823e38 and –1.401298e-45 if the number is negative, or 1.401298e-45 and 3.402823e38 if the number is positive. To declare a variable that can hold small decimal numbers with no concern for precision, use the Single data type.

    Here is an example:

    Code:
    Private Sub Form_Load()
        Dim C As String
        Dim Married As Boolean
        Dim Student As Byte
        Dim Tracks As Integer
        Dim Population As Long
        Dim Distance As Single
    End Sub

    Double



    While the Single data type can allow large numbers, it offers less precision. For an even larger number, Microsoft Visual Basic provides the Double data type. This is used for a variable that would hold numbers that range from 1.79769313486231e308 to –4.94065645841247e–324 if the number is negative or from 1.79769313486231E308 to 4.94065645841247E–324 if the number is positive. To declare a variable that can store large decimal numbers with a good level of precision, use the Double keyword.

    Here is an example of declaring a Double variable:

    Code:
    Private Sub Form_Load()
        Dim Distance As Double
    End Sub

    Currency



    The Currency data type is used for a variable that can hold monetary values. To declare such a variable, use the Currency keyword.

    Here is an example:

    Code:
    Private Sub Form_Load()
        Dim C As String
        Dim Married As Boolean
        Dim Student As Byte
        Dim Tracks As Integer
        Dim Population As Long
        Dim Distance As Single
        Dim Salary As Currency
    End Sub

    Other Data Types



    Date



    A date is a numeric value that represents the number of days that have elapsed since a determined period. A time is a numeric value that represents the number of seconds that have elapsed in a day.

    To declare a variable that can hold either date values, time values, or both, use the Date keyword. After the variable has been declared, you will configure it to the appropriate value.

    Code:
    Private Sub Form_Load()
        Dim C As String
        Dim Married As Boolean
        Dim Student As Byte
        Dim Tracks As Integer
        Dim Population As Long
        Dim Distance As Single
        Dim Salary As Currency
        Dim DOB As Date
        Dim KickOffTime As Date
    End Sub

    Variant



    A Variant can be used to declare any kind of variable. You can use a variant when you can't make up your mind regarding a variable but, as a beginning programmer, you should avoid it.
     

Share This Page