I'm following a tutorial on C# on making a text-based game and I ran into an issue right at the start. The following code:
namespace GameV2
{
class Level
{
private static Room[,] rooms;
#region Properties
public static Room[,] Rooms
{
get { return rooms; }
}
#endregion
public static void Initialize();
*{*
}
private static *BuildLevel*();
{
}
return false;
}
*}*
gives me 3 errors.
Error 1 Invalid token '{' in class, struct, or interface member declaration
Error 2 Expected class, delegate, enum, interface, or struct
Error 3 Type or namespace definition, or end-of-file expected
The italics represent the errors in order. Fr some reason Visual c# express won't let me use { in a method definition, and pushes my final } out of the code box. Any ideas on why this happens?
You don't have semicolons after methods. You may be confusing them for C
method prototypes.
BuildLevel should have a return type.
All statements have to be inside methods, you can only have declarations outside of methods
This should compile:
namespace GameV2
{
class Level
{
private static Room[,] rooms;
#region Properties
public static Room[,] Rooms
{
get { return rooms; }
}
#endregion
public static void Initialize()
{
}
private static bool BuildLevel()
{
return false;
}
}
}
public static void Initialize();
private static *BuildLevel*();
Those are declarations. They cannot be followed by { }. Remove the ; and it will work.
private static TYPEHERE *BuildLevel*();
This is missing a return type.
Remove the two ;
public static void Initialize()
{
}
private static BuildLevel()
{
}
public static void Initialize();
{
}
should be
public static void Initialize()
{
}
Remove the semicolons from the end of your function declarations (before the opening curly brace).
Watch out for the semicolons. You have semicolons between the method names and their bodies.
This may be the problem:
private static *BuildLevel*();
{
}
return false;
You didn't specify a return type, and the return false; should be inside the brackets.
Related
Picture a case like this:
I have a controller action (or service method) where I need to call three methods in a consecutive order, each method has a single responsibility.
public return_type MyMethod(_params_) {
// .. some code
Method_1 (...);
Method_2 (...);
Method_3 (...);
// ... some more code
}
A developer can make the mistake of calling Method_2 before Method_1, or at least we can say that nothing forces him to follow this order, or to get an exception when the order isn't followed.
Now we can call Method_2 inside Method_1, and Method_3 inside Method_2, but that doesn't seem right when each method handles a completely different responsibility.
Is there a design pattern for this situation? Or any "clean" way to handle this?
This is exactly what facade pattern do.
Try to extract the three methods to another class, and make them private. Expose a single method MyMethod that calls the other methods in the desired order. Clients should use Facade.MyMethod
More details: https://en.m.wikipedia.org/wiki/Facade_pattern
I suppose you should leave control of execution for yourself and give possibility just to set what should be executed.
public interface IMethodsExecutor
{
void Execute();
void ShouldRunMethod1();
void ShouldRunMethod2();
void ShouldRunMethod3();
}
public class MethodsExecutor: IMethodsExecutor
{
private bool _runMethod1;
private bool _runMethod2;
private bool _runMethod3;
public MethodsExecutor()
{
_runMethod1 = false;
_runMethod2 = false;
_runMethod3 = false;
}
public void ShouldRunMethod1()
{
_runMethod1 = true;
}
public void ShouldRunMethod2()
{
_runMethod2 = true;
}
public void ShouldRunMethod3()
{
_runMethod3 = true;
}
private void Method1()
{
}
private void Method2()
{
}
private void Method3()
{
}
public void Execute()
{
if (_runMethod1)
{
Method1();
}
if (_runMethod2)
{
Method2();
}
if (_runMethod3)
{
Method3();
}
}
}
So that the usage will be:
IMethodsExecutor methodsExecutor = new MethodsExecutor();
methodsExecutor.ShouldRunMethod1();
methodsExecutor.ShouldRunMethod3();
methodsExecutor.Execute();
If I have
public class AImplementation:IAInterface
{
void IAInterface.AInterfaceMethod()
{
}
void AnotherMethod()
{
((IAInterface)this).AInterfaceMethod();
}
}
How to call AInterfaceMethod() from AnotherMethod() without explicit casting?
There are lots of ways of doing this without using the cast operator.
Technique #1: Use "as" operator instead of cast operator.
void AnotherMethod()
{
(this as IAInterface).AInterfaceMethod(); // no cast here
}
Technique #2: use an implicit conversion via a local variable.
void AnotherMethod()
{
IAInterface ia = this;
ia.AInterfaceMethod(); // no cast here either
}
Technique #3: write an extension method:
static class Extensions
{
public static void DoIt(this IAInterface ia)
{
ia.AInterfaceMethod(); // no cast here!
}
}
...
void AnotherMethod()
{
this.DoIt(); // no cast here either!
}
Technique #4: Introduce a helper:
private IAInterface AsIA => this;
void AnotherMethod()
{
this.AsIA.IAInterfaceMethod(); // no casts here!
}
You can introduce a helper private property:
private IAInterface IAInterface => this;
void IAInterface.AInterfaceMethod()
{
}
void AnotherMethod()
{
IAInterface.AInterfaceMethod();
}
Tried this and it works...
public class AImplementation : IAInterface
{
IAInterface IAInterface;
public AImplementation() {
IAInterface = (IAInterface)this;
}
void IAInterface.AInterfaceMethod()
{
}
void AnotherMethod()
{
IAInterface.AInterfaceMethod();
}
}
And yet another way (which is a spin off of Eric's Technique #2 and also should give a compile time error if the interface is not implemented)
IAInterface AsIAInterface
{
get { return this; }
}
You can't, but if you have to do it a lot you could define a convenience helper:
private IAInterface that { get { return (IAInterface)this; } }
Whenever you want to call an interface method that was implemented explicitly you can use that.method() instead of ((IAInterface)this).method().
Yet another way (not best):
(this ?? default(IAInterface)).AInterfaceMethod();
Can't you just remove the "IAInterface." from the method signature?
public class AImplementation : IAInterface
{
public void AInterfaceMethod()
{
}
void AnotherMethod()
{
this.AInterfaceMethod();
}
}
This question is like Cannot access Public method in Web Control's Page_Load event
However since you are not allowed to ask questions inside someone else's question I'm making a new question.
I have the following layout:
namepspace
{
public partial class
{
protected void Page_Load
{
}
class Employee
{
public static bool employeeType
{
}
}
}
If I want to access employeeType in Page_Load, how can I access it?
This
protected void Page_Load
{
// We can access whitout creating a instance since it is static
Employee.employeeType
}
Doesn't work? Also, what exactly is employeeType? If it is a function:
protected void Page_Load
{
Employee.employeeType();
}
But then you are missing the parenthesis and the return type on the declaration. You should declare like this, preferably starting with uppercase (call it with uppercase also):
class Employee
{
public static void EmployeeType()
{
}
....
}
Edit
So, it is a static property. Then:
protected void Page_Load
{
bool type = Employee.EmployeeType;
}
class Employee
{
public static bool EmployeeType
{
get { return true; } // Your logic here...
}
....
}
I am seeing a strange problem in my C# code. I have something like this:
public static class ErrorHandler {
public static int ErrorIgnoreCount = 0;
public static void IncrementIgnoreCount() {
ErrorIgnoreCount++;
}
public static void DecrementIgnoreCount() {
ErrorIgnoreCount--;
}
public static void DoHandleError() {
// actual error handling code here
}
public static void HandleError() {
if (ErrorIgnoreCount == 0) {
DoHandleError();
}
}
}
public class SomeClass {
public void DoSomething() {
ErrorHandler.IncrementIgnoreCount();
CodeThatIsSupposedToGenerateErrors(); // some method; not shown
ErrorHandler.DecrementIgnoreCount();
}
}
The problem is that the compiler often decides that the order of the three calls in the DoSomething() method is not important. For example, the decrement may happen before the increment. The result is that when the code that is supposed to generate errors is run, the error handling code fires, which I don't want.
How can I prevent that?
Add Trace or Logs to your code in IncrementIgnoreCount, DecrementIgnoreCount and HandleError function.
That will help you to view real call order.
I looked on another question similar to this but couldn't quite understand what they did to solve the problem.
I am simply passing a value into a public static int:
namespace ModNote
{
public partial class homeScreen : Form
{
public homeScreen()
{
InitializeComponent();
}
private void gamemodButton_Click(object sender, EventArgs e)
{
backgroundProgram.moduleNumber = 1;
this.Hide();
moduleScreen showForm = new moduleScreen();
showForm.Show();
}
and this is where this variable is initialized
namespace ModNote
{
#region // Setting up Variables
public class backgroundProgram
{
public static int moduleNumber;
}
#endregion
}
and here a picture of the error: http://puu.sh/opETJ/fb8152d164.png
Thankyou.
edit: initializing the string array causes this error, any problems with this array being initialized? (moduleArray)
namespace ModNote
{
#region // Setting up Variables
public class backgroundProgram
{
public static int moduleNumber;
public static string[] noteArray;
public static string[] moduleArray = new string[7]
{ File.ReadAllText(#"ModulesFile\CGP1005M.txt"),
File.ReadAllText(#"ModulesFile\CMP1005M.txt"),
File.ReadAllText(#"ModulesFile\CMP1123M.txt"),
File.ReadAllText(#"ModulesFile\CMP1124M.txt"),
File.ReadAllText(#"ModulesFile\CMP1125M.txt"),
File.ReadAllText(#"ModulesFile\CMP1127M.txt"),
File.ReadAllText(#"ModulesFile\CMP1129M.txt")
};
}
#endregion
}
If the exception is throw here:
public static string[] moduleArray = new string[7]
{ File.ReadAllText(#"ModulesFile\CGP1005M.txt"),
File.ReadAllText(#"ModulesFile\CMP1005M.txt"),
File.ReadAllText(#"ModulesFile\CMP1123M.txt"),
File.ReadAllText(#"ModulesFile\CMP1124M.txt"),
File.ReadAllText(#"ModulesFile\CMP1125M.txt"),
File.ReadAllText(#"ModulesFile\CMP1127M.txt"),
File.ReadAllText(#"ModulesFile\CMP1129M.txt")
};
Then one of those lines is throwing an exception. There are all sorts of reasons for an exception to be thrown when reading from a file - security, not found, in use, etc.
I would suggest moving that logic to the static constructor so you can debug it to find the immediate problem, then add better error handling.
Another option is to not read all of that data in the static constructor and instead create an Initialize method or something. Exceptions in static constructors are difficult to handle in general.