Sunday, August 25, 2013

30

/*In programming, every character space in a string is a position, but we refer to that position as index beginning at zero*/
static void Main(string[] args)
{
  string foo = "Hello World";
  int index = foo.IndexOf('e');
  Console.WriteLine(index);
}
/*This returns '1' because the index of 'e' is the 2nd position in the string*/
int index = foo.IndexOf('el');
/*would return the same index because C# will look for the beginning of the string 'el'*/
int index = foo.IndexOf('e ');
/*would return -1 because there is no 'e' followed immediately by ' '*/

/*Find the second L
string foo = "Hello World";
int index = foo.IndexOf('L' foo.IndexOf("l")+1);
Console.WriteLine(index);

/*Find the third L
string foo = "Hello World";
int index = foo.IndexOf('L' foo.IndexOf("l")+2);
Console.WriteLine(index);
/*Or
string foo = "Hello World";
int index = foo.LastIndexOf('L');
Console.WriteLine(index);

Substring
string foo = "Hello World";
string bar = foo.Substring(7, 3);

Console.WriteLine(bar);
//Will output "orl"

Substring
string foo = "Hello World";
string bar = foo.Remove(0, 3);
string bar1 = bar.Remove(2, 3);
string bar2 = bar1.Remove(3, 1);

Console.WriteLine(bar2);
//This will output lord


Substring
string foo = "Hello World";
string bar = foo.Replace("Hello ", string.Empty);

Console.WriteLine(bar);
/*Will output Hello

string foo = "Hello World";
string bar = foo.Replace("o", "a");
string bar1 = bar.Replace("arl", "eir");
Console.WriteLine(bar1);
/*Will output Hella Weird












Sunday, August 4, 2013

While/Do Loops

This is very very very important to understand if you want to make games.


int[] intArray = {3, 5, 2, 6};

int count = 0;

while (count < intArray.Length) 
       { 
       Console.WriteLine("index " + count + " = " + intArray[count]);//See how I passed that into the parameter?
       count++;
       }
            Console.WriteLine("End Program");

            Console.Read();

NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
            string action = "Quit";
            
            //Note that these are just options display or instructions and the actions are called in the switch statements
     Console.WriteLine("What would you like to do?");
     Console.WriteLine("1 - Attack?");
     Console.WriteLine("2 - Run?");
     Console.WriteLine("3 - Defend?");
     Console.WriteLine("4 - Quit?");


     do     
     {
         action = (Console.ReadLine());
         switch (action)
            { 
                case "attack":
                    Console.WriteLine("You Attacked!");
                    break;
                case "defend":
                    Console.WriteLine("You Defended!");
                    break;
                case "run":
                    Console.WriteLine("You Ran!");
                    break;
                case "quit":
                    Console.WriteLine("Good bye!");
                    break;
                default:
                    Console.WriteLine("Invalid input");
                    break;
            }
       } while (action != "Quit");
// do will check at this point to see if the program should continue or if it should end.

Saturday, August 3, 2013

Case Swiitch

******************With numbers ******************

int number;//You need this or nothing will work

            Console.WriteLine("What would you like to do?");
            Console.WriteLine("1 - Attack?");
            Console.WriteLine("2 - Run?");
            Console.WriteLine("3 - Defend?");


            number = Convert.ToInt32(Console.ReadLine());

            switch (number)
            { 
                case 1:
                    Console.WriteLine("You Attacked!");
                    break;//kicks you out of this case
                case 2:
                    Console.WriteLine("You Defended!");
                    break;
                case 3:
                    Console.WriteLine("You Ran!");
                    break;
                default:
                    Console.WriteLine("That is not a valid input");
                    break;
            }

*

Switch Statements are better to use if you know that there are going to be a limited number of answers

Nested

bool tralse false;
bool fruetrue;


if (tralse) {
    I would like this statement to run;
    // Too bad: the condition is false and so this statement wiln't run. 
    if (frue) {
        I would like this statement to run as well;
    // Too bad: the initial condition is false and so compiler immediately turns it's snout up at this nested if statement
}
}

When nesting for loops you have to use different names for the incrementer


for(int x = 1; x < 1000: x *= 2)
   {
   for(int y = 1; y < 1000: y *= 2)//Not the best example
      {
         if (y == 8 || y == 32)
         {
            continue;
         }  
       Console.WriteLine("etcétera bla bla bla"); 
      }
    if (y == 8 || y == 32)
    {
       continue;
    }  
    Console.WriteLine("etcétera bla bla bla"); 
   }  

Monday, July 29, 2013

The Convert Class

What's your name?
            string name;
     Console.WriteLine("What is your name");
     name = Console.ReadLine();// This will create the user input at enter
     Console.WriteLine("Hello " + name);
     Console.Read();

What's your number- ..hold up I gotta convert the string
            int celly;
     int homePhone;

     Console.WriteLine("What is your cell number?");
     celly = Convert.ToInt32(Console.ReadLine());// the readline is for text input, so any integers need to be converted.

     Console.WriteLine("What is your home number?");
     homePhone = Convert.ToInt32(Console.ReadLine());

     Console.WriteLine("Your cell number is " + celly + " and your home number is " + homePhone);
     Console.WriteLine("These numbers added together is " + (celly + homePhone));
// note "+ (celly + homePhone)" in parentheses  If this were done without parentheses, this would simply concatenate the variables to the writeline string instead of performing the math operation.
     Console.Read();

Double
     double myDoubleVar;
     myDoubleVar Convert.ToDouble(Console.ReadLine());

Floats
     double myFloatVar;
     myFloatVar (float)Convert.ToDouble(Console.ReadLine());

Math

Modulus is useful in that you can keep things from going over a certain threshold 

            const int maxDegrees = 360;
            int angle = 365; //Doesn't exist on a 2D scale
            int finalOutput;

            finalOutput = angle % maxDegrees;
//Instead of continuing past the set threshold, the value will loop clocklike

            Console.WriteLine("Angle is equal to " + finalOutput);
            Console.Read();

        
This will echo back 5 instead of 365

C# will truncate any decimals values. If you want to round off, use:


int x = 3; Math.Round //Simple Round
int x = 3; Math.Floor //Round down
int x = 3; Math.Ceiling  //Round Up

Typecasting:


int x = 10;
float y = 3;
______________________________________________________________
finalValue = x / y; //This doesn't work the way you'd want it to because C# will upgrade the integer to float.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

finalValue = x / (int)y; //This will temporarily unfloat the Y value

or 

finalValue = (int)(x / y); //This also "works" kind of, but the returned value will be a little off.

Friday, July 19, 2013

Arrays

Declare you datatype with a set of braces affixed  - int[] x;
Then allocate the array  - int[] x = new int[3];
Assign the values  - 
int[] x = new int[3];
x[0] = 1;
x[1] = 2;
x[2] = 3;
x[2] = 3;//Try to add this one & it will crash

or

int[] x = new int[3] {1, 2, 3};

or just

int[] x = {1, 2, 3};

Access the elements

System.Console.WriteLine(x[1] * x[2]); // will output 6
----------------------------------------------------------------------------------------------------

There are two kinds of multidimensional arrays:

Rektangular                                                                                                                        string[,] x = new string[2, 2];
x[0, 0] = "00"; x[0, 1] = "01"; 
x[1, 0] = "10"; x[1, 1] = "11"; 

or

string[,] x = { { "00", "01" },
                      { "10", "11" }  };


Jagged

string[][] a = new array[2][];
a[0] = new array[1]; a[0][0] = "00";
a[1] = new array[2]; a[1][0] = "10"; a[1][1] = "11";

or


string[][] a = { new string[]  { "00", };

                 new string[]  { "00" , "00", }  };

#################################################################



















Strings

String Methods

string a "String";

  • Replace("i", "o");     //Strong
  • Insert(0, "Ham");     //HamString
  • Remove(0, 3);        //ing
  • Substring(0, 3);      //Str
  • ToUpper();            //STRING
  • Length;                  //6


Increments

x++; //adds one to the value of x and stores it as it's new value.

++x; //this will return the value after it has been changed

x++; //this will return the original value before changing the value

Escape slashes

\n - Newline
\t - Horizontal Tab
\v - Vertical Tab
\b - Backspace
\r - Carriage Return
\0 - Null Character
\f - Form Feed
\' - Single Quote
\a - Alert Sound
\" - Double Quote
\\ - BackSlash
\uFFFF Unicode Character

To escape the escape string add @:
string mV = @"c\\windows\system64\myvirus.exe";

Operators

Arithmetic


  • +    -    *    /    %

Assignment


  • =    +=    -=    /=    %=    ++    --    &=    ^=    |=    <<=    >>=

Comparison


  • ==    !=    >    <    >=    <=

Logical


  • &&    ||    !

Bitwise


  • &   |   ^   <<   >>   ~
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

x = 3 / (float)2; //to get the exact answer, one of the numbers needs to be converted to a floating point


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Some operators take precedence over others. In order to decide in which order our statements will run, we can use parentheses.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Take string variable A, and concaffeinate it with a new string, then store that in string variable B:
a = "Hella";
b = a + "Word";

Append the new string to the original value of A and store it as the new concatenated A value:
a = "Hella";
a += "Word";















Variables

Variable types must be declared:

Signed 

  • sbyte    -128 to +127
  • short    -32768 to +32767
  • int        - 2^31 to +2^31-1
  • long     -2^63 to +2^63-1

UnSigned 

  • byte       -  0 to 255
  • ushort    - 0 1o 65535
  • uint        - 0 to 2^32-1
  • ulong     - 0 to 2^64-1
  • int         - hexadecimal (base 16)

Floating point

  • float
  • double
  • decimal

Char

  • char

Boolean

  • bool

String

  • string

###########################################################################

Variables of the same type can be declared sequentially, when separated by commas:
int myInt1 = 10, myInt1 = 101, myInt1 = 500000, myInt1 = 8,


int can be any kind of number except a floating point
String FirstString = "";//<--This is the identifier = "";
String SecondString = "";//I could allocate now if I wanted
//Or I can allocate data to the string var later in the program like this
FirstString = "Lorem";
SecondString = "Ipsom":

In the case of a constant: it must be initializedConst String FirstString = "Sit Amet";
This is helpful in a large program so you don't accidentally change something that shouldn't be changed.

Float DeciNumber = 3.14f;//You need an F here
Double can hold more memory and is like a bigger version of float
Double DeciNumber = 3553435.34531112;//You dont need an F here


Boolean //Chicken Flavored


bool Tralse = false;

if (Tralse) {
    run this statement;
} //This statement will not run because because it is false. If tralse were set to true it would run.

bool Tralse;
Tralse = (42 < 48);//You can also put a series of conditions in here joined by logical operators
if (Tralse) {
    run this statement;
//This statement will run because because it is true.

Nomenclature

{
      I am delimited by curly braces
}
***************************************************************
System.Console.Write("Hella World");
//This brings the class System.Console into scope to output console text

System.Console.WriteLine("Hella World");
//The addition of Line will cause a line break after the output.
***************************************************************

Tuesday, July 16, 2013

Namespaces and Assemblies

It is best to always define your classes in a namespace, that way if other programmers decide to use your assembly in their program and they use a class with the same name as one of your classes, they can still coexist under different namespaces.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Etcetera;
using System.BlaBlaBla;

When invoking a template, Using Directives are automatically added, bringing certain classes into the scope of your program.

Here is what a generic console project will look like:

namespace TheNameOfMyProgram
{//All this is the of the namespace of my program
    class Program
    {//I am creating a class called program that is delimited in these curly braces.
        static void Main(string[] args)
        {//This is the main function & entry point

        }
       }
}


Assemblies are referenced in the the references folder of the solution explorer. You can add and delete them by R+Clicking.