I’ve started building a project and I tried to make a method which would output data from a string variable into a tab page on a form application. I am trying to keep my code as organised as possible so I tried to make a method to use to output the data. The issue is, no matter where I have the method. Whether it’s in a different class or in the opening class I still can’t reference the control from my custom method. What is it that I’m missing to make it work?


If you need any further information just ask, thanks in advance.

This code is taken from the initial class on the initial namespace:
private void msiFileSaveSaveas_Click(object sender, EventArgs e) //Button press to save data
{
sfdFileBrowser.ShowDialog();
if (sfdFileBrowser.FileName != "")
{
Globals.FileLocation = sfdFileBrowser.FileName;
}
sfdFileBrowser.FileName = "";
FileFunctions.SaveFile();
tabWorkspace.TabPages.Add(Globals.FileName); //working control reference
}

public static void PasteData //custom method (public static void PasteData()) *Fix*
{
tabWorkspace.TabPages.Add(Globals.FileName); //not working control reference
}

Edit: I have solved the issue. When I declared the method within the same class I did not have open/close brackets at the end. This has solved it for within the same class. After researching I believe it is not suggested, and not very possible to do it from another class.
no comments yet
Be the first to share what you think!
C# devs
null reference exceptions

source