an object reference is required to access non-static field method or property c #

1

I have a private method that returns me as a result of the execution of a data set.

What I want to do is reference in this method a textBox that I have in the main button of my Windows Form application. For the user to manually enter the 'Caption', this new caption will be replaced by the one already in place.

The textBox I can call it without problems to patir of the button1_click but not from my method.

I get the error an object reference is required to access non-static field method or property Here is the method in which I am trying to call textBox4.

How could I do that?

static private string GetMappingTable()
{
    string SourceID = "";

    var ds = GetMappingTable();
    foreach (DataTable dst in ds.Tables)
    {
        foreach (DataRow dr in dst.Rows)
        {
             //Aqui me aparece el error
            if (textBox4.Text != "");
            var DataSourceId = ds.Tables["Table"]
            .Select("Caption = 'testUSers'")
            .Select(r => r["SourceID"]) 
            .Where(s => s != DBNull.Value)
            .Select(s => s.ToString()) 
            .FirstOrDefault();

            SourceID = DataSourceId;
             //Aqui me aparece el error
            SourceID = textBox4.Text;
        }
    }
    return SourceID;
}
    
asked by A arancibia 07.04.2016 в 21:05
source

1 answer

2

The problem is that you have marked your method as static

static private string GetMappingTable()

Static methods do not have access to the instance fields since they do not belong to any.

Simply remove the keyword static

    
answered by 07.04.2016 / 21:21
source