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!
Matt

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.

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()
        {
            InitializeComponent();
        }

        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)
        {
	// *** THIS IS A BUNCH OF CODE THAT DOES NOT REFERENCE WBWPR
        }

        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);

            //form2.ShowDialog();
        }

        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.Quit();
            }

            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);
	// *** THIS HAPPENS FIRST - WBWPR IS CORRECT HERE
            if (wbWPR == null)
            {
                MessageBox.Show("Unable to open " + OurFile);
                ExcelObj.Quit();
            }

            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);
                ExcelObj.Quit();
            }
        }

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

        private void bttnExit_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Application.Exit();
        }
    }
}

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...

TO:
wbWPR = ExcelObj.Workbooks.Open...


Have fun,
Jerry

This article has been dead for over six months. Start a new discussion instead.