【发布时间】: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