我有一个项目在C#winforms中,与一个文件称为:PublicSettings.cs
(此文件是在一个文件夹中称为:类),我有一个变量。使用变量从另一个文件.cs
现在,我想从同一个项目中的其他文件使用该变量。
PublicSettings.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LVSetup.Class
{
class PublicSettings
{
private string _ConnStr = "Connection";
public string ConnStr
{
get
{
return this._ConnStr;
}
set
{
this._ConnStr = value;
}
}
}
}
我想在文件中使用的变量ConnStr
:frmLogin.cs
frmLogin.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LVSetup.Class;
namespace LVSetup
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void btnEnter_Click(object sender, EventArgs e)
{
string a = PublicSettings.ConnStr;
}
}
}
但没有ConnStr
内PublicSettings
,只是(Equals and ReferenceEquals)
这里有什么问题?
问题是您试图从静态上下文中访问非静态变量 – DairyLea 2014-12-01 20:46:31
此问题似乎是无关紧要的,因为它是关于核心c#语法的,可以通过仔细阅读文档和教程轻松解决。 – walther 2014-12-01 20:47:35
@walther,这是一个伟大的downvote原因..不是一个很好的理由。 – paqogomez 2014-12-01 20:48:12