C# - Tier Separation - How to use these delegates? -
here's relevant code:
clickmegame.cs
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace classlibrary { public class clickmegame { public onclickme onclickmecallback; public int score; public clickmegame() { score = 0; } private void incrementscore() { score++; } } }
clickmecallbackdefinitions.cs
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace classlibrary { public delegate void onclickme(); }
mainwindow.cs (windows form)
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using classlibrary; namespace clickme { public partial class mainwindow : form { private clickmegame game; public mainwindow() { initializecomponent(); game = new clickmegame(); game.onclickmecallback = clickmebutton_click(); } private void clickmebutton_click(object sender, eventargs e) { updateui(); } private void updateui() { scorelabel.text = string.format("the score is: {0}", game.score); } } }
so i'm trying is, when user clicks button present on form, want label on form update game score increments every click.
i'm learning about/want able delegates in want separate project 2 tiers; presenation , logic. know it's unnecessary so, i'd make such when click button, windows form receives information game score via delegates/callback methods. i'm unsure how this, tried making callback definition , referencing it, i'm lost there.
assuming ui button uses click event clickmebutton_click here go.
public partial class mainwindow : form { private clickmegame game; public mainwindow() { initializecomponent(); game = new clickmegame(); game.onclickmecallback = param => updateui(); } private void clickmebutton_click(object sender, eventargs e) { game.onclickmecallback.invoke(); } private void updateui() { scorelabel.text = string.format("the score is: {0}", game.score); } }
Comments
Post a Comment