我有一個項目在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