I have three classes.
(1) Logic
(2) Communication
(3) GUI
The GUI communcates to the Comm object by events.
The Comm object talks the the logic object by regular class invokation.
I'm trying to get the Logic object to call a method by use of a delegate that is in the comm object.
I'm trying to get that called method to call a method in the GUI by use of another delegate.
The compiler does not like this.
Error 1 An object reference is required for the non-static field, method, or property
Here is snippet of the code I wrote.
Code:
namespace HareAndTortoise
{
publicclassGameLogic
{
// com objects to talk to com layer
privateCostCarrotsCom CostCarrotsComObj;
privateGainCarrotsCom GainCarrotsComObj;
privateIllegalMoveCom IllegalMoveComObj;
public GameLogic()
{
//NumberGenerator = new randomNumberEngine();
RegisterDelegates();
}
publicvoid RegisterDelegates()
{
CostCarrotsComObj = newCostCarrotsCom(HareAndTortoise.Communication.PostCarrotLossCom);
GainCarrotsComObj = newGainCarrotsCom(HareAndTortoise.Communication.PostCarrotGainCom);
IllegalMoveComObj = newIllegalMoveCom(HareAndTortoise.Communication.PostIllegalMoveCom);
}
publicvoid ChangeDesciptionLogic(int index, Control x, Control[] c, GameState GS)
{
int CostOrGain = 0;
if (GS.Players[GS.Turn].PriorPosition != index)
{
if (DetermineLegalMove(index, x, c, GS) == false)
{
//PostIllegalMove();
}
else
{
CostOrGain = GainOrLoss(index, c, GS);
if (CurrentMoves.IsForwardMove(GS.Players[GS.Turn].CurrentPosition, index) == true)
{
CostCarrotsComObj(CostOrGain);
}
else
{
GainCarrotsComObj(CostOrGain);
}
}
}
else
{
//if ()
//{
//}
//else
//{
//}
}
}
Code:
namespace HareAndTortoise
{
//from com object to GUI
publicdelegatevoidPublishGameState();
publicdelegatevoidIllegalMove();
publicdelegatevoidLegalMove(int CostOrGain);
publicdelegatevoidMakeMove();
publicdelegatevoidPostLog(string LogRecord);
publicdelegatevoidPostPlayerStatus();
publicdelegatevoidRedrawBoard();
publicdelegatevoidPaintPlayers();
publicdelegatevoidCorrectBoard();
//from logic to com object
publicdelegatevoidCostCarrotsCom(int Cost);
publicdelegatevoidGainCarrotsCom(int Gain);
publicdelegatevoidIllegalMoveCom();
publicclassCommunication
{
public Communication()
{
RegisterDelegates();
}
publicvoid RegisterDelegates()
{
PublishState = newPublishGameState(HareAndTortoise.GameUI.UpdateState);
PostIllegalMove = newIllegalMove(HareAndTortoise.GameUI.IllegalMove);
PostCarrotGain = newLegalMove(HareAndTortoise.GameUI.CarrotGain);
PostCarrotLoss = newLegalMove(HareAndTortoise.GameUI.CarrotLoss);
PostMovePlayer = newMakeMove(HareAndTortoise.GameUI.PaintPlayer);
UpdateLog = newPostLog(HareAndTortoise.GameUI.LogActions);
PostStatus = newPostPlayerStatus(HareAndTortoise.GameUI.PostStatus);
PaintBoard = newRedrawBoard(HareAndTortoise.GameUI.ExternalCallToRedraw);
PaintPlayers = newPaintPlayers(HareAndTortoise.GameUI.RePaintPlayers);
BoardCorrection = newCorrectBoard(HareAndTortoise.GameUI.BoardCleanUp);
}
publicstaticvoid PostIllegalMoveCom()
{
PostIllegalMove();
}
publicstaticvoid PostCarrotLossCom(int Loss)
{
PostCarrotLoss(Loss);
}
publicstaticvoid PostCarrotGainCom(int Gain)
{
PostCarrotGain(Gain);
}
Code:
namespace HareAndTortoise
{
publicpartialclassGameUI : Form
{
publicCommunication CommunicationObj;
public GameUI()
{
InitializeComponent();
InitializePanels();
CommunicationObj = newCommunication();
}
publicvoid InitializePanels()
{
// Assign three handlers to each panel control
foreach (Control c in controls)
{
if (c != null)
{
c.MouseHover += newEventHandler(MouseHoverHandler);
c.MouseLeave += newEventHandler(MouseLeaveHandler);
c.Click += newEventHandler(MouseClickHandler);
}
}
}
privatevoid MouseClickHandler(Object sender, EventArgs e)
{
int index = 0;
foreach (Control c in controls)
{
if (c isPanel && sender.Equals(c))
{
ControlIndex = index;
CommunicationObj.SelectPosition(index, c, controls, GS);
break;
}
index++;
}
}
publicstaticvoid IllegalMove()
{
HareAndTortoise.GameUI.g[ControlIndex].FillRectangle(newSolidBrush(Color.Black), 0, 0, 50, 50);
HareAndTortoise.GameUI.g[ControlIndex].DrawString("Illegal\nMove", ArielFont, WhiteColor, newPointF(10, 10));
}
publicstaticvoid CarrotGain(int Gain)
{
HareAndTortoise.GameUI.g[ControlIndex].FillRectangle(newSolidBrush(Color.Black), 0, 0, 50, 50);
HareAndTortoise.GameUI.g[ControlIndex].DrawString("Gain\n" + Gain.ToString(), ArielFont, WhiteColor, newPointF(5, 10));
}
publicstaticvoid CarrotLoss(int Loss)
{
HareAndTortoise.GameUI.g[ControlIndex].FillRectangle(newSolidBrush(Color.Black), 0, 0, 50, 50);
HareAndTortoise.GameUI.g[ControlIndex].DrawString("Cost\n" + Loss.ToString(), ArielFont, WhiteColor, newPointF(5, 10));
}