cannot implicitly convert type obect to....

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to push a array or struct onto a queue then dequeue... when
dequeue'ing, it is returned at type"object"... i can see the "value" is
acutally the struct/array type however, once i get this return object I
cannot access any of the definitions/values withing the struct... heres an
ex...

public class BFSPegBoardNode
{

public PegBoard[,] BFSBoard;
public Move BFSMove;

}

BFSPegBoardNode CurrentNode = new BFSPegBoardNode();
MyMoveQueue.Enqueue(CurrentNode);
objCurrentNode = MyMoveQueue.Dequeue();

in this code i declared objCurrentNode as an object however, i really want
it to be of type BFSPegBoardNode.

PLEASE HELP... thanks
 
jmrieman said:
I am trying to push a array or struct onto a queue then dequeue... when
dequeue'ing, it is returned at type"object"... i can see the "value" is
acutally the struct/array type however, once i get this return object I
cannot access any of the definitions/values withing the struct... heres an
ex...

public class BFSPegBoardNode
{

public PegBoard[,] BFSBoard;
public Move BFSMove;

}

BFSPegBoardNode CurrentNode = new BFSPegBoardNode();
MyMoveQueue.Enqueue(CurrentNode);
objCurrentNode = MyMoveQueue.Dequeue();

in this code i declared objCurrentNode as an object however, i really want
it to be of type BFSPegBoardNode.

PLEASE HELP... thanks
Try this:

objCurrentNode = (BFSPegBoardNode)MyMoveQueue.Dequeue(); //Will throw an
exception if it cannot cast.

or

objCurrentNode = MyMoveQueue.Dequeue() as BFSPegBoardNode; //will return
null if it cannot cast. This only works for Reference Types IIRC.

HTH

JB
 
Back
Top