2 error abt variables
details: VS 2022, FILE: form1.cs
errors: ```Severity Code Description Project File Line Suppression State
Error (active) CS0236 A field initializer cannot reference the non-static field, method, or property 'Form1.savepath2' excelbetter C:\Users(user hidden for pivacy)\source\repos\excelbetter\excelbetter\Form1.cs 8Severity Code Description Project File Line Suppression State
Error (active) CS0236 A field initializer cannot reference the non-static field, method, or property 'Form1.workingFile' excelbetter C:\Users\nuh uh\source\repos\excelbetter\excelbetter\Form1.cs 9 using ClosedXML.Excel;
namespace excelbetter
{
public partial class Form1 : Form
{
public string savepath2 = @"C:\Users\nuh uh\excel" + "YIPPE.xlsx";
public XLWorkbook workingFile = new XLWorkbook(savepath2);
public IXLWorksheet spreadsheet1 = workingFile.AddWorksheet("s");
public Form1()
{
InitializeComponent();
}
public void process_Data_Click(object sender, EventArgs e)
{
Close();
}
}
}
```
7 Replies
well, as said - your field initializers cant access the members they are trying to
you need to move that code to the constructor
huh
initializers are limited in what they can do. they cant access other non-static members, since those might not have been initialized yet
its a chicken and egg type thing
so you need to initialize them inside your constructor instead
so define them as what they are?
yes, feel free to define them there
but give them their value in the ctor
so first define like
XLWorkbook (varname);
then
XLWorkbook (varname) = ("BLEH")
something like that, yeah