0

I'm trying to port an existing C# project to a new distributed one, using interfaces connecting client and server components (its a Sokoban game for a college exercise).

And here is my issue:

In one component, say: Level.cs, i have:

Level.cs

namespace Sokoban
{
public enum MoveDirection { Right, Left, Down, Up }


public class Level
    {
    private MoveDirection sokoDirection = MoveDirection.Right;
    ...
    }
...
}

And by the other hand, the interfaces:

Interfaces.cs:

namespace Interfaces
    {
    public class ILevel : MarshalByRefObject
        {
        ---> what should i place here??? <---
        }
    }

I've tried with no success the following options:

Interfaces.cs:

public virtual enum MoveDirection { get; set; }
public virtual enum MoveDirection() { throw new NotImplementedException(); }

Level.cs:

public static enum MoveDirection { return { Right, Left... }; }
public static enum MoveDirection { get { return { right, Left, ...}; } }
JAguirre
  • 45
  • 8
  • Why are you using `MarshalByRefObject`? Are you trying to use Remoting? Remoting is a legacy technology that is retained for backward compatibility with existing applications and is not recommended for new development. Distributed applications should now be developed using WCF or ASP.NET Web API. See the note at the top of http://msdn.microsoft.com/en-us/library/vstudio/xws7132e.aspx for proof. – John Saunders Jan 14 '14 at 12:38
  • Hi John! thanks for the instantly response! – JAguirre Jan 14 '14 at 12:40
  • And, _why_ are you using `MarshalByRefObject`? – John Saunders Jan 14 '14 at 12:42
  • I'm using Marshal for serializaton puroposes. You're right: i'm using Remoting. – JAguirre Jan 14 '14 at 12:47
  • Save yourself the grief, and stop doing that. Remoting is ancient and has long since been replaced with WCF. You should stop using Remoting ASAP. – John Saunders Jan 14 '14 at 12:48
  • Enum is a valuetype, you dont need to inherit from `MarshalByRefObject`. It will work out of the box. – leppie Jan 14 '14 at 12:57

2 Answers2

4
public interface ILevel
{
     MoveDirection MoveDirection { get; set; }
}

and the implementation:

public class Level : MarshalByRefObject, ILevel
{
     public MoveDirection MoveDirection { get; set; }    
}
Charles Mager
  • 25,735
  • 2
  • 35
  • 45
  • Hi! i've tried that, but Visual Studio complains: syntax error, expected: ",". – JAguirre Jan 14 '14 at 12:44
  • Where does it say it's missing? You may have to post the full code. – Charles Mager Jan 14 '14 at 12:48
  • The question is really unclear to me, but maybe he wants the name Soko, as in `public interface ILevel { MoveDirection SokoDirection { get; set; } }` with the implementation: `public class Level : MarshalByRefObject, ILevel { public MoveDirection SokoDirection { get; set; } }`. – Jeppe Stig Nielsen Jan 14 '14 at 13:24
  • Hi! This seems to be close to the solution. I've tried it, and now the issue is: // Error advice: "Interfaces.ILevel.MoveDirection is a property, but is used as a type": public MoveDirection MoveDirection { get; set } – JAguirre Jan 14 '14 at 13:58
  • Again, we'd need the rest of the code. The likelihood is you've tried to use ILevel.MoveDirection somewhere in the middle of a class definition (i.e. not in a method). – Charles Mager Jan 14 '14 at 14:02
0

I don't know if I exactly match the question. Maybe it is the case, that you want provide an enum to all classes which implements the interface or use the classes. I have tried to provide the enum via the interface too. But no luck.

I'm a C# beginner so my maybe trivial approaches are:
1. Provide it via an abstract class. It works, but you get a little bit overhead in class definition if it was originally enough to use an interface (write overwrite etc.).
2. My workaround is to provide enums in a separate namespace, which is to include everywhere where you need it.
In your case e.g.:


Interfaces.cs

using Enumerations;
namespace Interfaces
{
    interface ILevel : MarshalByRefObject
    {
        int changeDirection(MoveDirection newDirection);
    }
}
namespace Enumerations
{
    public enum MoveDirection { Right, Left, Down, Up };
}
MichiBack
  • 1,310
  • 13
  • 12