0

I have a Form which has code similar to this:

public partial class Form1 : Form
{
    private int m_var1;
    private int m_var2;
    string sMsg;
    bool bReturn;

    private bool MyFunction()
    {
        // POINT A: at this point m_var1 and m_var2 are both 100            
        sMsg = "Test Message";
        bReturn = (DialogResult.Yes == MessageBox.Show(sMsg, "MyApp",MessageBoxButtons.YesNo, MessageBoxIcon.Question));
        // POINT B: at this point m_var1 and m_var2 are both 0
    }
}

Why at POINT B have m_var1 and m_var2 both changed to 0 as I am experiencing?

CJ7
  • 22,579
  • 65
  • 193
  • 321
  • 11
    Please provide a small but complete sample project that reproduces this problem. This is no normal behavior and your question doesn't contain enough information to answer it. – Daniel Hilgarth Oct 15 '12 at 10:47
  • 1
    Are the variables' values by any chance influenced by another thread? – waldrumpus Oct 15 '12 at 10:47
  • Please give the code where your assigning the values to m_var1 and m_var2 or add full function code(MyFunction) – andy Oct 15 '12 at 10:48
  • @DanielHilgarth: that is not feasible at the moment. Is there some way I can debug what is happening at the `MessageBox.Show` line? – CJ7 Oct 15 '12 at 11:23
  • 1
    @CJ7: `MessageBox.Show` surely has nothing to do with this phenomen. – Daniel Hilgarth Oct 15 '12 at 11:25
  • @DanielHilgarth: well then what is it then? It happens only after that line. It cannot be anything other than something that happens between Point A and Point B. – CJ7 Oct 15 '12 at 11:28
  • @CJ7: `MessageBox.Show` doesn't change your variables. Period. That's why I was asking for more code. You might have a thread that manipulates some data, but it really is impossible to say. – Daniel Hilgarth Oct 15 '12 at 11:30
  • @DanielHilgarth: can I have a debugger running in the background to see what is happening? – CJ7 Oct 15 '12 at 11:33
  • 1
    @DanielHilgarth: using Carra's answer below I was able to find out that the Form.Activated event was resetting the variables. The MessageBox was causing the form to activate again! – CJ7 Oct 15 '12 at 12:16

1 Answers1

2

This sounds like a thread issue. I can explain how I would try to solve this problem:

  • Change m_var1 and m_var2 to Properties.
  • Set a breakpoint to their setters.

Check the stacktrace of the code that hit the setter.

Carra
  • 17,808
  • 7
  • 62
  • 75
  • in visual studio you can have the debugger stop when any symbol is assigned a value. Ie you can set a break point when m_var1 is assigned, so you don't technically need to change them to properties – Rune FS Oct 15 '12 at 11:43
  • @CJ7 sorry won't help you since it doesn't work for managed code at least not according to http://stackoverflow.com/questions/160045/visual-studio-debugger-break-when-a-value-changes – Rune FS Oct 15 '12 at 11:55