Why is the Game.cs file causing an error?
Why is the Game.cs file causing an error?
What is the error?
I have spent about 40 minutes but there is still errors. A lot of my past projects(maybe about 30%) still have errors.
You don’t generally call the
Main function, but perhaps you’ve been told to. When you call any function in C#, you provide the parameters, but not the type of parameter. Just like you don’t write:
int x = 5; System.Console.WriteLine(int x); //this definitely throws an error
That is where all the errors stem from.
It didn’t tell me to.
I used this line so that I could provide a default value. I didn’t want it to just be a letter but just the current cursor:
I just mean that I have to go to the
Program class which is in program.cs and get the cursor shape.
Main is a function which returns nothing. What are you trying to access using
. from it?
Well yes. What did you think would happen? Why are you trying to call the
Main function anyway?
To get the
cursor field variable which is in
UpdatePositionmethod it doesn’t mathch any standards of
switchand it just sets it to the
Programclass’ cursor variable(is it a field).
You can’t access variables within a function using
. notation. That is for accessing properties of classes. If you want a function to return a variable, then you can use the value stored in variable elsewhere.
This doesn’t work:
Also, what is an warning and exception in C#?
After some time I figured out to get
cursor I can’t use a
char return type. I cleverly thought maybe I could use
Too bad it doesn’t work. I have a big pile of errors. Let’s not worry about getting the
cursor variable and returning it and using it as if the user doesn’t press a key(in the Game.cs and
Here is my code:
I’ll give you a little bit of a picture of my errors:
Is getting a variable from a
Main method in the same namespace even possible? I can’t really use
No. I’ve been trying to say this for a while.
Main must return
void and accept a
String argument. Otherwise it isn’t the entry point for the program. You also don’t really call it using
class.Main, as its functionality isn’t part of the class it’s “in”. Why not create other method in your
Program class to return some of the variables you’re using.
EDIT: If you follow the instructions on the side of the lesson, I’m sure you’ll do fine.
How am I going to do that? I can’t just create another method that gets the method since it is in the
Main method. Now what?
Read through the instructions. They will guide you through it. Ignore my idea; according to the instructions, you don’t have to alter any code in
You don’t get it. I am extending the project not following instructions. So do you mean I can’t create another method that gets the variable. Yesterday I thought of a solution:
because the method call return value of
UpdateCursor() is reassigned to itself. I can break the problem down:
set the cursor variable which is going to be the return value of the
UpdateCursor value and is also going to be reassigned to itself, if the key parameter is not up, right, left, down the variable is not going to be changed for the rest of the method since it doesn’t match the up, right… keys. I makes sense for if the key parameter(it is going to be the representation of key presed), is not one of the arrow keys(up, down, left, right), the cursor variable should be the same as the original cursor variable in Program.cs. I can’t get that variable from
Main or another method.
check if a key parameter equals right key, left key, up key, down key:
set appropiate values to the cursor:
if right key:
if left key:
if up key:
If down key:
after set return the cursor.
UpdateCursor() gets called and gets reassigned to the same cursor variable.
This is to change the cursor shape.
Let me explain my solution:
If I can’t get the
cursor variable from Program.cs then this:
I can’t find the original cursor shape; if the key parameter didn’t represent up, down, left, right than I would be reassigning the cursor variable in Program.cs to itself(not nessesary and also I have some benefits). I have a smart things:
What if I reassign the cursor variable to itself only if the key pressed is up, down, left, right?
Benefit of that:
The only cursor possibilities of the key parameter is a string value representing:
A left, right, up, down.
Then I don’t need to find the original cursor key since it would match one the four conditions(a
case statement in a
switch statement). If it was right, down, left right then the returned value would be either
<. So I wouldn’t have to get the original cursor variable since it wouldn’t be it by the end. What a good plan!
Oh no! This shows an error: