Data variable, constant and data types
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Data variable, constant and data types

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Posts
    7

    Data variable, constant and data types

    I always find it hard to understand data variables, constants and data types

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,074

    Re: Data variable, constant and data types

    Data Types

    A Data type identifies the type of information you’d like to store inside a variable. By instructing the compiler what Data Type you would like to use, you do two things :
    1. Create the necessary space in memory for that particular variable
    2. Ensure that your variable can hold only the specific data that you have specified. For example: When you create a variable soleley intended for a date, you will not be able to enter any other type of information inside it

    Variables

    A variable is a container where a piece of information can be stored. This container exists solely in memory (computer memory). You could store anything inside that designated memory location be it a name, a whole document, a number or a date. When you create a variable, you create space in memory for that variable’s contents.

    How do I create Variables?

    In order to create a variable, you need to make use of the Dim statement, here’s an example :

    Code:
    Dim intAge As Integer
    This little statement creates a variable named intAge. intAge is created with an Integer (whole number) datatype.

    Here is another Example :

    Code:
    Dim strName As String
    strName is the name of our variable. String is our variable’s data type. This means that strName can accept any alphabetic and numeric input. Do not get confused now with numeric input. Remember, as I mentioned before Data Types are quite strict in what they allow into a variable. Strings can accept both Alphabetic and numeric input; the difference is that it doesn’t see the numeric input as actual numeric data that should be used in calculations.

    Storing values in a variable

    Stroing a value in a variable is as simple as :

    Code:
    intAge = 35
    strName = “Hannes”
    With the small examples above I stored a vlaue into each of our variables we created earlier. Just a note: you will not necessarily store all your data in your variables simultaneaosly or independantly. It just depends on when you need to store which info.

    Another note: Remember I spoke about String Data types? Well, look closely to the strName variable assignment I did above. I made use of “ and “ around the data. If I did this :

    Code:
    strName = “Hannes 123”
    It would also have been a legal string; obviously using numbers for a name doesn’t make sense, but this was just a silly example.

    Constants

    Constants are also a container where a piece of information can be stored in memory. So what is the difference between Constants and Variables

    Constants vs. Variables

    The difference is simple. A Variable, as its name implies, can hold varying values. This means that you can change the value of a variable umpteen times and it will still store each change at the same location.
    Constants however refer to values that cannot be changed during program’s execution. This means that once you store a value as a constant, that value cannot be changed, ever.

    How do I create Constants?

    You create a Constant as follows :

    Code:
    Const intMonths = 12
    With Constants you name your constant and immediately assign it a value. That value cannot ever change, so if you were to assign a different value into your constant ( as shown in the next tiny example) you will get an error.

    Code:
    intMonths = 13
    ** This is part of a book I'm trying to write...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center