Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Sean Conolly
Sean Conolly
19,110 Points

How do I fix this code in C#?

using System; using System.IO;

namespace Treehouse.CodeChallenges { public class Program { public static void Main(string[] arg) {

    }

    static string ParseWeatherForecast(string[] values)
        {
            var  weatherForecast = new WeatherForecast();
            //string values = line.Split(',');
            weatherForecast.WeatherStationId = DateTime.Parse(values[0]);
            DateTime.TryParse(values[0], out weatherForecast)

             return weatherForecast;
        }

}

}

2 Answers

Steven Parker
Steven Parker
231,236 Points

But if you want to use TryParse on the value, you might want to do that before the assignment, and use the value it produced in the assignment. Also, you might want to wrap the whole thing in an if condition so that the assignment is only done when the TryParse is successful. Something like this:

            var mydate = new DateTime();
            if (DateTime.TryParse(values[0], out mydate)} {
                weatherForecast.WeatherStationId = mydate;
            }
Sean Conolly
Sean Conolly
19,110 Points

Thanks Steven, here is the question:

Create a static method named ParseWeatherForecast that takes a string[] parameter named values and returns a WeatherForecast. Instantiate a WeatherForecast variable named weatherForecast and assign the appropriate value in the values array to the WeatherStationId property. Use the sample data shown in WeatherForecast.cs to determine which value in the array is the WeatherStationId. Don't forget to return the weatherForecast in the new method!

Sean Conolly
Sean Conolly
19,110 Points

Thanks Steven, here is the question:

Create a static method named ParseWeatherForecast that takes a string[] parameter named values and returns a WeatherForecast. Instantiate a WeatherForecast variable named weatherForecast and assign the appropriate value in the values array to the WeatherStationId property. Use the sample data shown in WeatherForecast.cs to determine which value in the array is the WeatherStationId. Don't forget to return the weatherForecast in the new method!