C# interfaces making me feel extra dumb

09/16/2010 21:06 pro4never#1
Ok so I've obviously seen the simple interface examples used before... I kinda understand how they work.. yet for some reason I seem to be unable to use them in my case atm :S


So what I'm doing is messing around with an existing quad tree example. My hopes is I'll be able to adapt it for use on my server (I HAVE looked through the example and understand it fairly well so far.. it's not that complicated, just like having something working with as a template to start)

The problem I'm having is this....

The example I have uses interfaces to pull x/y dimensions for the objects being stored in the quad (allows for collision detection simply using xna framework to determine what needs to be spawned/removed)... yet for some reason I can't figure out for the life of me how to go about doing this.


Sample code;

Code:
 public interface HasRect
    {
        Rectangle Rect { get; }
    }
    public class Quad<T> where T : HasRect
    {....
So Has rect is being used to get a rectangle from the objects being added to the quad... this is then used for coord and dimension information through the rest of the code..

So to add anything to this quad obviously, I need to add this to my Entity information.
Code:
public class GameClient : Calculation.HasRect
    {....
Now I need to find a way to implement this interface member... which is where I'm stuck.

Doing public Rectangle Rect = new Rectangle(X, Y, Height, Width); obviously works but does not allow me to do anything because it's not implementing the interface...

Some generic examples of things I've tried (I'm well aware they are not the right way.. just listing some things I've done..

HasRect(Rect); (where rect is a defined rectangle already created)

HasRect A = new HasRect(); //cannot create instance of the abstract class or interface blablabla

asRect A;
A.Rect = new Rectangle(1,2,3 ,4);

//cannot be assigned to, it's read only... I can add set; to the code and it will then tell me that A is an unassigned local variable obviously


Forget what else I tried... Basically I've tried every way I know to setup values and interface seems to just keep kicking my ass... I KNOW it's something simple but I can't seem to find any valid information on google about this type of thing. It's all calling functions and stuff through interfaces and inheritance.
09/17/2010 01:53 Nullable#2
From my understanding of your post you want to implement HasRect's members in the GameClient class, so you tried having a field in the class which may look like this
Code:
public class GameClient : HasRect
{
    public Rectangle Rect = new Rectangle(X, Y, width, height);
}
If this is the case then it won't work, you cannot declare fields in interfaces, thus you cannot declare a field in the inheriting class and expect it to satisfy the case.

What you declared in HasRect interface is a get only property, thus you have to implement it in GameClient class the same way, as a get only property
Code:
public class GameClient : HasRect
{
	private Rectangle m_Rect; // private member to be able to work around with the rectangle within class
	public Rectangle Rect // the declaration of the get only property
	{
		get { return this.m_Rect; }
	}
}
This piece of code shall work.
09/17/2010 05:10 pro4never#3
yahh I knew it was something to do with me just being a complete moron.

Never really used interfaces before so this is good learning experience. it was driving me crazy though cause I'm so used to other ways of doing things so that's just how my mind tends to work now.

I'll test it out tomorrow when I have some free time and get back w/ any issues I run into w/ quadtree. The example code I have now makes theoretical sense to me. Thinking I'm going to test out its actual usage and then see if I can write a fully custom quad system myself using it as reference (cause theoretical understanding and practical/usable knowledge != same lol)


<edit>

yah got it all working code wise but C# hates trying to use .xna so I'm writing all my geometry from scratch.

yayy for math I haven't had to do in ages! xD