Showing posts with label Variables. Show all posts
Showing posts with label Variables. Show all posts

Wednesday, October 27, 2010

Programming State

An object state (things the object has - attributes like Name, Price, Damage) is simply a value that needs to be stored in memory. Object states are implemented as constants or variables.

Note: Other terms for state include property, attribute and data member.


Constant
When we need to store a value that does not change, we declare a constant.

Code eg: Constant Declaration
Private Const strThisConstant As String = "Something"

        Variable
         When we need to store a value that will need to change, we use a variable.

Code eg: Variable Declaration
Private strThisVariable As String = "Something"

Addition



Code eg: Addition of two integer variables.

' Declare variables
Dim RoundOneScore As Integer = 5000
Dim RoundTwoScore As Integer = 4678
' Create the expression
Dim TotalScore As Integer = RoundOneScore + RoundTwoScore

                    The expression in the example will evaluate to 9678

Arrays: Variables That Represent More Than One Value

http://msdn.microsoft.com/en-au/library/ms172582.aspx

Words and Text: Using String Variables to Organize Words

From http://msdn.microsoft.com/en-au/library/whhs118y.aspx

"In this lesson, you will learn how to use the String data type to represent words and text.
The previous lesson showed how to use variables to store data in a program, and that each variable must be of the appropriate type for the data that it will store. In this lesson, you will learn more about the String data type, which is used to store text." Read more >>>

Representing Words, Numbers, and Values with Variables

From http://msdn.microsoft.com/en-au/library/9kc1d337.aspx

"Variables are an important concept in computer programming. A variable is a letter or name that can store a value. When you create computer programs, you can use variables to store numbers, such as the height of a building, or words, such as a person's name. Simply put, you can use variables to represent any kind of information your program needs.

You might ask, "Why use a variable when I could just use the information instead?" As the name implies, variables can change the value that they represent as the program is running. For example, you might write a program to track the number of pieces of candy you have in a jar on your desk. Because candy is meant to be eaten, the number of pieces of candy in the jar is likely to change over time. Rather than rewriting your program every time that you get a sugar craving, you can represent the number of pieces of candy with a variable that can change over time...." Read More >>

Variables

A variable is a value that can change. For instance, weather forecasters often refer to variable winds and variable temperatures - meaning that the wind and temperature could change without warning. In the context of computers, however, variables only change when told to and cannot do anything on their own.

Variables must be declared before they are used in Visual Basic.NET. To declare a new variable, you must use the Dim keyword, which is short for "dimension", referring to the process of reserving memory for the new variable.

You can create any variable you need using Dim. When you hear the word "variable', just remember that it is just a pointer to a location in memory in which some data is stored.

Variables may be used in many different situations such as storing a username, storing numbers for calculation, storing a weather temperature etc.

A declaration statement is used to declare the existence of the variable. It is defined as a particular data type. It may then be assigned a value.

Syntax
Dim VariableName As DataType

Code eg: Declaring a variable.
In this case, the variable is a String data type called strUserFirstName with a value of Straximo 
 
Dim strUserFirstName As String = "Straximo"