Defining a static method in an interface

  • Thread starter Thread starter JME-56
  • Start date Start date
J

JME-56

Is it possible to define a static method in a C# interface? If not, is
there a workaround i can use?

It's like this. I'm writing this very small graphics library for .NET.
Now there's this class, and it contains 2 methods:

class DglSurface()
{
INativeSurface face;
...
internal DglSurface(INativeSurface face_)
{
face = face_;
}
public DglSurface(string filename)
{
// this is where i need help
}
}

Now, face is the native implemetation (SDL, DirectX, OpenGL). The
INativeSurface interface is as follows:

internal interface INativeSurface : IDisposable
{
DglSurface Load(string filename);
void SetAlpha(byte alpha);
void Blit(Point dest_point, DglSurface dest);
void Blit(Rectangle src_rect, Rectangle dest_rect, DglSurface dest);
int Width { get; }
int Height { get; }
}

Now the problem is: i want to construct a surface from a file, with
DglSurface.ctor(string filename). But, I can't declare Load to be
static in the interface. So I need an instance - and that's impossible
because i need the load function to create one! And it's also not
possible to declare a constructor in an interface...

So how can I arrange the Load method to be called as a static
function?
Any workarounds maybe?

I'm sorry if this message isn't very clear...
 
JME-56 said:
Is it possible to define a static method in a C# interface?
No.

If not, is there a workaround i can use?

Not really, I'm afraid.

Now the problem is: i want to construct a surface from a file, with
DglSurface.ctor(string filename). But, I can't declare Load to be
static in the interface. So I need an instance - and that's impossible
because i need the load function to create one! And it's also not
possible to declare a constructor in an interface...

So how can I arrange the Load method to be called as a static
function?
Any workarounds maybe?

Basically just document that you must provide a Load method, or that
you must provide a parameterless constructor - and hope that the class
author reads your documentation and tests the class rather than relying
on compiler errors.

We had a small debate about this recently - search for threads in the
C# newsgroup with posts by both myself and Nicholas Paldino in - you
should find it.
 
Back
Top