Your Hours
method does not return anything. It must return something and since it can be a fraction, I suggest to return double
// My ClassName with method Hours()
public static double Hours() { //return double here
var citiesDistance = new Dictionary<string, int> {
{"Place1",10},
{"Place2",20},
{"Place3",30},
};
var cities = "Place1";
double length = citiesDistance[cities]; //use double
double speed = 100; //use double
double hours = length / speed; //use double
return hours; //note that it is returned
}
And then in your main form
// Form1. The window form
private void button1_Click(object sender, EventArgs e)
{
double hours = ClassName.Hours();
//Do something with hours, example:
MessageBox.Show(hours.ToString("f3")); //"f3" is to limit the number of fraction digits (this case being 3) it prints. Change 3 to any number you want
}
You can get what you want. To convert it to string, simply do hours.ToString()
Edit:
If you have user input (which is a comboBox), you should do it like this
// My ClassName with method Hours()
public static double Hours(string cities) { //return double here, note the user input
var citiesDistance = new Dictionary<string, int> {
{"Place1",10},
{"Place2",20},
{"Place3",30},
};
//var cities = "Place1"; //Note that this is commented now
double length = citiesDistance[cities]; //use double
double speed = 100; //use double
double hours = length / speed; //use double
return hours; //note that it is returned
}
And when you call it, you call it like this
// Form1. The window form
private void button1_Click(object sender, EventArgs e)
{
if (comboBox.SelectedIndex >= 0){ //to prevent non-selection to give crash
double hours = ClassName.Hours(comboBox.SelectedItem.ToString());
//Do something with hours, example:
MessageBox.Show(hours.ToString("f3")); //"f3" is to limit the number of fraction digits (this case being 3) it prints. Change 3 to any number you want
}
}