无法从另一个 class c# 调用变量
Unable to call a variable from another class c#
我的 C# 应用程序中有以下 class:
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);
但是
但我看到这个错误:类型或命名空间名称 'sessName' 在命名空间 'Citrix_Killer' 中不存在(您是否缺少程序集引用?)
仅使用 sessName 时也会失败 - 谁能给我指出正确的方向?
非常感谢
解决办法是在创建的时候将需要的值传给Form1
。例如,假设您想要访问 sessID
、server
和 sessName
,请更改 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);
我的 C# 应用程序中有以下 class:
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);
但是
但我看到这个错误:类型或命名空间名称 'sessName' 在命名空间 'Citrix_Killer' 中不存在(您是否缺少程序集引用?)
仅使用 sessName 时也会失败 - 谁能给我指出正确的方向?
非常感谢
解决办法是在创建的时候将需要的值传给Form1
。例如,假设您想要访问 sessID
、server
和 sessName
,请更改 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);