Variables
Wade Cantley
Variable Patterns
[type] [variable name] = [value];
Playing with numbers
An "int" is a number without decimals.
A "float" is a number with 7 digits and ends with the letter 'f'. No matter where the decimal is, the number of digits will only be 7. So 102.4567 or 10.12345 will work. As you can see, you're limited to 999999.9 being the largest decimal number. This saves memory but is also difficult to use as most functions take in and work with doubles.
A "double" is a number with a decimal of up to 14 decimals of accuracy. Anything more than 14 decimals is rounded up. so 1111.1234567891234567 would be rounded to 1111.12345678912346
int skittles = 100; float exactNumberOfSprinkles = 107.4567f; double exactNumberOfSlices = 4.45678945678945;
Playing with Chars & Strings
A "char" type will only pay attention to the last character in a string. In the case below, there are only single characters in the strings presented and when output, they need to be converted to a string.
char twizlerColor = 'r'; char twizlerNumber = '2'; char twizlerLit = '*'; lblLabel1.Text = candleColor.ToString(); lblLabel2.Text = candleNumber.ToString(); lblLabel3.Text = candleLit.ToString();
Note : If we wanted to put more characters into a variable, they would need to go into a character array.
A "string" can contain any kinds of characters and acts as its own "unit" of memory. This is in contrast to a character array, where each character in the array is its own unit of memory.
string sugarySongToSing = "I T@ste the ra1nbow, & it tastes G00d!";
Typecasting
Each type is its own class and comes with different options for transforming a type.
Setup to have 100 skittles. And then we are going to show the number of skittles in a text string.
int skittles = 100; lblLabel.Text = skittles.ToString();
When adding doubles and floats, use doubles as the target variable. Floats will only handle calculations between floats.
GOOD : double = double + float
BAD : float = double + float
Fact : most functions take in doubles because they are more flexible and provide high-level of accuracy. Floats require less memory space but require casting when using with other types.