I'm making a "simple" two-form Windows app. In the first form, when a button is hit, I'm trying to instantiate the second form and pass a variable to it. That variable is valid (and a watch indicates no changes) until I get into the button_click routine, then suddenly it's null! All of the form1 code is in the same code module, same namespace, same partial class.

Hope that's clear. Any ideas why? Thanks!

Sorry I didn't get what you mean or where problem happens??!! please clarify.. thanks

I've got "private void bttnReport_Click(object sender, EventArgs e)" in the class code for form1. I have a public variable (wbWPR) elsewhere in that form1 code that is used successfully in other routines until that Click routine is hit. When it gets there, wbWPR is null, when it had had an actual value in it before. I've put a watchpoint on wbWPR which didn't show it being written to.

Sounds impossible. Can you post some code ? maybe there is s syntax error or something else.

Sounds impossible. Can you post some code ? maybe there is s syntax error or something else.

That's what I thought! Here is some code. I've marked with three asterisks the places to note (that I know of).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Globalization;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;

namespace WTGTrack
    public partial class frmWTGTrack : Form
        public frmWTGTrack()

        object m = Type.Missing;

        public int WTGs = 0;
        public Worksheet SNSheet = null;
        public Microsoft.Office.Interop.Excel.Workbook wbWPR = null;// *** THIS IS THE PROBLEM VARIABLE

        public Microsoft.Office.Interop.Excel.Application ExcelObj = null;
        public string OurPath = "";
        private string OurFile = "WPR.xls";
        private Microsoft.Office.Interop.Excel.Worksheets sheets = null;
        private string SheetName = "SERIAL NUMBERS";    // the source

        private void bttnSubmit_Click(object sender, EventArgs e)

        private void bttnReport_Click(object sender, EventArgs e)
            MessageBox.Show(wbWPR.Sheets.Count.ToString());// *** HERE IS WHERE WBWPR IS NULL!  WHY?

            //Form2 form2 = new Form2(wbWPR);


        private void frmWTGTrack_Load(object sender, EventArgs e)
            string s;

            ExcelObj = new Microsoft.Office.Interop.Excel.Application();
            if (ExcelObj == null)
                MessageBox.Show("Unable to start Excel");

            ExcelObj.Visible = true;

            OurPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase.Replace(@"file:\",""));

            Microsoft.Office.Interop.Excel.Workbook wbWPR = ExcelObj.Workbooks.Open(OurPath + "\\" + OurFile,m,m,m,m,m,m,m,m,m,m,m,m,m,m);
            if (wbWPR == null)
                MessageBox.Show("Unable to open " + OurFile);

            Microsoft.Office.Interop.Excel.Sheets sheets = wbWPR.Worksheets;
            SNSheet = null;
            int i = 1;
            while (i <= wbWPR.Sheets.Count)
            {   // find our source sheet and also the max number of WTGs
                if (i > wbWPR.Sheets.Count) break;
                SNSheet = (Microsoft.Office.Interop.Excel.Worksheet)wbWPR.Worksheets[i];
                if (SNSheet.Name == SheetName)
                    break;  // found our sheet

                if (SNSheet.Name == "Data")
                    s = SNSheet.get_Range(SNSheet.Cells[5, 3], SNSheet.Cells[5, 3]).Value2.ToString();
                    WTGs = Convert.ToInt32(s);

                SNSheet = null;
                i += 1;
            if (SNSheet == null)
                MessageBox.Show("Unable to find worksheet " + SheetName);

        private void frmWTGTrack_FormClosing(object sender, FormClosingEventArgs e)
            SNSheet = null;
            wbWPR = null;// *** NOT GETTING HERE, BUT WE SHOULDN'T BE
            ExcelObj = null;

        private void bttnExit_Click(object sender, EventArgs e)

I think I got this one fixed. As you may see, I'm a newbie to C#. Just created my first class and it seems to be working fine. Thanks!

Yes, I think I see your problem.
At the top of the file you are establishing the public Microsoft.Office.Interop.Excel.Workbook wbWPR = null variable.

Then later on, you do the same thing (again) in frmWTGTrack_Load.
Now what is happening, is you are creating a locally scoped variable in the frmWTGTrack_Load method. This means the one at the top is not being used, but rather the local variable is set. Once you leave the frmWTGTrack_Load method, that local variable goes away, and now other methods see the original, which was never changed from null to a value.

To correct the problem. Simply remove the type declaration from within the frmWTGTrack_Load method.

Change frmWTGTrack_Load method from:
Microsoft.Office.Interop.Excel.Workbook wbWPR = ExcelObj.Workbooks.Open...

wbWPR = ExcelObj.Workbooks.Open...

Have fun,

Thanks, Jerry, duh!