[英]public static property won't show in intellisense
I'm slowly learning C# and bit confused about the code. 我正在慢慢学习C#,并对代码有些困惑。 I have here a simplified version of my class for the database connection settings
我这里有我的课程的简化版本,用于数据库连接设置
public class DatabaseSettings
{
private static string connectionString = string.Empty;
public static string ConnectionString
{
get
{
return connectionString;
}
private set
{
connectionString = value;
}
}
public bool TestConnection()
{
bool _returnVal = false;
string _connectionString = String.Format("Data Source={0};Persist Security Info=False", databaseLocation);
using (SqlCeConnection connection = new SqlCeConnection(_connectionString))
{
try
{
connection.Open();
connectionString = _connectionString; // sets the value of the connection string here
_returnVal = true;
}
catch (SqlCeException e)
{
// other codes here
_returnVal = false;
}
finally
{
connection.Close();
}
}
return _returnVal;
}
// other methods here
}
now on my Main Class
, I am confused what is happening here. 现在在我的
Main Class
,我很困惑这里发生的事情。 When i tried this code: 当我尝试此代码时:
string _databaseLocation = "database path";
DatabaseSettings _dbaseSetting = new DatabaseSettings(_databaseLocation, true);
_dbaseSetting.TestConnection();
string newConnectionString = _dbaseSetting.ConnectionString;
// ^ i got an error here
// Member 'SQLCE_Sample.ClassList.DatabaseSettings.ConnectionString.get'
// cannot be accessed with an instance reference;
// qualify it with a type name instead
this one below works without an error but the problem is I got an empty string: 下面的这个工作正常,没有错误,但是问题是我得到了一个空字符串:
string _databaseLocation = "database path";
DatabaseSettings _dbaseSetting = new DatabaseSettings(_databaseLocation, true);
string newConnectionString = DatabaseSettings.ConnectionString;
What I really want is I need to create a class that has a public method
which tests the connection from the application to the database . 我真正想要的是创建一个具有
public method
的类,该public method
测试从应用程序到数据库的连接 。 Inside that method contains a syntax that sets the value of the connection string . 该方法内部包含设置连接字符串值的语法。 Then I need also a
property
that retrieves the value of the connection string without instantiating the class ( that's why i added the static
keyword ). 然后,我还需要一个
property
,该property
无需实例化类即可检索连接字符串的值 ( 这就是我添加static
关键字的原因 )。 How can I possibly to this? 我怎么可能呢?
It appears you want to access (statically) the value of a property that is set by an instance of your class. 看来您要访问(静态地)由类的实例设置的属性的值。 So, all you need to do is listen to the compiler error -- access the property as a static reference rather than an instance reference.
因此,您所需要做的就是监听编译器错误-将属性作为静态引用而不是实例引用进行访问。 Change:
更改:
string newConnectionString = _dbaseSetting.ConnectionString;
To: 至:
string newConnectionString = DatabaseSettings.ConnectionString;
You'll of course need to make sure that you call TestConnection
on the instance before you ever try to access the propery statically like this; 当然,在尝试像这样静态访问
TestConnection
之前,您需要确保在实例上调用TestConnection
。 otherwise ConnectionString
will be null. 否则,
ConnectionString
将为null。
Your public static variable and static property have same name. 您的公共静态变量和静态属性具有相同的名称。 Rename either of them.
重命名它们之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.