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# C# Streams and Data Processing Parsing Data Working with DateTime

how to pass 'timeOfDay' variable to the TryParse method as an out parameter?

Hi guys, how can I pass 'timeOfDay' variable to the TryParse method as an out parameter?

thank you!

Program.cs
using System;
using System.IO;

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

        public static WeatherForecast ParseWeatherForecast(string[] values)
        {
            var weatherForecast = new WeatherForecast();

            /*
            Back in the ParseWeatherForecast method, add a new variable named timeOfDay to hold a DateTime value. 
            Use the DateTime.TryParse method to parse the correct value in the values array. 
            Check the sample data to determine the correct array element. 
            If the TryParse is successful, set the TimeOfDay property on the weatherForecast object.
            */

            /* video min 05:18 */
            if( var timeOfDay = DateTime.TryParse(values[1]) )
            {
                weatherForecast.TimeOfDay.set();
            }

            weatherForecast.WeatherStationId = values[0];

            return weatherForecast;
        }
    }
}
WeatherForecast.cs
using System;

/* 
Sample CSV Data

weather_station_id, time_of_day,      condition,    temperature, precipitation_chance, precipitation_amount
HGKL8Q,             06/11/2016 0:00,  Rain,         53,          0.3,                  0.03
HGKL8Q,             06/11/2016 6:00,  Cloudy,       56,          0.08,                 0.01
HGKL8Q,             06/11/2016 12:00, PartlyCloudy, 70,          0,                    0
HGKL8Q,             06/11/2016 18:00, Sunny,        76,          0,                    0
HGKL8Q,             06/11/2016 19:00, Clear,        74,          0,                    0
*/

namespace Treehouse.CodeChallenges
{
    public class WeatherForecast
    {
        public string WeatherStationId { get; set; }

        public DateTime TimeOfDay { get; set; }
    }
}

1 Answer

Tyler B
Tyler B
5,787 Points

Hi Tiago,

I know this is tricky but if you refer back the video before the challenge towards the very end at about 6 minutes in you'll see that TryParse() actually takes it's output in as a parameter using the out keyword. So you'll need to instantiate a DateTime variable before then using TryParse() and passing it in after the input parameter. The TryParse() can then be wrapped in an IF statement and if it is successful (returns true) you can assign that DateTime variable you instantaiated before to the weatherForecast.TimeOfDay.

Hope that helps!

Thank you Tyler, you just unlocked the out parameter that was missing ;)

keep rocking!