Delegate Calling a method that Calls a delegate.

This is a discussion on Delegate Calling a method that Calls a delegate. within the C# Programming forums, part of the General Programming Boards category; I have three classes. (1) Logic (2) Communication (3) GUI The GUI communcates to the Comm object by events. The ...

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Delegate Calling a method that Calls a delegate.

    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));
    }
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The error message means that you're trying to call an object method as if it were a normal static function (without the "object." notation).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Code:
    public static void 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));
    }
    What exactly is ControlIndex ? It's not a parameter, yet it is used in a static method...
    What is g ? Is it really a public static property of GameUI ? If so, think about changing it.

    Sidenote: You logic should not think in terms of UI objects. No System.Windows.Forms objects should be used there. Check if "Control" parameters are really neccessary.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for criticism on my GameState class
    By Raigne in forum Game Programming
    Replies: 1
    Last Post: 03-21-2008, 10:45 AM
  2. calling a method from another file
    By Beowolf in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2007, 10:40 PM
  3. Problem calling external method from within a shared library
    By Major Tom in forum C++ Programming
    Replies: 0
    Last Post: 04-21-2007, 10:14 AM
  4. Calling a Method from Another Class...
    By Dan17 in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2006, 06:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21