I’m working with an application that utilizes the Google Weather API, right now I got some real ugly code just to populate the 3 day forecast, which looks like so (remember no laughing)
groupBox3.Text = set.Forecast[1].DayOfTheWeek;
label4.Text = string.Format("High {0}", set.Forecast[1].High);
label3.Text = string.Format("Low: {0}", set.Forecast[1].Low);
label11.Text = string.Format("Condition: {0}", set.Forecast[1].Condition);
pictureBox1.Load(set.Forecast[1].Icon);
groupBox4.Text = set.Forecast[2].DayOfTheWeek;
label14.Text = string.Format("High {0}", set.Forecast[2].High);
label13.Text = string.Format("Low: {0}", set.Forecast[2].Low);
label20.Text = string.Format("Condition: {0}", set.Forecast[2].Condition);
pictureBox2.Load(set.Forecast[2].Icon);
groupBox5.Text = set.Forecast[3].DayOfTheWeek;
label7.Text = string.Format("High {0}", set.Forecast[3].High);
label6.Text = string.Format("Low: {0}", set.Forecast[3].Low);
label11.Text = string.Format("Condition: {0}", set.Forecast[3].Condition);
pictureBox3.Load(set.Forecast[3].Icon);
I told you it was ugly. What I would like to do is have a single loop (and possible Generics) to accomplish the same task as this ugly code. I’ve tried a couple different ways but they always fail. The weather is being generated like so
WeatherSet set = WeatherService.Response(GoogleWeatherRequest.RequestData(new WeatherService("99109")));
And inside the variable set there is information for current information and 3 day forecast, but having it run like this drives me insane because it so noobish and not very efficient. So anyone got any cool & efficient way toaccomplish this task?
I think you should create your own custom control ForecastView for that with groupbox, three labels and picturebox on it. Let it have a property named
Forecastand put the logic that fills the fields inside its setter. That way the code of the form will be really clean, likeSeeing as you are already putting your field inside of the groupbox, placement shouldn’t be a problem.
Also, putting it all in separate control will give you a lot of code reuse options if one day you need to present this data on another form or even in another app. Not to mention that you’ll be able to change the appearance of all forecasts at once if you decide to put the pictureBox left instead of right, etc.