So first, I am very new to programming and this is my first time posting so sorry in advance!
So I'm posting my updated code below and removed the previous information I posted to avoid confusion about what I'm asking since I realize now that I originally asked the wrong question.
I need the ComboBox
selection to be accessible by the PostBtn_Click
. Once I click the button, I need to take the value selected, and use it within my CalculateWindChill()
method within my Forecast
class. Most of my attempts have resulted in an error telling me that the name does not exist in the current context.
private void PostBtn_Click(object sender, EventArgs e)
{
Forecast currentForecast = new Forecast();
{
predicted_LoTemp =
Convert.ToInt32(PredictedLoTemp.SelectedItem);
Wind_Speed = Convert.ToInt32(WindSpeed.SelectedItem);
HiTempLbl.Text =
Convert.ToString(PredictedHiTemp.SelectedItem);
LoTempLbl.Text =
Convert.ToString(PredictedLoTemp.SelectedItem);
WindSpeedLbl.Text =
Convert.ToString(WindSpeed.SelectedItem);
WindChillLbl.Text =
Convert.ToString(currentForecast.CalculateWindChill());
}
}
public class Forecast
{
public int Predicted_LoTemp { get; set; }
public int Wind_Speed { get; set; }
//Formula for calculating wind chill
//Currently "Predicted_LoTemp" and "Wind_Speed" do not pull
//a value from the combobox as needed
public double CalculateWindChill()
{
double WindChill = 35.74 0.6215 *
Convert.ToDouble(Predicted_LoTemp) - 35.75 *
Math.Pow(Convert.ToDouble(Wind_Speed), 0.16)
0.4275 * Convert.ToDouble(Predicted_LoTemp) *
Math.Pow(Convert.ToDouble(Wind_Speed), 0.16);
return WindChill;
}
}
CodePudding user response:
Ok, so I finally got it to work with the following:
private void PostBtn_Click(object sender, EventArgs e)
{
int LoTempSelection = Convert.ToInt32(PredictedLoTemp.SelectedItem);
int WindSpeedSelection = Convert.ToInt32(WindSpeed.SelectedItem);
Forecast currentForecast = new Forecast( LoTempSelection,
WindSpeedSelection);
{
HiTempLbl.Text = Convert.ToString(PredictedHiTemp.SelectedItem);
LoTempLbl.Text = Convert.ToString(PredictedLoTemp.SelectedItem);
WindSpeedLbl.Text = Convert.ToString(WindSpeed.SelectedItem);
WindChillLbl.Text =
Convert.ToString(currentForecast.CalculateWindChill());
}
}
public class Forecast
{
public int Predicted_LoTemp { get; set; }
public int Wind_Speed { get; set; }
public Forecast(int LoTempSelection, int WindSpeedSelection)
{
Predicted_LoTemp = LoTempSelection;
Wind_Speed = WindSpeedSelection;
}
CodePudding user response:
You should just make those Forecast
properties regular auto-properties:
public class Forecast
{
public int PredictedLoTemp { get; set; }
public int WindSpeed { get; set; }
public void DoSomething()
{
var product = PredictedLoTemp * WindSpeed;
// ...
}
// ...
}
You can then set them based on the ComboBoxes
when you create the Forecast
object:
var currentForecast = new Forecast
{
PredictedLoTemp = (int)PredictedLoTemp.SelectedItem,
WindSpeed = (int)WindSpeed.SelectedItem
};
currentForecast.DoSomething();
You then use those same properties within the Forecast
class.
EDIT:
Addressing your updated question. The issue is that you're not actually setting the properties anywhere. I provided you with an example above but you didn't follow it. Firstly, this is weird:
Forecast currentForecast = new Forecast();
{
predicted_LoTemp =
Convert.ToInt32(PredictedLoTemp.SelectedItem);
Wind_Speed = Convert.ToInt32(WindSpeed.SelectedItem);
HiTempLbl.Text =
Convert.ToString(PredictedHiTemp.SelectedItem);
LoTempLbl.Text =
Convert.ToString(PredictedLoTemp.SelectedItem);
WindSpeedLbl.Text =
Convert.ToString(WindSpeed.SelectedItem);
WindChillLbl.Text =
Convert.ToString(currentForecast.CalculateWindChill());
}
I don't know why you have those braces there but I think that you might be confused about how object initialisers work. Those braces are completely pointless and that code should be written like this:
Forecast currentForecast = new Forecast();
predicted_LoTemp = Convert.ToInt32(PredictedLoTemp.SelectedItem);
Wind_Speed = Convert.ToInt32(WindSpeed.SelectedItem);
HiTempLbl.Text = Convert.ToString(PredictedHiTemp.SelectedItem);
LoTempLbl.Text = Convert.ToString(PredictedLoTemp.SelectedItem);
WindSpeedLbl.Text = Convert.ToString(WindSpeed.SelectedItem);
WindChillLbl.Text = Convert.ToString(currentForecast.CalculateWindChill());
As you can see, you're not setting the properties of the Forecast
object anywhere there so the data is not getting into the object to use. If you had followed the example I provided then you'd be doing this:
var currentForecast = new Forecast
{
Predicted_LoTemp = (int)PredictedLoTemp.SelectedItem,
Wind_Speed = (int)WindSpeed.SelectedItem
};
predicted_LoTemp = Convert.ToInt32(PredictedLoTemp.SelectedItem);
Wind_Speed = Convert.ToInt32(WindSpeed.SelectedItem);
HiTempLbl.Text = Convert.ToString(PredictedHiTemp.SelectedItem);
LoTempLbl.Text = Convert.ToString(PredictedLoTemp.SelectedItem);
WindSpeedLbl.Text = Convert.ToString(WindSpeed.SelectedItem);
WindChillLbl.Text = Convert.ToString(currentForecast.CalculateWindChill());
Notice that, in this case, there is no semicolon after the Forecast
constructor so the braces are actually part of the object initialisation. That syntax is literally called an object initialiser. That code is equivalent to this:
var currentForecast = new Forecast();
currentForecast.Predicted_LoTemp = (int)PredictedLoTemp.SelectedItem;
currentForecast.Wind_Speed = (int)WindSpeed.SelectedItem;
predicted_LoTemp = Convert.ToInt32(PredictedLoTemp.SelectedItem);
Wind_Speed = Convert.ToInt32(WindSpeed.SelectedItem);
HiTempLbl.Text = Convert.ToString(PredictedHiTemp.SelectedItem);
LoTempLbl.Text = Convert.ToString(PredictedLoTemp.SelectedItem);
WindSpeedLbl.Text = Convert.ToString(WindSpeed.SelectedItem);
WindChillLbl.Text = Convert.ToString(currentForecast.CalculateWindChill());
Now you're actually setting the properties before using them.
The one other change is that you appear to want to treat the properties as double
values, so you should probably declare them as double
in the first place. I used int
because you were already but if they will be treated as double
then declare them as that type.