Object reference is required

D

DC

I have created the following abstract class:

public abstract class BaseC
{
protected int _ID;
protected string _name;

protected abstract DataTable GetData(int ID);
// intended to hit data access layer

protected static string GetValue(int ID)
{
DataTable DT = new DataTable();
DT = GetData(ID);

if (DT.Rows.Count == 1)
{
return DT.Rows[0]["name"].ToString();
}
else
{
return "";
}
}

}

I have also created the following derived class

public class DerivedC : BaseC
{

protected override DataTable GetFields(int ID)
{
DAL.List listDAL = new DAL.List();
listDAL.ID = ID
return listDAL.SelectOne();
}
}

As you can see the SetFields method in BaseC will be
inherited and the GetFields method will be overridden. I
am getting the following error:

"An object reference for the nonstatic field, method, or
property 'NameSpace.BaseC.GetFields(int)"

The GetFields(int) method is called from SetFieds(int) in
BaseC; however, the GetFields(int) method will be
overridden by the derived class so that the DAL can be
accessed.

Does anybody have any ideas on how to fix this?
 
R

Richard Cook

i assume you mean GetData() rather than GetFields(). The problem is that
GetValue() is static, ie a class method rather than an instance method; you
can't call the instance method GetData from a static context (there's no
"this").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top