C# Programming for Beginners | C# Code for Creating C# Objects, Calling C# Methods

C# Code for Creating C# Objects, Calling C# Methods


This time we will look at how we can create c# objects, and use those objects to call the methods of the class.  You should have already created the Bird class according to the last part of this series.  If not go back and read that post here: C# Code for declaring C# classes, C# methods and properties.


To create the instance of your class (or object) you need to find the line: static void Main(string[] args)


This is where the program starts its execution.  To create your object you have to state the class name first then give the object a name and then use the “new” keyword like this:


//Create the separate Eagle and Chicken objects


      Bird Eagle = new Bird();


      Bird Chicken = new Bird();




After creating these objects you can refer to them by name.  Here we set the properties of the “Eagle” object:


//Set all the properties of Eagle object


       Eagle.pColour = "Brown";


       Eagle.pHeight = "90cm";


       Eagle.pWeight = "1100grams";


       Eagle.pPredator = true;




To output all the properties to the console window use the WriteLine() method of the Console class like so:


  
//Print all Eagle Properties to the console


   Console.WriteLine("EAGLES PROPERTIES:");


   Console.WriteLine(Eagle.pColour);


   Console.WriteLine(Eagle.pHeight);


   Console.WriteLine(Eagle.pWeight);


   Console.WriteLine("Predator? " + Eagle.pPredator.ToString());


   Eagle.Fly();


   Console.ReadLine();


To call the C# methods of the bird class, use the object name and a dot followed by the method name. 


That’s all it takes to call the method.  I’ve added a ReadLine() call as well – this is just a hack to stop the Console window from closing, so that you can see all the properties and the result of the method call.  I’ll leave you to try and create the Chicken object, using the above as the example.  Test your code using the green debugging arrow in Visual Studio.  The output should be as follows:


EAGLES PROPERTIES:
Brown

90cm
1100grams
Predator? True
Bird is flying




Here is the entire Program.cs class for your reference:

C# Source Code:


using System;


using System.Collections.Generic;


using System.Linq;


using System.Text;






namespace TestClasses


{


  class Program


   {


    public class Bird


    {


        //this is the constructor


        public Bird()


        {


        }


       


        private string colour;


        private string height;


        private string weight;


        private bool predator;


       


        //Properties


        public string pColour


        {


            get{return colour;}


            set{colour = value;}


        }






        public string pHeight


        {


            get { return height; }


            set { height = value; }


        }






        public string pWeight


        {


            get { return weight; }


            set { weight = value; }


        }






        public bool pPredator


        {


            get { return predator; }


            set { predator = value; }


        }






        //Methods - What can the Bird do?


        public void Fly()


        {


            Console.WriteLine("Bird is Flying");


        }






        public void Eat()


        {


            Console.WriteLine("Bird is Eating");


        }






        public void Walk()


        {


            Console.WriteLine("Bird is Walking");


        }


    }






static void Main(string[] args)


        {


         //Create the separate Eagle and Chicken objects


         Bird Eagle = new Bird();


         Bird Chicken = new Bird();






         //Set all the properties of Eagle object


         Eagle.pColour = "Brown";


         Eagle.pHeight = "90cm";


         Eagle.pWeight = "1100grams";


         Eagle.pPredator = true;






         //Set all the properties of Chicken object


         Chicken.pColour = "Yellow";


         Chicken.pHeight = "30cm";


         Chicken.pWeight = "200grams";


         Chicken.pPredator = false;






         //Print all Eagle Properties to the console


        Console.WriteLine("EAGLES PROPERTIES:");


        Console.WriteLine(Eagle.pColour);


        Console.WriteLine(Eagle.pHeight);


        Console.WriteLine(Eagle.pWeight);


        Console.WriteLine("Predator? " + Eagle.pPredator.ToString());






        Eagle.Fly();


  Eagle.Walk();


  Eagle.Eat();






        Console.ReadLine();


        }


    }






}




I hope you have enjoyed this programming tutorial on C# Programming for Absolute Beginners and I hope you have at least learned something about C# objects and C# methods.

0 comments: