Event handler probs.


E

Enkidu

I've not much experience of C#, so maybe someone can help me with this
little problem?

I've discovered that an event handler seems to be able to call methods
in its own class, but not in other classes even if they are 'in scope'
(if that is the correct term).

For instance:

private void button1_Click(object sender, EventArgs e)
{
label1.Text = "Do nothing!";
Program.pgm.doNothing();
}

If the 'doNothing' line is commented the method changes the text of the
label on clicking the button. If the 'doNothing' line is included the
program compiles but when the method is executed (as evidenced by
stepping through it), but on return the whole application closes, with
no errors. What am I missing?

Cheers,

Cliff
 
Ad

Advertisements

E

Enkidu

Exactly what it says. There was originally no code between the open and
closing brackets, It now contains a bit of meaningless code!

public int doNothing()
{
int i = 3;
i++;
return i;
}

Cheers,

Cliff
 
Ad

Advertisements

E

Enkidu

Ah me! When I am debugging I often keep a 'doNothing' method around to
basically replace another method if I want to temporarily disable it. It
turns out that the 'doNothing' method actually works fine, but the
method it was standing in for demonstrates my problem. This indeed
points to a problem with my code in the original routine. Sorry for my
confusion.

The mystery is that I can step through the original method code (in C#
Express edition) and does exactly what I accused the 'doNothing' routine
of doing, in that it appears to 'return' normally to the event handler,
but when the event handler exits, the application closes (via a
'Dispose'). There appears to be no 'unhandled exception' or if there is,
I do not see it.

Sorry to have troubled you. Looks like I need to dig a little deeper.

Cheers,

Cliff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top