Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
221,902 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!