【问题标题】:Unable to call a variable from another class c#无法从另一个类 c# 调用变量
【发布时间】:2016-02-04 18:28:17
【问题描述】:

我的 c# 应用程序中有以下类:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Citrix_Killer
{
    public static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        public static void Main()
        {
        string name = Myfunc.userName();
        List<string> servers = Myfunc.get_Servers();
        string[] session = Myfunc.get_Session(servers, name);

        string sessID = session[0];
        string server = session[1]; 
        string sessName = session[2];  

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
        }
    }
}

其中 sessId、server 和 sessName 都有适当的值。 在我的 Form1.Designer 中,我想调用这些详细信息以显示在表单上(button1 的文本):

        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(12, 12);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 0;
        this.button1.Text = Program.sessName;
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);

然而

但我看到此错误:命名空间“Citrix_Killer”中不存在类型或命名空间名称“sessName”(您是否缺少程序集引用?)

仅使用 sessName 时也会失败 - 有人可以指点我正确的方向吗?

非常感谢

【问题讨论】:

  • sessName 不是 Program 的公共财产
  • 通过public sessName = ..公开sessName
  • sessName 仅在 Program.Main() 范围内可用。如果您想从另一个类访问它,请将其声明为类级变量并将其公开。
  • 解决方案是通过其构造函数将所需的值传递给Form1。详情请看我的回答。

标签: c# variables .net-3.5 public


【解决方案1】:

解决方案是在创建时将所需的值传递给Form1。例如,假设您要访问sessIDserversessName,则更改Program.cs

namespace Citrix_Killer
{
    public static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        public static void Main()
        {
            ...
            Application.Run(new Form1(sessID, server, sessName));
        }
    }
}

并更改Form1.cs 以接受其构造函数中的值:

public partial class Form1 : Form
{
    private readonly string _sessId;
    private readonly string _server;
    private readonly string _sessName;

    public Form1(string sessId, string server, string sessName)
    {
        _sessId = sessId;
        _server = server;
        _sessName = sessName;
        InitializeComponent();
        ...
    }

然后你可以在你的初始化代码中引用它们:

    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(12, 12);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(75, 23);
    this.button1.TabIndex = 0;
    this.button1.Text = _sessName;
    this.button1.UseVisualStyleBackColor = true;
    this.button1.Click += new System.EventHandler(this.button1_Click);

【讨论】:

  • 谢谢大卫,很好的解释和一个很好的例子。如果您有机会,请告诉我您为什么在 Form1 类中将变量设置为私有只读?谢谢
  • @basijan,因为这些值是在应用程序启动时计算并通过构造函数传递给对象的,所以它们不应该改变。因此,通过将它们设为readonly 来强化这一事实是一个好主意。如果以后需要更改它们,编译器将防止在 Form1 内对它们进行可能的重大更改,并鼓励您重新设计解决方案以正确处理它们在运行时更改的需求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 2013-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多