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().

After I hit run it gives me some feedback that I’m not understanding “Define a static void method called namePets with two string parameters”.

Methods are just not making sense to me for some reason.

using System;

namespace MethodOverloading
{
class Program
{
static void Main(string args)
{
namePets(“Timmy”, “Tommy”)

{
static void namePets(string petOne, string petTwo);
}

}
}

your first problem is that you DEFINED your NamePets method in the main method, it should be done outside (after the main method curly braces {} and only be CALLED in the main. Second, you dont HAVE to use PascalCase on methods (every first letter should be capitalize), but it would look better for anyone who might have to read your code, or even yourself once you will be used to coding. Third, you are not using the curly braces the right way: you start with an opened curly brace ({) then you define your method (static void namePets(string petOne, string petTwo)) then you add a semi colon ; wich has nothing to do there (it should be after the body of your method, here it is: Console.WriteLine($"Let\'s welcome our 2 new space guests: {param1} and {param2}!!"); and then add the closing curly brace (}). The correct way would be to declare your method, then open the curly braces, add the body where you add the semi colon at the end and then close the curly braces. Fourth you forgot the semi colon ; after calling your method in the main method. Finally, here is what your code should look like once you are done

using System;

namespace MethodOverloading
{
  class Program
  {
    static void Main(string[] args)
    {
      NamePets("Patate", "Pistache", "Pouf");
      NamePets("Patate", "Pistache");
      NamePets();
    }
    static void NamePets(string param1, string param2)
    {
      Console.WriteLine($"Let\'s welcome our 2 new space guests: {param1} and {param2}!!");
    }
    static void NamePets(string param1, string param2, string param3)
    {
      Console.WriteLine($"Let\'s welcome our 3 new space guests: {param1}, {param2} and {param3}!!");
    }
    static void NamePets()
    {
      Console.WriteLine("Aw, you have no spacefaring pets :(");
    }
  }
}

Pay attention to everything in this code, where are my curly braces ({}), where are my semi colons ; and it should help you to understand what you did wrong. this is complicated at first, but you’ll get used to it. Keep up the good work!

1 Like

Thank you. I’ll keep trying.

1 Like

None of this is making any sense anymore.

I don’t know if I’m just ■■■■■■■■ or what, but the instructions seem so bad. They’re asking you to do one thing, but what they ACTUALLY want you to do and the way they want you to do it is so different from what is written it’s ridiculous. Glad I’m getting a glimpse of the material provided by Codecademy before putting out any money. If this is the style of “learning” it’s not very good. I feel like I learn better just from watching youtube videos for free and following along with MVS.

1 Like

Let’s say I have a method NamePets with zero arguments and a method NamePets with two optional arguments. Which one will be executed when I call NamePets() and, moreover, why?

Hey, guys I just wanted to say thank you for such a supportive and helpful community that helps my beginning of learning c#!

lol I get mad because I feel like they aren’t explaining something or the console broken but then I realize I have something wrong with my code and my understanding. if you are frustrated take a step back take a break and come back to it with a fresh mind.

This lesson doesn’t make any sense to me. What they’re asking for and the Solution Code aren’t the same? And I don’t get why you can say pet1 and pet2 when you never specifically set those variables. I get that you call them in the method, but you don’t label them as “pet1” just “petNames” so I don’t get why it counts. I thought there was something wrong except I see people doing similar things in the comments. Really wish they explained this lesson better!!