i am trying to build an interface array for my checker class. i have 3 classes:
main class, checker class and king class.

my interface:

interface IchecherMove{
        bool isMoveValid(IchecherMove[,] checker, int columnStart, int rowStart, int columnEnd, int rowEnd);
        string FindPiece();
        string print();
        bool pieceDevour(IchecherMove[,] checker, int columnStart, int rowStart, int columnEnd, int rowEnd);

i made an array out of it:

IchecherMove[ , ] checker = new Checker[9 , 9];
            checker[8, 1] = new Checker("W");
            checker[8, 3] =new Checker("W");
            checker[8, 5] = new Checker("W");
            checker[8, 7] = new Checker("W");

            checker[7, 2] = new Checker("W");
            checker[7, 4] = new Checker("W");
            checker[7, 6] = new Checker("W");
            checker[7, 8] = new Checker("W");

            checker[6, 1] = new Checker("W");
            checker[6, 3] = new Checker("W");
            checker[6, 5] = new Checker("W");
            checker[6, 7] = new Checker("W");

            checker[1, 2] = new Checker("B");
            checker[1, 4] = new Checker("B");
            checker[1, 6] = new Checker("B");
            checker[1, 8] = new Checker("B");

            checker[2, 1] = new Checker("B");
            checker[2, 3] = new Checker("B");
            checker[2, 5] = new Checker("B");
            checker[2, 7] = new Checker("B");

            checker[3, 2] = new Checker("B");
            checker[3, 4] = new Checker("B");
            checker[3, 6] = new Checker("B");
            checker[3, 8] = new Checker("B");

            checker[5, 8] = new King("BK"); // this is shown as an error and it says " make sure the object type is convertiable to the array

The application of the the interface to the classes looks like that:

class Checker : IchecherMove
class King : IchecherMove

what does the program mean by the error aforementioned.? why can i put several objects into an interface array?

6 Years
Discussion Span
Last Post by NewOrder

Hello, here's where your problem sits:
1. You define and array of interfaces

IchecherMove[ , ] checker

2. Then instantiate your array using Checker class

new Checker[9 , 9];

don't see the problem yet? :) ok, continuing ..

3. You're inserting some variables into it, including this:

checker[5, 8] = new King("BK");

So what the problem is .. when you define an array of interface type and instantiate it by derived class - everything is seems to be good (since the Chacker class is easily automatically can be converted to IchecherMove). But if you said, that your array would contain a King instance - it's arguing with you, since King can't be implicitly converted to Chacker (as you said at instantiation time).

this change might resolve the error:

IchecherMove[ , ] checker = new IchecherMove[9 , 9];

but you cannot instantiate interfaces. So we need some workaround .. as a way, I can suggest you to use List<T> :

//for 1-dimentional
List<ISomeInterface> list = new List<ISomeInterface>();

//and for 2-dimentional
List<List<ISomeInterface>> list = new List<List<ISomeInterface>>();

then to instantiate 1 row ..

list.Add(new List<ISomeInterface>());

and to add elements to this row ..

list[0].Add(new SomeClass());
ist[0].Add(new SomeOtherClass());

where, ofcourse

class SomeClass:ISomeInterface{
class SomeOtherClass:ISomeInterface{

Good luck :)


You were good up until you hit:

this change might resolve the error:

IchecherMove[ , ] checker = new IchecherMove[9 , 9];

but you cannot instantiate interfaces. So we need some workaround ..

You can declare arrays of interfaces, as it's not instantiating an interface, it's just creating a place to hold items of that interface. You can fix the code by changing this part.


Momerath, thanks for correcting.
Probably, shouldn't multitask while posting :D

Votes + Comments
thanks for the help

no, no , no. i dont want to use generics & collections. i know arrayLists, and i see that you found an easier solution. but that would mean i have to re-write the printing code...damn

so are you saying that interfaces cant include more than 1 object type?

should i use an abstract class instead of an interface?


Interfaces can have lots of object types. Arrays instantiated as a specific class (as you did) cannot hold objects that are not of that type, or not derived from that type. Change the one line to create an interface array, not an array of Checker.

Votes + Comments
thanks for the help

oooh dammit. what a stupid mistake i made. i didnt even notice!! i am such a dumn @ss., thanks momerath. now it works...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.