C# Programming Beginner Tutorial: Variables & Data Types to Fuel the Gaming Engine!
Hey everyone,
I'm so excited to have you back! Today's going to be a good one, because there's so much information that I will be able to provide you in how variables can be used in your programming careers and lives in general. Variables and Data Types are the backbone of (pretty much) everything in the programming world. We'll even dive down the Rabbit Hole in order to show you how you can use Variables & Data Types in cooperation with each other to create objects that can, then, be reused and used as a hierarchy chain of other objects.
So, without further Ado...
Variables
Ok, so here we sit on the edge of our seats, wondering what I'll say about the greatness of variables. Variables are, as I described previously, basically a way to store information for later use. It is the Mathematics of the programming world. These are the components that make the Hamster run inside his proverbial wheel.
Variables can come in any data type as long as it's storing the correct information in the appropriate way. There are mathematical-based variables (integers, decimals, floating point decimals, etc.) and word/character-based variables (text strings, characters) and dates and lists and other cool stuff! All of these are used to create a foundation for your own programming. In conjunction with coded logic, the program that you build and execute can do a nearly-infinite number of things based on what's happening.
This is why, when asked by clients or other people "Can you do [insert crazy request here] on the computer?" I like to answer back that "I can do almost anything that's needed, but some of the things just might not be feasible in the limited amount of time that I have." I mean, let's face it, we as Humans can program Space Shuttles and orbital Satellites; Video Games and applications that are made specifically to make us avoid knowing what's going on in the world; and program Robots to perform tasks that we can't physically perform or that we believe is beneath us. I tend to program in the hope that some day I can start and finish a video game (which I believe is where we will be heading as these tutorials ramp up) or to automate mundane work tasks that take up my valuable time.
The thing is, variables at their core are just arbitrarily-named data types. As an example, here is what most would agree is a naming convention that (while maybe not perfect) tells what it is and what it does:
decimal invoice_tax_percentage; //boring, but it says so
...and here's one that most should agree is horrible:
decimal Turkey_sandwich_on_Rye; //uh, what? are we ordering sammiches for everyone?
As you can see, that last one doesn't say anything about it and does not give any indication of what it will store. Then, of course, you have the standards ones that I see all the time (a la basic math formulas):
int x; //x can be anything
int y; //y can be anything
I actually use these, but only in a temporary way. For, you see, variables can be set at a public level or a private level (or internal or protected, yes) and each of these explains directly the access level. A public variable is one that can be accessed anywhere within the class or from a derived object of the class... this means, to try to put in more understandable words, that if you create an object called character that holds character attribute stats for a D&D-style Character Sheet (as was discussed here), the variables within the class can be used outside of it if someone were to create an object of data type CharacterSheet.
As an example:
using DnD_Character_Sheet;
.
.
.
CharacterSheet my_character_sheet = new CharacterSheet();
.
.
.
my_character_sheet.Dex += 1; /increase the Dex attribute on my character sheet by 1.
So what you see above are 3 lines that would first (using DnD_Character_Sheet) link the new program to the code in the CharacterSheet class in order to utilize its functionality. the using statement is a way to say "please use the following class".
The next line that you see (CharacterSheet my_character_sheet = new CharacterSheet();) creates a new instance of the class. This is the key to using objects. You have 1 base class (CharacterSheet here) and you can create many different objects from it. Then, each of those derived objects can use the same internal structure.
What I mean is that each has the attribute-based variables that are accessible outside of the class code itself (because they are all set up as public) and a couple of functions. The only function (action-based code) that is accessible outside of the class itself is the DiceRoll() function as all of the others are considered private.
In the hopes that I'm not losing you, because we're sliding down the walls in this Rabbit Hole very quickly! I know this is becoming a bit jargon-y and I apologize, but these are definitely terms that you need to understand and grasp to become a successful programmer.
Let me know in the comments whether this makes sense or if I need to spend more time in my next Post discussing Variables. The one thing you can count on is that they will be used in almost all (if not all) programs that you build.
Now, let's actually spend some time discussing Data Types and what they can do.
Data Types
So, Data Types are basically exactly what they say they are, but more confusing. Underneath it all, they are just pieces that can only use certain types of information. Below I have a list of the most common data types that I use:
char: A text-based character... to define a char you use single-quotation marks surrounding a single character.
string: A text-based string of characters that can relate nearly anything... to define a string you use double-quotation marks surrounding the entire line of text.
int: An integer, a positive or negative whole number or zero (-25, 0, 31, 142322)... to define an int you use no markings except for the minus (-) sign if negative. Keep in mind that large numbers do not use commas.
decimal: A positive or negative number that can include digits after the decimal point (25.345, -13422.1, -5)... to define a decimal you use no markings except for the minus (-) and the letter "m" at the end.
double: This is a less-precise version of decimal... define like a decimal but use the letter "f" at the end.
bool: This is a boolean data type and defines true/false and nothing else.
DateTime: This is a data type that represents a Date and Time... to define a Date you set it by calling the DateTime(...) function and populating the appropriate parameters that are passed-in (year, month, day, hour, minute, second, etc.).
...the next one is a more advanced data type that is very useful and you will find yourself using it (or a variant) quite often:
List: This data type literally says what it stores (a list). The usage is to define the data type that will be stored within angle brackets () and then populate the list. Then, after populated, the list can be defined as an array of items (multiple instances of the same item) without having to define each variable individually.
The Examples
char middle_initial = 'J'; //notice that the character itself can be Uppercase? it can also be symbols, but they get complicated sometimes.
string my_character_name = "Aaron Jaxler";
int threat_level = 0;
decimal inventory_weight = 235.0m;
double character_weight = 185.0f;
bool is_equipped = false;
DateTime anniversary_date = new DateTime(2005, 5, 12); //this is just one option that defines it as May 12th, 2005.
List
equipment_items = new List
(); //then you have to use an _extension method_ to Add an item. equipment_items.Add("Pocket Watch"); equipment_items.Add("Bull Whip"); equipment_items.Add("Cast Iron Skillet");
In Conclusion
Well, that's today's lesson for all my C# Programmer wannabes! I hope it gives you a ton of information without being too difficult to follow. Again, if you have questions or wish me to dive deeper into these 2 components, let me know if the Comments Section and I'll jot down plans and get further examples.
Once you have a bit of a grasp on variables and data types you should really begin to understand just how important they are and how versatile. They are the components that hold the critical details of the program. Without them, you basically have a shell without anything inside. An empty shell is only worthwhile if it's been decorated to look beautiful so that it can subsist without substance.
Thanks everyone! See you next time!!
Leaving a comment since the resteem button is unavailable.
DISCLAIMER: dropahead Curation Team does not necessarily share opinions expressed in this article, but find author's effort and/or contribution deserves better reward and visibility.
to maximize your curation rewards!
with SteemConnect
Do the above and we'll have more STEEM POWER to give YOU bigger rewards next time!
News from dropahead: Bye bye 25+ and 50+! Welcome 20+ 40+ and 60+!
Congratulations! This post has been upvoted from the communal account, @minnowsupport, by dbzfan4awhile from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews/crimsonclad, and netuoso. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.
If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP. Be sure to leave at least 50SP undelegated on your account.