DFBarracks - Forums




Delta Force Barracks Forums - Powered by XMB 1.9.11
Delta Force Barracks Forums
Not logged in [Login - Register]
Go To Bottom

Printable Version  
Author: Subject: Tutorial Level: Intermediate: Variables Part I
bullet magnet
Captain
****


Avatar


Posts: 263
Registered: 6-26-2005
Member Is Offline

Mood: Modding

[*] posted on 6-27-2007 at 04:58 PM
Tutorial Level: Intermediate: Variables Part I


Now lets get to one of the backbones of Visual Basic, or any language for that matter, variables. This is where it gets a bit complicated but it must be learned.

Variables in real life apply to programming the same way.

In P+6=10, P is the variable since it is unknown, although we know it is 4.

Variables do the same in programming, they hold a value, until you need it.

Lets get a running start.


To use a variable, you need to declare, or "set" it. This is usually done with the keyword "Dim", after Dim, you type what you want the name of your variable to be, in this case we'll be using "firstvariable". After this, we need to tell what type of variable it is.

Just a few of he different variable data types are:

Long, Buffer, Boolean, Byte, Integer, Double, String

I'll explain a few of them.

A Boolean is a True or False

A Integer is a number between -2,000,000,000 and 2,000,000,000

A Double handles decimals.

A String is Text.

1.) Ok lets put these into action to help you understand a bit better.

Add a button to a form and a Label, now lets Declare or Set a variable. Here is how its done:

Code:
[size=3]Dim myfirstvariable As String[/size]


2.) OK, lets discuss what the above code did, it Declared or Set a variable named "myfirstvariable" and made it the data type "String" which is Text.

3.) Alright, so we have a variable, remember variables hold something until it needs to be used, this one is not being used yet so lets put it to use.

We are going to use that Variable to store the text "Hello". Here is how you do this:

Take your variable name, and set it equal to the text "Hello" like this:

Code:
[size=3]myfirstvariable = "Hello"[/size]


4.) Alright now lets review, we declared a variable called myfirstvariable, and made it the type "String" which means it stores text. We then made it store the text "Hello"

5.) Ok so it stores the text "Hello" now we can put our variable to use in the program.

6.) You should have this code in the Button1 code.

Code:
[size=3]Dim myfirstvariable As String myfirstvariable = "Hello"[/size]


Now lets add:

Code:
[size=3]Label1.Text = myfirstvariable[/size]


Should be this:
Code:
[size=3]Dim myfirstvariable As String myfirstvariable = "Hello" Label1.Text = myfirstvariable[/size]


7.) Now lets run the program, when you click the button, what text do you think will appear on the Label?


The text should read "Hello", this is because the Label read the Text stored in the variable "myfirstvariable"

Quotes

You need to understand Quotation Marks to do variables. In the code above, if you had wrote it like this

Code:
[size=3]Label1.Text = "myfirstvariable"[/size]
instead of
Code:
[size=3]Label1.Text = myfirstvariable[/size]


The label would have said just that, "myfirstvariable" If you use quotes, it uses the text As-Is, if you do not, it uses the value stored in your variable.

You have now made a variable, play around with it.

Check in for more,

Bullet Magnet




View user's profile View All Posts By User
Distant Storm
General
******


Avatar


Posts: 870
Registered: 9-13-2004
Member Is Offline

Mood: Distant

[*] posted on 6-28-2007 at 09:10 AM


Great work mate nice and clear, keep em comming




View user's profile Visit user's homepage View All Posts By User
bullet magnet
Captain
****


Avatar


Posts: 263
Registered: 6-26-2005
Member Is Offline

Mood: Modding

[*] posted on 6-28-2007 at 01:39 PM


Thanks, tryin to put this through the eyes of a beginner. Got more coming.



View user's profile View All Posts By User

  Go To Top

Powered by XMB 1.9.11
XMB Forum Software © 2001-2012 The XMB Group
[Queries: 15] [PHP: 96.6% - SQL: 3.4%]












Copyright © 2002 - 2008 All Rights Reserved - DFBarracks.com
Site Design by A.Bullet - ScreamingDemon.com