For some reason when I call a namespace within this class, I get an error which states that
it requires an object reference. I thought that using a reference variable of the IMethodResponses type would allow me to access a method within its own creation, but
I can’t accomplish this, and nor can I simply just implement the interface without a reference and use its methods…
Can someone help me out with this? I’ll post the Interface itself too in case their’s something wrong. I should probably note that I haven’t been doing this very long.
//Class Implementing Interface:
internal sealed class Name : INameCreation, IMethodResponses
{
public ScratchCreate create = ScratchCreate.create;
ListCreate select = new ListCreate();
public IMethodResponses _responses = IMethodResponses; //<--Error
public static void ChooseName()
{
int response;
Console.WriteLine("Press \'1\' to select from a list of names, or \'2\' to create your own.");
Console.WriteLine("If you wish to quit, you may do so by pressing \'0\'");
response = int.Parse(Console.ReadLine());
do
{
Console.WriteLine("Sorry, you didn't enter any of the correct responses...");
Console.WriteLine("Press \'1\' to select from a list of names, or \'2\' to create your own.");
Console.WriteLine("If you wish to quit, you may do so by pressing \'0\'");
response = int.Parse(Console.ReadLine());
}
while (response != 0 || response != 1 || response != 2);
_responses.IntegerResponse(response);
}
public void IntegerResponse(int response)
{
switch (response)
{
case 0:
Console.WriteLine("Goodbye!");
break;
case 1:
break;
case 2:
break;
}
}
//Interface:
namespace PlayerCreation
{
public interface INameCreation
{
}
public interface IMethodResponses
{
void IntegerResponse(int response);
void StringResponse(string response);
}
}
If you make your IntegerResponse method static you can call it with:
For:
From a static method you can acess only static members.
Setting an type into a variable of the same type won’t work.
What is expected to fit into _responses is an instance of any class of its type or any class that implements its interface (IMethodResponses). Since you cannot create an instance of an interface than it is the latter.
This will work but is not needed, since IntegerResponse can safely be made static as is by using the static keyword before the method definition.
What is a bit awkward is the use of
public static void ChooseName(). This method is static and returns nothing. It just uses Console.Writeline. You will find out that as soon as you start developing a real application this will need to be redone.