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#

cbekter
cbekter
7,600 Points

Cannot convert from 'SoccerStats.GameResult' to 'SoccerStats.GameResult[]

Getting an error when I am adding the value of gameResult into the soccerResults list. I watched the video and it looks like my program looks just like hers, but I am getting this error, while hers builds. Can someone please help me see what I am overlooking?

using System;
using System.Collections.Generic;
using System.IO;

namespace SoccerStats
{
    class Program
    {
        static void Main(string[] args)
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            DirectoryInfo directory = new DirectoryInfo(currentDirectory);
            var fileName = Path.Combine(directory.FullName, "SoccerGamesResults.csv");
            var fileContents = ReadSoccerResults(fileName);

        }

        public static string ReadFile(string fileName)
        {
            using (var reader = new StreamReader(fileName))
            {
                return reader.ReadToEnd();
            }
        }

        public static List<GameResult[]> ReadSoccerResults(string fileName)
        {
            var soccerResults = new List<GameResult[]>();
            using (var reader = new StreamReader(fileName))
            {
                string line = "";
                reader.ReadLine();
                while((line = reader.ReadLine()) != null)
                {
                    var gameResult = new GameResult();
                    string[] values = line.Split(',');
                    //gameResult.GameDate = DateTime.Parse(values[0]);
                    DateTime gameDate;
                    if(DateTime.TryParse(values[0], out gameDate))
                    {
                        gameResult.GameDate = gameDate;
                    }
                    gameResult.TeamName = values[1];
                    HomeOrAway homeOrAway;
                    if(Enum.TryParse(values[2], out homeOrAway))
                    {
                        gameResult.HomeOrAway = homeOrAway;
                    }
                    soccerResults.Add(gameResult);
                }
            }
            return soccerResults;
        }
    }
}

1 Answer

Emmanuel C
Emmanuel C
10,636 Points

It seems like in ReadSoccerResults(string fileName) youre initializing soccerResults to a list of GameResults array and returning that. If i recalled that lesson correctly you just want a list of GameResults. When you add gameResult to soccerResults, its expecting an array of the GameResult object or GameResult[], but youre only passing in a single GameResult object. which is what that error is all about.

I would remove the array brackets [] from the initialization and the return type. And that should fix it.