【问题标题】:Cant access a public variable from a public class anywhere in my solution (VS, C'#)无法从我的解决方案中的任何地方的公共类访问公共变量(VS,C'#)
【发布时间】:2014-09-30 10:47:44
【问题描述】:

我已经接管了一个项目,我正在努力从公共类访问公共变量。 (奇怪)。

我对 C# 比较陌生,只做过一些 MVC、ASP.NET 的东西,所以我不知道我是不是有点像小丑,但是:

public int AlertCount
{
    get { return Convert.ToInt32(alertCountTextBlock.Text); }
    set { alertCountTextBlock.Text = value.ToString(); }
}

/// <summary>
/// Property which represents the number of new alerts the project currently has 
/// </summary>
public int NewAlertCount
{
    get { return Convert.ToInt32(newAlertCountTextBlock.Text); }
    set { newAlertCountTextBlock.Text = value.ToString(); }
}

这是我试图从我的解决方案中的另一个类访问的两个变量(上图)

namespace Intelligence_Gathering_System.Pages.Project.Controls
{
    /// <summary>
    /// UserControl which acts as a selection button for each project. It displays the projects name and
    /// current alert item totals along with providing methods for sharing and selecting of the project
    /// </summary>
    public partial class ProjectHeaderControl 
    {
        private readonly ProjectPage _parentReference;                    //Reference to the controls 'parent' Project Page       

        /// <summary>
        /// Overloaded constructor which initialises the controls members
        /// </summary>
        /// <param name="projectName">The name of the project</param>
        /// <param name="alertCount">The total number of Alert Items in the project</param>
        /// <param name="newAlertCount">The total numebr of New Alert Items in the project</param>
        /// <param name="parent">A reference to the controls 'parent' Project Page</param>
        public ProjectHeaderControl(string projectName, int alertCount, int newAlertCount, ProjectPage parent)
        {

这是变量所在的类结构(命名空间、部分类和构造函数),

我只是试图从另一段代码(在同一解决方案和项目中)调用以更改计数以递增和递减

我已经尝试在我需要递增和递减的类中(有多个类我需要这样做)放置完整的命名空间路径,然后是变量以精确定位它和类。变量(如下所示)

int x = Intelligence_Gathering_System.Pages.Project.Controls.NewAlertCount;

int x = ProjectHeaderControl.NewAlertCount;

他们都没有工作,我有点困惑为什么。

我在这里遗漏了什么明显的东西还是.... 是否与 C# 相关的语法我不确定。

任何帮助将不胜感激。

问候

约旦

【问题讨论】:

  • 当然! @Patrick Developer,有时间!谢谢 :)
  • 没问题,Jon Skeet 的回答应该被接受 :)

标签: c# visual-studio variables constructor public


【解决方案1】:

NewAlertCountclass 中的一个属性 - 但我们不知道该类是什么。 (也许是ProjectHeaderControl - 你的描述有点难以理解。)

Intelligence_Gathering_System.Pages.Project.Controls 是一个命名空间。命名空间不能声明属性。

您需要指定包含属性的 - 它们要么需要是静态属性,要么您需要从实例中获取属性> 班级。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 2020-03-27
    相关资源
    最近更新 更多