Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
GeneralRe: Query on Delegate Pin
OriginalGriff12-Jul-12 2:23
mveOriginalGriff12-Jul-12 2:23 
GeneralRe: Query on Delegate Pin
ashish712-Jul-12 2:32
ashish712-Jul-12 2:32 
GeneralRe: Query on Delegate Pin
ashish712-Jul-12 2:44
ashish712-Jul-12 2:44 
QuestionRe: Query on Delegate Pin
DaveyM6912-Jul-12 3:22
professionalDaveyM6912-Jul-12 3:22 
AnswerRe: Query on Delegate Pin
ashish712-Jul-12 3:31
ashish712-Jul-12 3:31 
GeneralRe: Query on Delegate Pin
DaveyM6912-Jul-12 7:38
professionalDaveyM6912-Jul-12 7:38 
GeneralRe: Query on Delegate Pin
ashish712-Jul-12 7:53
ashish712-Jul-12 7:53 
GeneralRe: Query on Delegate Pin
OriginalGriff12-Jul-12 3:48
mveOriginalGriff12-Jul-12 3:48 
There are only two changes you can really make there: the first is to change from anonymous methods to named methods.
C#
this.game.GameStarted += delegate
{
    this.ResetWinTransition.Begin();
    this.StatusPanel.Visibility = Visibility.Visible;
    this.TapToContinueTextBlock.Opacity = 0;
    this.TotalMovesTextBlock.Text = this.game.TotalMoves.ToString();
};
Becomes
C#
this.game.GameStarted += MyGameStarted;
...
private void MyGameStarted(object sender, EventArgs e)
{
    this.ResetWinTransition.Begin();
    this.StatusPanel.Visibility = Visibility.Visible;
    this.TapToContinueTextBlock.Opacity = 0;
    this.TotalMovesTextBlock.Text = this.game.TotalMoves.ToString();
};
This is a matter largely of style - as I said I prefer named methods, but have a look at the other comments you got before you make your mind up.

The other change looks trivial, and it adds a little code, but it is something I do with all delegates, be they event handlers or not.
C#
if (this.GameStarted != null)
{
    this.GameStarted(this, null);
}
Becomes
C#
EventHandler eh = GameStarted;
if (eh != null)
{
    eh(this, null);
}
The only difference is that I read the value once, and action from that value, while your code reads it twice. If the delegate method is removed from the list between the two instructions, your version will get a null reference, while mine won't. That said, it is very,. very unlikely to ever happen, but I prefer to code for the unlikely events as well as the common ones! Laugh | :laugh:

As a side note, you don't need to write this so often - I almost never use it, except where a local variable masks the name of the class level version:
C#
private string name;
public string Name {get { return name; } set { name = value; } }
public MyClass(string name)
   {
   this.name = name;
   }
The rest of the time there is no conflict, so the clear resolution of name to a class level variable is unneccessary:
C#
Console.WriteLine(this.name);
Is identical to:
C#
Console.WriteLine(name);
But the latter needs less typing and is quicker to read.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

GeneralRe: Query on Delegate Pin
ashish712-Jul-12 3:58
ashish712-Jul-12 3:58 
GeneralRe: Query on Delegate Pin
OriginalGriff12-Jul-12 4:11
mveOriginalGriff12-Jul-12 4:11 
GeneralRe: Query on Delegate Pin
DaveyM6912-Jul-12 4:32
professionalDaveyM6912-Jul-12 4:32 
AnswerRe: Query on Delegate Pin
Abhinav S11-Jul-12 21:53
Abhinav S11-Jul-12 21:53 
QuestionConfusion Pin
nitish_0711-Jul-12 21:02
nitish_0711-Jul-12 21:02 
AnswerRe: Confusion Pin
Abhinav S11-Jul-12 21:41
Abhinav S11-Jul-12 21:41 
AnswerRe: Confusion Pin
OriginalGriff11-Jul-12 21:41
mveOriginalGriff11-Jul-12 21:41 
AnswerRe: Confusion Pin
BillWoodruff14-Jul-12 15:17
professionalBillWoodruff14-Jul-12 15:17 
General[SOLVED] Adding Forms to TabControl Pin
AmbiguousName11-Jul-12 18:27
AmbiguousName11-Jul-12 18:27 
AnswerRe: Adding Forms to TabControl Pin
BobJanova11-Jul-12 23:29
BobJanova11-Jul-12 23:29 
GeneralRe: Adding Forms to TabControl Pin
AmbiguousName12-Jul-12 0:13
AmbiguousName12-Jul-12 0:13 
GeneralRe: Adding Forms to TabControl Pin
BobJanova12-Jul-12 4:05
BobJanova12-Jul-12 4:05 
GeneralRe: Adding Forms to TabControl Pin
AmbiguousName12-Jul-12 20:11
AmbiguousName12-Jul-12 20:11 
GeneralExposing a queue as a public property Pin
JoeRip11-Jul-12 17:44
JoeRip11-Jul-12 17:44 
GeneralRe: Exposing a queue as a public property Pin
PIEBALDconsult11-Jul-12 19:50
mvePIEBALDconsult11-Jul-12 19:50 
GeneralRe: Exposing a queue as a public property Pin
Abhinav S11-Jul-12 19:59
Abhinav S11-Jul-12 19:59 
GeneralRe: Exposing a queue as a public property Pin
BobJanova11-Jul-12 23:27
BobJanova11-Jul-12 23:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.