C# help, overloading a method

what is method overriding in c#
method overloading
method overloading in c# msdn
method overloading in c# interview questions
operator overloading in c#
method overloading with different return type in c#
method overloading and overriding in c#
why we use method overloading in c#

I have one last thing to add to my assignment before im finished.

This is a part of my code:

static decimal FahrToCels(int fahrenheit) //Metod för konvertering av Fahrenheit(F) till Celsius(C)
{
    decimal celsius = (decimal)((fahrenheit - 32) * 5) / 9; // Matematisk uträkning för F till C. 
    return Math.Round (celsius, 1); //Lagrar ett decimal tal avrundat till 1 decimal och lagrar i celsius
}

static void Main(string[] args)
{
    Console.Write("Vänligen ange temperatur till bastun och tryck enter: "); //skriver ut meddelande

    do
        int fahr = int.Parse(Console.ReadLine()); // Omvandlar string och lagrar användarens inmatning i en int fahrenheit
        decimal celsius = FahrToCels(fahr); // Metoden FahrToCels konverterar inmatad temperatur till celsius och lagrar i decimal celsius

As can be seen, ive created a method, that is later used after the user is told to enter degrees in fahrenheit. The method converts the entered number to celsius.

Now the last thing im told to do is by overloading the method, make it possible for the user to enter zero(0) and by doing that randomly generate a number before that number goes into the fahrenheit to celsius converting method. Im guessing the generated numbet has to be like between 140-195 because the user needs to enter zero until the generated number equals to 73-77 after converting to celsius!

I know how to generate a random number, and i think i understand what overloading does, but im totally lost on how to do this one...

An idea would be to create a function under (or over) the method inside the class that takes no arguments. Nothing else special is required. When you want to call FahrToCels(), you have the option to call either method based on the type and quantity of the arguments.

    static decimal FahrToCels ()
    {
        // Your code here
    }

C# Method Overloading, Method Overloading. With method overloading, multiple methods can have the same name with different parameters: Example. int MyMethod  1. An idea would be to create a function under (or over) the method inside the class that takes no arguments. Nothing else special is required. When you want to call FahrToCels (), you have the option to call either method based on the type and quantity of the arguments.

Create a new method like this

    static decimal FahrToCels(string value) //note value is of type 'string'
    {
        //your implementation goes here, check if value is 'zero'
    }

This solves your requirement to use method overloading, event though I find it a bit odd.

Overloading, In each example, I'll give a short program which will declare some methods and call one - and then I'll explain what gets called in which version of C#, and why. With method overloading, multiple methods can have the same name with different parameters: Example int MyMethod(int x) float MyMethod(float x) double MyMethod(double x, double y)

Apart from other possible concerns: Overloading a method has nothing to do with specific parameter values (unless you are using different types, e.g. short, int, long). In your case: "if the parameter value is 0 then return a random number" is not something solvable by overloading.

Now after reading your comment on the question; you could create a method that doesn't take any parameters static decimal FahrToCels() and call that in case you read a 0 from the input. This new method would then generate a random value and convert that.

Personal opinion: I'm not gonna comment on how reasonable that assignment is. The more standard case would be to use an if statement to decide if the input was 0 and if so generate a random value and pass that to the method you already have. But I might be missing something here.

Method Overloading In C#, C# method overloading allows a class to declare multiple methods with Method overloading is an example of the polymorphism feature of an  What is Method Overloading in C#? When you want to implement the feature of polymorphism, it is done with the help of Method Overloading. In a layman term, if we want to use one function for more than one form, we use method overloading. Since C# is OOPs based language, it completely supports polymorphism.

Method overloading, Method overloading. A lot of programming languages support a technique called default/optional parameters. It allows the programmer to make one or several  In C#, we can overload constructors, methods and index properties since they have parameters defined in their signature. C# Method overloading: In a class, if we defined two methods with the same name but different signature then it’s called method overloading.

Member Overloading, For example, in the following, the WriteLine method is overloaded: C# Copy. public static class Console { public void WriteLine(); public void  What is Method Overloading or Function Overloading in C#? It is a process of creating multiple methods in a class with the same name but with a different signature. In C#, It is also possible to overload the methods in the derived classes, it means, it allows us to create a method in the derived class with the same name as the method name defined in the base class.

What is method overloading in C#?, Two or more than two methods having the same name but different parameters is what we call method overloading in C#. Method overloading in C# can be performed by changing the number of arguments and the data type of the arguments. C# method overloading allows a class to declare multiple methods with the same name, separated by their signatures. This article explains method overloading and method overriding in C# with examples.

Comments
  • float is a better choice than int or double for continuous quantities like temperature. Also, please give us an example of what "to enter zero" means. You know that zero is a perfectly valid temperature, right?
  • @DourHighArch This is a school assignment and i find it a little odd. My code works perfect and does what it is supposed to. But the last thing the assignment wants me to to is " The user should also be able to enter a zero(0) to randomly generate a temperature. If this happens, no value should be sent to the method, this can be solved by method overloading" So I guess if the user enters the number zero, a random temperature should be generated. The rest of my code consists of some if and else to get the user to enter the correct temperature.
  • @DourHighArch The reason for the int variable is because the assignment said it should be stored as an int first. For some reason.
  • Even if im a newbie in programming, im aware that the assignment is strange.... Yes, making a method that doesnt take any parameters is what it tells me I could do. But how do i create this? I know how to generate random numbers, but how do i call for the method when the user enters 0? Because as of now when entering zero, is converts to -17,8 degree celsius. If i were to use if-statements, where would i put it? Because of now it automatically uses the convertion method when entering any given number
  • you would need an if stament (that's why I was questioning the assignment). so it would be something like: if(fahr == 0) { <call method without params> } else { <call the regular method> }
  • Yes that seems like a solution. But how would I first make it generate the random number that then continues to the convering method? I need to make a new method, that has a number generator and the converter in it I guess?
  • One possibility would be: generate the random number (either with or without an extra method) and use it as a parameter for calling the other FahrToCels method. Then return what you get from that.