Just do this:
forecastDayControls.Add(forecastDay1);
You do not need the ref
keyword, because forecastDay1
is a class
and therefore it is always passed by reference.
题
I've read places that Controls cannot be loaded into an array or list and accessed in this manner, but I'm intrigued.
My problem is as follows: I am displaying a week's worth of forecast data within my application. The forecast data is retrieved from Wunderground's JSON API and stored into an object that contains the fields necessary for display (high/low temps, condition summary, etc). My app uses the Bing maps API to perform a geocoded search and the lat/long retrieved from the Bing Maps control is passed to the request to Wunderground's service to retrieve forecast data based on a lat/long coordinate. The response contains a list of "ForecastDay" objects, which hold the forecast data for each day.
Each day is displayed in its own custom user control, and I have named them accordingly as such: forecastDay1, forecastDay2, and so on.
I'm curious as to if there's a way to add these controls to a list by reference sort of like
forecastDayControls.Add(ref forecastDay1);
so I can iterate through the controls and populate them with information based on the corresponding day, each of which has been already been put into a list by the JSON deserialization. My code is perfectly functional, but it'd be nice and pretty looking
for(int i = 0; i < forecastDayControls.Count; i++)
{
forecastDayControls[0].ForecastDay = forecastDay[0];
}
if I could get them into a list and handle them that way, instead of going through 1 by 1 and having ugly code:
forecastDay1 = forecast.forecastDay[0];
forecastDay2 = forecast.forecastDay[1];
forecastDay3 = forecast.forecastDay[2];
forecastDay4 = forecast.forecastDay[3];
forecastDay5 = forecast.forecastDay[4];
forecastDay6 = forecast.forecastDay[5];
forecastDay7 = forecast.forecastDay[6];
Thanks!
解决方案
Just do this:
forecastDayControls.Add(forecastDay1);
You do not need the ref
keyword, because forecastDay1
is a class
and therefore it is always passed by reference.