Saturday, October 31, 2015

C# what is a class

C# has got too much going on and you do forget whats a class, or object, or method or function...so what were we learning, yes the "class".

Let me put it as simply as possible.


Even after that mall example you didn't get it, you a thick head, follow the example below:

Lets take a look at our SimpleCalculator Example;

The class code;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class SimpleCalculator
    {

        public int add(int x, int y)
        {
            Console.Write("\nThe addition is ");
            return x + y;

        }


        public int mul(int x, int y)
        {
            Console.Write("\nThe multiplication is ");
            return x * y;

        }
    }
}
Have a look at the image below in fullscreen to know what is what.


C# how to create a simple calculator based on user input

We've seen all those tutorials about calculators n all which show how to add and stuff, example;
int x = 4;
int y = 5;
int result = x + y;
Console.WriteLine("Total Sum is : {0}", result);  
Now above code is like really basic and to be honest kinda stupid cuz common who really embeds their sum in a program, what you want is that the user enters what two numbers he/she wanna add/multiply and then the result is displayed on the console. Thats cool innit ?

So fireup your visual studio create a new console application, if you dunno how to read,
How to create new console app. in C#.

1. Ask for user input whether he/she wants to add or multiply, and store input in "choice
Console.Write("Enter 1 for addition, 2 for multiplication: ");
int choice = int.Parse(Console.ReadLine()); 
2. Now make a new class called SimpleCalculator which will hold our add and multiply methods.

class SimpleCalculator{

}
3. Now lets make two methods for add and multiply

 public int add(int x, int y)
        {
            Console.Write("\nThe addition is ");
            return x + y;

        }
  public int mul(int x, int y)
        {
            Console.Write("\nThe multiplication is ");
            return x * y;

        }

4. Full code for main method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {



            Console.Write("Enter 1 for addition, 2 for multiplication: ");
            int choice = int.Parse(Console.ReadLine());


            if (choice == 1)
            {
                //addition
                Console.WriteLine("Welcome to Addition...");
                Console.WriteLine("\nEnter first number: ");
                int num1 = int.Parse(Console.ReadLine());
                Console.WriteLine("\nEnter second number: ");
                int num2 = int.Parse(Console.ReadLine());
                SimpleCalculator AddCalc = new SimpleCalculator();
                Console.WriteLine(AddCalc.add(num1, num2));

            }
            else if (choice == 2)
            {
                Console.WriteLine("Welcome to Multiplication...");
                Console.WriteLine("\nEnter first number: ");
                int num1 = int.Parse(Console.ReadLine());
                Console.WriteLine("\nEnter second number: ");
                int num2 = int.Parse(Console.ReadLine());
                SimpleCalculator Mul = new SimpleCalculator();
                Console.WriteLine(Mul.mul(num1, num2));
            }





            Console.ReadLine();

        }
    }
}
5. Full code for SimpleCalculator class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class SimpleCalculator
    {

        public int add(int x, int y)
        {
            Console.Write("\nThe addition is ");
            return x + y;

        }


        public int mul(int x, int y)
        {
            Console.Write("\nThe multiplication is ");
            return x * y;

        }
    }
}

C# how to create a new console application in Visual Studio

I know ya'll wanted to learn C# to build all those boss looking windows forms applications (the ones with a gui) btw gui stands for guided user interface. But for the learning of basics a console application is whats needed. Don't ask for  a reason thats how its been since ages.




They first invented the wheel didn't they...


So how would you make a console application, its simple follow me homie;

 Open up visual studio and press ctrl+shift+n to bring the new project dialog box



Thats it a new console application just got created...