我想使用构造函数为我的类建立对象。问题是构造函数正确执行(在 Visual Studio 调试器中遍历构造函数中的每个赋值),但是在构造函数完成并建立对象后,我不能使用我的任何类的方法来访问数据成员。
构造函数上方列出的数据成员与构造函数内部分配的数据成员之间似乎存在脱节。
发布的错误是:“NullReferenceException Unhandled - 对象引用未设置为对象的实例。”
...
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.IO;
namespace ExcelManip
{
class ExcelInterop
{
//MEMBERS
private Application _excelApp;// = new Application();
private Workbooks books;
private Workbook workBook;
//CONSTRUCTOR
public ExcelInterop(string thisFileName)
{
Application _excelApp = new Application();
Workbooks books = _excelApp.Workbooks;
Workbook workBook = books.Open(thisFileName,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
}