无法访问静态上下文中的非静态字段[fieldname] [英] Cannot access non-static field [fieldname] in static contex

查看:52
本文介绍了无法访问静态上下文中的非静态字段[fieldname]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基类:

public class DatabaseBase
{
  private readonly string connectionString;
  private bool useCounters;

  public DatabaseBase(string connectionString)
  {
   this.connectionString = connectionString;
  }

  public DatabaseBase(...)
  {
   connectionString = ...;   
  }

  public DatabaseBase(..)
  {
   connectionString = string.Format(...);
  }

  public string ConnectionString
  {
   get { return this.connectionString; }
  }

...

派生类:

public class ProjectDB : DatabaseBase
{
private bool useServiceConnection;

 private static string ConnectionString
 {
     get
     {
        string connectionString = useServiceConnection == true ? ConfigurationManager.AppSettings["SomeConnection1"] : ConfigurationManager.AppSettings["SomeConnection2"];
         return connectionString;
     }
 }


public ProjectDB() : this(false)
{
}

 private bool isServiceCall;

public ProjectDB(bool useServiceConnection)
    : base(ConnectionString)
{
    this.useServiceConnection = useServiceConnection;
}

private SqlConnection CreateConnection()
{
    return new SqlConnection(ConnectionString);
}

此行出现错误无法在静态上下文中访问非静态字段useServiceConnection":

I'm getting the error "Cannot access non-static field useServiceConnection in static context" for this line:

string connectionString = useServiceConnection == true ? ConfigurationManager.AppSettings["SomeConnection1"] : ConfigurationManager.AppSettings["SomeConnection2"];

但是,如果我将useServiceConnection设置为静态var以满足要求,那么在构造函数中会出现相同的错误:

However if I make useServiceConnection a static var to satisfy the quirement, then I get that same error here in the constructor:

public ProjectDB(bool useServiceConnection)
    : base(ConnectionString)
{
    this.useServiceConnection = useServiceConnection;
}

现在,如果我将useServiceConnection和ConnectionSting属性设为非静态,那么我在这里得到构造函数的错误:

Now if I make useServiceConnection and ConnectionSting property non-static, then I get that error for the constructor here:

public LitleDB(bool useWebServiceConnection)
    : base(ConnectionString)
{
    this.useWebServiceConnection = useWebServiceConnection;
}

我认为我理解的前2个.

I think the first 2 I understand.

但是现在更新下面的示例,为什么在这种情况下构造函数仍会给我一个错误?这些不再是静态的,那么静态上下文是从哪里来的呢?所以这就是我现在所拥有的:

But now with the example updated below, why would the constructor in this case give me an error still? Those are no longer static so where's the static context being expected from? So here's what I have now:

public class ProjectDB : DatabaseBase
 {
    private bool useServiceConnection; <-- NO LONGER STATIC

     private new string ConnectionString <-- NO LONGER STATIC
     {
         get
         {
            string connectionString = useServiceConnection == true ? ConfigurationManager.AppSettings["SomeConnection1"] : ConfigurationManager.AppSettings["SomeConnection2"];
             return connectionString;
         }
     }

    public ProjectDB() : this(false)
    {
    }

     private bool isServiceCall;

    public ProjectDB(bool useServiceConnection)
        : base(ConnectionString)  <--- IT'S COMPLAINING HERE NOW, SO WHERE IS IT TRYING TO ACCESS STATICALLY?  I DON'T GET WHY
    {
        this.useServiceConnection = useServiceConnection;
    }

我在此类中有其他静态属性,这与它有关系吗?我没有用它们.

I have other static properties in this class, does that have anything to do with it? I'm not using them though.

推荐答案

变量 useServiceConnection 不是 static ,因此它仅存在于类实例的上下文中.由于属性 ConnectionString 是静态的,因此它在实例的上下文中不存在,因此它不能看到"实例成员.您需要将 ConnectionString 设置为非静态,或者将 useServiceConnection 设置为静态.

The variable useServiceConnection is not static so it only exists within the context of an instance of the class. Since the property ConnectionString is static, it does not exist in the context of an instance, and so it cannot "see" instance members. You will need to either make ConnectionString not being static, or make useServiceConnection static.

这篇关于无法访问静态上下文中的非静态字段[fieldname]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆