tags:

views:

16

answers:

1

Hi, I have a class:

public abstract class AbstractDBConnector
{
    private AdServiceDB db;

    public AdServiceDB Adapter
    {
        get 
        {
            if (db == null) db = new AdServiceDB();
            return db;
        }
    }
}

and a class that inherits from it:

public class BaseDataValidator : AbstractDBConnector
{
    public static bool Check()
    {
        var t = Adapter.Users.Where(x=>x.Id<10).ToList(); //the error is here
        return true; //example
    }
}

this code obviously generates an error: An object reference is required for the non-static field, method, or property Is it even possible to do a trick to use the Adapter in the static method ?

+1  A: 

Only if Adapter is also static, which you probably don't want it to be (but maybe you do, I'm not sure what the exact use case is, there's not enough info). Pass the adapter to the method as a parameter if the method must be static, but it seems more likely that your method just shouldn't be static in the first place.

EDIT: note that for the "make it static approach to work you'll have to make both Adapter and db static.

Donnie
OK, but if I set Adapter as a static property, then (IMO) it shouldnt be used in a Web project (in the nearest foture it will be).
Tony
Yes. That's why I started off stating that that was probably a bad idea, and it takes you back to my original suggestion of either having the method not be static at all or passing the adapter to it.
Donnie