Troubleshooting this TutorialSometimes, even though you try hard to understand the information in a tutorial, things don't work out quite like you want it to. This page is here to help you resolve any problems you might be having with the tutorial on creating classes in C#. The Common Mistakes section describes common problems that people have when doing the things in this tutorial, and how to resolve them. The Frequently Asked Questions section describes questions that people have that aren't related to mistakes, but rather, trying to understand the stuff better or exploring how it can be used. If your problem or concern isn't addressed here, feel free to add a comment below, so that I know where you're having trouble. I like to keep these pages fairly clean, so I may remove comments that I felt like have been addressed. If I remove your comment and you don't feel like the problem has been fixed, repost the question and we'll take another look at it. If a tutorial has a mistake in it, I will fix the mistake and reply to the comment with a brief explanation. However, after a couple of weeks I'll likely go back and remove the original comment as well as my reply, because, hopefully, the problem will have been fixed, and it won't be a concern any more. |
Common MistakesNone listed yet… |
Frequently Asked QuestionsNone listed yet… |
I don't think this is an error, but I do not see a place where we set the number of lives. When I debugged the application, it does not run through the while loop in main. Like I said, this may not be an error as you may cover this in later lessons. Thanks for a fantastic site!
Please disregard this, you cover getters and setters in your next lesson.
Hello,
First of all, thank you, great tutorials.
It could be worth it to mention that basic types are initialised to 0. Also, as mentionned above, since the variable related to the number of remaining lives is never set, the while loop never gets executed.
I did find it a bit confusing at first, albeit the mentions in the following tutorials.
Once again, thank you, great tutorials.
BR
Nich-1
Using the code below, I received the error that "The type or namespace 'Player' could not be found." Is there something I did wrong?
The basic problem here is that the Program class and the Player class are in different namespaces, and the Program class can't make sense if it.
Classes (all types) typically are organized into namespaces. These namespaces are large, logical groupings or related classes.
Any class you create can be accessed through what is called it's "fully qualified name", which is a fancy way of saying the full namespace, followed by the class name.
For example, your Player class has the fully qualified name of CreatingClasses.Player. Your Program class has the fully qualified name of Practice.Program.
One way to solve this problem is to simply use the fully qualified name instead of just the class name:
That's really wordy. And honestly, it gets much worse than that. Many times, the namespace is much longer, with a series of names separated by dots. For example, System.Collections.Generic.List.
It probably won't come as a surprise that there are shortcuts to simplify this. The first thing to point out is that whatever namespace you're currently in will always know about other classes and other types from the same namespace. If both Player and Program were either both in the CreatingClasses or Practice namespace, you wouldn't be having this issue. (I assume it only even happened this way because the code from the tutorial that you copied in for the Player class was in a different namespace from what you were already working in.)
So the second way to fix this would be to simply change on or the other's namespace to match the other.
Last, but not the least, there will be times where you actually want to use different namespaces, but you don't want to always use fully qualified names. If you look up at the top of your file, you'll see those using Blah.Blah.Bla; statements. Those are called using directives. They're basically there to say, "Look in these namespaces for types that seem to be missing."
If you're following the tutorials in order, you've already taken advantage of this, possibly without even knowing. For example, the Console class is actually in the System namespace. It's fully qualified name is System.Console, and that's how you'd normally have to access it, except because you've got that using System; at the top of your file, the C# compiler is smart enough to hunt down the Console class over there in the System namespace.
So this brings up the third way you could deal with this. You could add in another using directive in the Program class (well, to be more accurate, in the file Program.cs) that says:
I know I cover namespaces in at least a bit of detail somewhere around here, but that should hopefully be enough to get you up and running again!
Post preview:
Close preview