![]() |
|
[C#] C# Variables [Beginner Level Tutorial] - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] C# Variables [Beginner Level Tutorial] (/showthread.php?tid=755046) |
C# Variables [Beginner Level Tutorial] - Pulser - Oct 20, 2010 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";Code int variable1 = 45;* - Chewbaka - Oct 20, 2010 Thank you, I don't know anything about C#. I would love to see more Tutorials from you. * - Pulser - Oct 20, 2010 (Oct 20, 2010 06:04 PM)iichewbakaii Wrote: Thank you, I don't know anything about C#. Glad to hear it! I will be posting many more tuts in the near future. * - tflo - Oct 21, 2010 You should go into global/local and private/public variables. |