C# 无法从另一个类c调用变量#
我的c#申请表中有以下类别:C# 无法从另一个类c调用变量#,c#,variables,.net-3.5,public,C#,Variables,.net 3.5,Public,我的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. ///
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());
}
}
}
然而
但我看到了这个错误:名称空间“Citrix_Killer”中不存在类型或名称空间名称“sessName”(是否缺少程序集引用?)
这在使用sessName时也会失败-有人能给我指出正确的方向吗
非常感谢解决方案是在创建表单时将所需的值传递到表单1。例如,假设您希望访问
sessiond
、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));
}
}
}
然后,您可以在初始化代码中引用它们:
//
// 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
。例如,假设您希望访问sessiond
、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));
}
}
}
然后,您可以在初始化代码中引用它们:
//
// 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
。例如,假设您希望访问sessiond
、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));
}
}
}
然后,您可以在初始化代码中引用它们:
//
// 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
。例如,假设您希望访问sessiond
、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));
}
}
}
然后,您可以在初始化代码中引用它们:
//
// 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);
sessName不是ProgramMake sessName public by
public sessName=…
sessName
的公共属性,它仅在Program.Main()的范围内可用。如果要从另一个类访问它,请将其声明为类级变量并将其公开。解决方案是通过其构造函数将所需的值传递给Form1
。请参阅我的答案以了解更多详细信息。sessName不是ProgramMake sessName的公共属性通过public sessName=…
sessName
仅在Program.Main()的范围内可用。如果要从另一个类访问它,请将其声明为类级变量并将其公开。解决方案是通过其构造函数将所需的值传递给Form1
。请参阅我的答案以了解更多详细信息。sessName不是ProgramMake sessName的公共属性通过public sessName=…
sessName
仅在Program.Main()的范围内可用。如果要从另一个类访问它,请将其声明为类级变量并将其公开。解决方案是通过其构造函数将所需的值传递给Form1
。请参阅我的答案以了解更多详细信息。sessName不是ProgramMake sessName的公共属性通过public sessName=…
sessName
仅在Program.Main()的范围内可用。如果要从另一个类访问它,请将其声明为类级变量并将其公开。解决方案是通过其构造函数将所需的值传递给Form1
。请看我的答案了解更多细节。谢谢David,解释得很好,这是一个很好的例子。如果有机会,请告诉我为什么在Form1类中将变量设置为私有只读?Thanks@basijan,因为这些值是在应用程序启动时计算的,并通过构造函数传递给对象,所以它们不应该更改。因此,通过使它们成为readonly
来强化这一事实是一个好主意。如果以后需要对它们进行更改,编译器将防止在Form1
中对它们进行可能的破坏性更改,并鼓励您重新编写解决方案,以正确处理它们在运行时更改的需要。感谢David,这是一个很好的解释和示例。如果有机会,请告诉我为什么在Form1类中将变量设置为私有只读?Thanks@basijan,因为这些值是在应用程序启动时计算的,并通过构造函数传递给对象,所以它们不应该更改。因此,通过使它们成为readonly
来强化这一事实是一个好主意。如果以后需要对它们进行更改,编译器将防止在Form1
中对它们进行可能的破坏性更改,并鼓励您重新编写解决方案,以正确处理它们在运行时更改的需要。感谢David,这是一个很好的解释和示例。如果有机会,请告诉我为什么在Form1类中将变量设置为私有只读?Thanks@basijan,因为这些值是在应用程序启动时计算的,并通过构造函数传递给对象,所以它们不应该更改。因此,通过使它们成为readonly
来强化这一事实是一个好主意。如果以后需要对它们进行更改,编译器将防止在Form1
中对它们进行可能的破坏性更改,并鼓励您重新编写解决方案,以正确处理它们在运行时更改的需要。感谢David,这是一个很好的解释和示例。如果有机会,请告诉我为什么在Form1类中将变量设置为私有只读?Thanks@basijan,因为这些值是在应用程序启动时计算的,并通过构造函数传递给对象,所以它们不应该更改。因此,通过使它们成为readonly
来强化这一事实是一个好主意。如果以后需要对它们进行更改,编译器将防止在Form1
中对它们进行可能的破坏性更改,并鼓励您重新编写解决方案,以正确处理它们在运行时更改的需要。