Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

C# Variables [Beginner Level Tutorial]

C# Variables



A variable is simply a place to store data in memory (Place Holders). By using variables you save yourself a lot of time and hassle.

String Variables

A string variable is and always will be Text! A string variable is used to hold a block of text until you call upon it at some point in your program.

How To Declare A New String Variable

Begin by creating a new button on your form. Double click your button and in between the brackets, type the following:

Code
string test = "Hello Hackforums";

String
This simply sets the variable type.

Test
This is the name of the variable, it can be anything you want as long as it is not one of the saved terms, for example, you can not have a string variable name "string"

= "Hello Hackforums";
This stores the text inside of the variable, by typing "=" you are assigning the text. "Hello Hackforums" is the text being stored inside of the variable. Text will always be stored inside of "".


Displaying Text In A String

To display the text that is stored inside of your string variable you do the following:

Code
MessageBox.Show(test);

The reason we can simply type (test) is because we have a predefined variable named test, with a value.
Spoiler (Click to View)



Integer Variables

An integer variable will always store a number. When setting up an integer variable you do NOT put the number inside of " ". The reason for this is because " " is only used when working with plain text. To begin setting up an integer, make a new button, and type the following.

Code
int test = 45;

Int = The Variable Type
test = The Variable Name
45 = The Value Inside Of The Variable

Now, we are going to do something different. Create 2 NEW integer variable underneath the one you already created. Name them whatever you want. However, in the 3rd variable, type the following.

Code
int test3 = variable1 + variable2;

This should be very easy to understand, this third variable holds the value of the first two variables added together.

Finally, to display the answer, do the following:

Code
MessageBox.Show(test3.ToString());

The reason we do test3.ToString() is because test 3 is an integer and it cannot be displayed normally as text. So, in order to display the variable as text we have to convert it to a string!
Spoiler (Click to View)

Conclusion
There are many many more variable types, but I am not going to get into them right now. Please, if you have any questions/concerns then post! If you are having trouble I will be on the rest of the night to give assistance. I am also going to be posting more advanced tutorials soon.

Finished Code:
Code
string variable1 = "Hello Hackforums";
            
            MessageBox.Show(variable1);

Code
int variable1 = 45;
            int variable2 = 40;
            int test3 = variable1 + variable2;

            MessageBox.Show(test3.ToString());
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
Thank you, I don't know anything about C#.
I would love to see more Tutorials from you.
(Oct 20, 2010 06:04 PM)Chewbaka Wrote:  Thank you, I don't know anything about C#.
I would love to see more Tutorials from you.

Glad to hear it! I will be posting many more tuts in the near future.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
You should go into global/local and private/public variables.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Javascript]  by Z3R00NDATOct 16, 2023 01:24 PM 3 134 Oct 16, 2023 01:24 PM
Last Post: Disease
[C] by bhaktefhabo87Oct 11, 2021 07:28 AM 2 184 Oct 11, 2021 07:28 AM
Last Post: Cool_Pro_Grammer
[Python] by simmerMay 28, 2021 05:33 AM 5 202 May 28, 2021 05:33 AM
Last Post: simmer