我在C#控制台应用程序中有以下代码段。
namespace backupSystemAdministration
{
class backupSystem {
static void Main(string[] args) {
backupSystemaccess obj = new backupSystemaccess();
string input = filepath.Split(',')[0];
}
private static void addbackup(string line) {
backupSystemaccess.wordval = line; //error
}
}
public class backupSystemaccess {
public static string _word;
public static string wordval {
get {
return _word;
}
set {
_word = value;
}
}
}
}
我无法从类backupSystem设置backupSystemaccess的属性。在 backupSystemaccess.wordval = line
行中收到错误答案 0 :(得分:3)
属性wordval
也需要public
,因为wordval
是静态的,你也应该将字段_word
设为静态。同样基于Naming Guidelines
,您最好将wordval
属性重命名为Wordval
:
static string _word;
public static string Wordval
{
get
{
return _word;
}
set
{
_word = value;
}
}
或者您可以改为使用Auto-Implemented Properties
:
public static string Wordval { get; set; }
答案 1 :(得分:0)
您将wordval
定义为static
,但_word
不是。{0}}。我不知道你想要实现什么,但 无法正常工作。
和
backupSystemaccess.wordval
说你不希望这是静态的。
另一方面,我不理解你的行
backupSystemaccess = new backupSystemaccess();
也许你应该尝试更好地理解OOP。
编辑:
现在您正在访问该属性wordval
,但当然它无法设置_word
,因为它不知道"知道"什么(以及内存中的位置)_word
是因为可以有尽可能多的_word
,但静态方法是唯一的。