Simon
Simon

Reputation: 1

C# help, overloading a method

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

Upvotes: 0

Views: 280

Answers (3)

MAO3J1m0Op
MAO3J1m0Op

Reputation: 421

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
    }

Upvotes: 1

Marty
Marty

Reputation: 545

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.

Upvotes: 0

Alexander Pope
Alexander Pope

Reputation: 1224

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.

Upvotes: 0

Related Questions