FAQ: Method Calls and Input - Method Overloading

This community-built FAQ covers the " Method Overloading" exercise from the lesson “Method Calls and Input”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise _ Method Overloading_

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I have been askin my self how to override a already existing value. So like in the code below I want to override the value of NamePets(“Laika”,“Albert”); to something else without manually changing it. I tried calling the method again with another values but that only prints out the new value im looking to override it without manually changing the value. NamePets(“example”,“example2”);. Is this possible ?

using System;

namespace MethodOverloading
{
  class Program
  {
    static void Main(string[] args)
    {
      NamePets("Laika","Albert");
      NamePets("Mango","Puddy","Bucket");
      NamePets();
    } 
    static void NamePets(string myFirstPet, string mySecondPet) {
      Console.WriteLine($"Your pets {myFirstPet} and {mySecondPet} will be joining your voyage across space!");
    }

    static void NamePets(string myFirstPet, string mySecondPet, string myThirdPet) {
      Console.WriteLine($"Your pets {myFirstPet}, {mySecondPet}, and {myThirdPet} will be joining your voyage across space!");
    }
    
    static void NamePets() {
      Console.WriteLine("Aw, you have no spacefaring pets :(");
    }
  }
}

I inserted this code for exercise 4:

using System;

namespace MethodOverloading
{
  class Program
  {
    static void Main(string[] args)
    {
      Namepets("Thing One", "Thing Two");
      Namepets("Comedian", "Clown", "Circus");
      Namepets();
    }
    static void NamePets(string nameOne, string nameTwo)
    {
      Console.WriteLine("The newest members of your space zoo will be  " + nameOne + " and " + nameTwo + "!");
    }
    static void NamePets(string nameOne, string nameTwo, string nameThree)
    {
      Console.WriteLine($"The newest members of your space zoo will be {nameOne}, {nameTwo}, and {nameThree}!");
    }
    static void NamePets()
    {
      Console.WriteLine("Your space zoo has been attacked! It seems all your exhibitions have escaped or been freed!");
    }
  }
}

The Console responded with:

Program.cs(9,7): error CS0103: The name 'Namepets' does not exist in the current context [/home/ccuser/workspace/csharp-method-overloading/c-sharp-hello-world-run-some-c-sharp.csproj]
Program.cs(10,7): error CS0103: The name 'Namepets' does not exist in the current context [/home/ccuser/workspace/csharp-method-overloading/c-sharp-hello-world-run-some-c-sharp.csproj]
Program.cs(11,7): error CS0103: The name 'Namepets' does not exist in the current context [/home/ccuser/workspace/csharp-method-overloading/c-sharp-hello-world-run-some-c-sharp.csproj]

What is my error?

Check the spelling of the “NamePets” in lines 9-11.

2 Likes

you need to capitalise the P

2 Likes

I get an error message/ tip thing saying "Expected a Main method in Program.cs even though all of my code is inside of the Main method

Can you post your code? Click on the </> and copy-paste your code between the back ticks . (Maybe you’ve figured it out by now.)

Only your method calls should be inside Main(). Each method should be separately defined – apart from Main().