okr okr - 2 months ago 7
C# Question

Calling a class from Windows form application

I'm learning Windows Forms and I'm stuck when working with Windows Forms and Classes. I want to make a form in which a user can enter the temperature in Farenheit then click a "convert" button and this will take the input value to a "convert" class where it gets converted to Celsius then have both Farenheit and Celsius displayed on a message box.

I have designed the form and I know how to create the class. What I don't know is how to take the input value (farenheit) from the form to a class then calling the converted value from the form and display it in a message box.

I'm a beginner in C# so I'd appreciate your comprehension and beginner-like answers. Thank you!

Answer

You need to create method

public class Convertor
{
 public datatype FarenheitToCelsius(String value)
 {
  datatype celsius;
  ...conversion logic   
  return celsius;
 }
}

Then you need to call method of class

public class form1
{
   public void button_click(arguments...)
   {
       Convertor c = new Convertor();
       MessageBox.Show(c.FarenheitToCelsius(textbox1.text));
   }
}

Note: this is just a partial example