Calling a method within a class from Main() - What am I doing wrong?

I'm in the process of learning C# by writing a simple program that converts Celsius to Fahrenheit and vice-versa. My current road block is calling the conversion methods from a class I created which is separate from Main(). For some reason, VC# cannot see the methods with the code I am using to make "the call." I am not certain if the problem lies with the code used to place the call or the way I've written the class itself. Below is the code I have written.

First - The Class that contains the conversion methods...

using System;

namespace Convert
{
public class Convert
{
public float UserFahren;
public float UserCelsius;
public float ConversionFahren;
public float ConversionCelsius;

float CalcCelsius(float UserValue)
{
// Formula to convert Fahrenheit to Celsius - °C = 5/9(°F-32)
UserFahren = UserValue;
ConversionCelsius = (UserFahren - 32) * 5 / 9;
return ConversionCelsius;
}

public float CalcFahren(float UserValue)
{
// Formula to convert Celsius to Fahrenheit - °F = 9/5°C + 32
UserCelsius = UserValue;
ConversionFahren = UserCelsius * 9 / 5 + 32;
return ConversionFahren;
}
}
}

Next - The code I am attempting to use to "call" the methods from Main()

using System;
using Convert;


namespace Program
{
class Program
{
public static string UserInput;
public static int UserChoice;
public static float UserTemp;

static void Main()
{
if (UserChoice == 1)
{
...
}

if (UserChoice == 2)
{
Console.Write("Please input the Fahrenheit temperature you wish to be converted: ");
UserTemp = float.Parse(Console.ReadLine());
Convert.CalcCelsius(UserTemp)); //Code "calling" the conversion methods
Console.WriteLine("Your converted Temperature is {0} approximately degrees Celsius \n", UserTemp);
Console.ReadLine(); // Used to keep console
}
}
}

When I compile the code, the debugger gives me the following error on the Bolded portion of my code.

Error 1 The type or namespace name 'CalcCelsius' does not exist in the namespace 'Convert' (are you missing an assembly reference )


Note: I realize that someone (i.e. Microsoft) has probably already written a class for this purpose but I am trying to learn OOP methodologies. My first attempt resulted in a program that did EVERYTHING in Main(). :D Please be gentle.


Answer this question

Calling a method within a class from Main() - What am I doing wrong?

  • Morely

    Hello All.

    Robert:

    The reason that you're getting this error is that, because you have used the same identifier for the namespace and the class, you've introduced ambiguity into the scope resolution.

    When you use the scope reference Convert. , the compiler starts at the namespace level to resolve the reference. Even though you have a "using Convert;" directive, explicit namespace references are still allowed, so the compiler thinks that you're referencing the namespace Convert, not the class Convert, because you are.

    With the current naming scheme, to reference the class Convert, you would have to use Convert.Convert.CalcCelsius . You see, very unwieldy. Don't use the same name for the namespace and the class.

    Another problem is that you have defined CalcCelsius as an instance method, but you're using it as a static method. To use an instance method, you have to have an instance -- in other words, an object -- of the Convert class. You create an object of that class with the "new" keyword, like so:

    Convert varName = new Convert();

    Then, to call the method, you use:

    varName.CalcCelsius(UserTemp);

    Alternatively, you can define the CalcCelsius() method as "static", and use it with the class reference, like you have done in your Main() method.

    HTH.



  • NuTech

    Roland and Mark,

    Thank you very much for your responses. I wish I could flag both of your responses as answers because you were both correct. I chose to make the methods static since it made the most sense to me and seemed to yield results the quickest. I've also clarified the ambiguous namespace and class names I used as well as moved the variable declarations into their respective methods. Now that you have helped me sort these issues out I will be able to expand on the construction of my conversion class to include additional US to "the rest of the world" conversions. Ultimately, this program will end up in the hands of my most important customer, my wife. :D Thanks again for your responses.

    Cheers!


    - Robert


  • fawltster

    Hi Robert!

    1st: The method CalcCelcius has to be public.
    2nd: Both (CalcCelcius and CalcFahren) are instance methods. So you have to instanciate a variable of type Convert to call the methods of this instance

    Convert converter=new Convert();
    converter.CalcCelcius(UserTemp)

    But in this particular case I think it would be a better idea to implement the two methods to be static so you may call

    Convert.CalcCelcius(UserTemp);

    as you already did.
    Therefor you have to change the variable definitions to static too. Or you simply put the variable definition inside the methods (which would be a better style because the information held in the variables is only related locally to the method and not to the class).

    greets,
    Roland


  • Calling a method within a class from Main() - What am I doing wrong?