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 trialLarry Singleton
18,946 PointsLost again... C# Dictionary
Need some direction, probably over thinking this... Am I even in the same ball park?
Link: https://teamtreehouse.com/library/c-collections/sets-and-dictionaries/dictionary
Question: Create a public field named WordCount that is a dictionary with keys of type string and values of type int. Initialize WordCount to an empty dictionary.
using System.Collections.Generic;
namespace Treehouse.CodeChallenges { public class LexicalAnalysis {
}
}
The result following an attempt following the video: using System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
public class LexicalAnalysis
{
private static Dictionary<char, string> _WordCount = new Dictionary<char, string>
}
public static string toInt(string input) { }
using System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
public class LexicalAnalysis
{
}
}
5 Answers
Larry Singleton
18,946 PointsI just passed it. I was completely overthinking it.
using System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
public class LexicalAnalysis
{
public Dictionary<string, int> WordCount = new Dictionary<string, int>();
}
}
I was thinking there had to be a 2nd dictionary created, a dictionary called "Empty" for the WordCount to be initialized to. I have a bad habit of over thinking these challenges. Thanks for being so thoughtful and willing to help. I greatly appreciate it.
Noah Yasskin
23,947 Pointsmy answer:
using System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
public class LexicalAnalysis
{
public static Dictionary<string, int> WordCount = new Dictionary<string, int>();
}
}
Larry Singleton
18,946 Pointsusing System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
public class LexicalAnalysis
{
public Dictionary<string, int> WordCount = new Dictionary<string, int>();
}
Steven Parker
231,198 PointsYou may have read the directions a bit hastily:
- The challenge asked you to "Create a public field", but you declared yours private.
- The challenge asked for a "field named
WordCount
", but you named yours_WordCount
(with underscore). - The challenge said to use *"keys of type
string
and values of typeint
, but you have char and string. - When creating an object instance with "new", remember to put the parentheses after the constructor.
- What's that line "
public static string toInt(string input) { }
"? It seems unrelated to the challenge.
Larry Singleton
18,946 PointsI know now, lol. Thanks. I'm actually on the MSDN website working on this now. Here is the latest I am working with.
using System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
public class LexicalAnalysis
{
public Dictionary<string, int> WordCount = new Dictionary<string, int>();
}
class CollIInit
{
Dictionary<int, empty> empty = new Dictionary<int>()
};
}
I'm setting in class right now and can't do videos, so I'm looking it up on the MSDN site. I should have taken Java... wrapping my head around this has been a huge struggle.
Steven Parker
231,198 PointsI'm confused by the code starting with "class CollIInit
" ... there's no mention of that in the instructions.
Your declaration and initialization of Wordcount are fine now. Just remove the extraneous code and you'll pass the challenge.
Steven Parker
231,198 PointsSteven Parker
231,198 PointsLarry Singleton — Glad to help. You can mark the question solved by choosing a "best answer".
Happy coding!