#238 – Call a Method from Another Method

You can call an instance method of a class from another instance method within that class.  The second method called will execute upon the same instance of the class for which the first method was called.

In the example below, we can call the Dog.Growl method, or the Dog.BarkAndGrowl method which in turn calls Growl.

    public class Dog
    {
        public string Name;
        public int Age;

        public void BarkAndGrowl()
        {
            Console.WriteLine("Woof.  --{0}", Name);
            Growl();
        }

        public void Growl()
        {
            Console.WriteLine("Grrr.  --{0}", Name);
        }
    }

Here’s an example of calling these methods on an instance of the Dog class.

            Dog kirby = new Dog();
            kirby.Name = "Kirby";

            kirby.Growl();      // Grrr.
            kirby.BarkAndGrowl();  // Woof.  Grrr.
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: