简体   繁体   English

无法从另一个类C#调用变量

[英]Unable to call a variable from another class c#

I have the following class in my c# application: 我的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());
        }
    }
}

Where sessId, server and sessName all have appropriate values. 其中sessId,server和sessName都具有适当的值。 In my Form1.Designer, I want to call these details to display on the form (the text for button1): 在我的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);

However 然而

But I am seeing this error: The type or namespace name 'sessName' does not exist in the namespace 'Citrix_Killer' (are you missing an assembly reference?) 但是我看到此错误:名称空间'Citrix_Killer'中不存在类型或名称空间名称'sessName'(您是否缺少程序集引用?)

This also fails when using just sessName - can someone please point me in the right direction? 当仅使用sessName时,这也会失败-有人可以指出正确的方向吗?

Many thanks 非常感谢

The solution is to pass the required values to Form1 when creating it. 解决方案是在创建Form1时将所需的值传递给Form1 For example, assuming you want access to sessID , server and sessName , change Program.cs : 例如,假设您要访问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));
        }
    }
}

And change Form1.cs to accept the values in its constructor: 并更改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();
        ...
    }

Then you can reference them in your initialisation code: 然后,您可以在初始化代码中引用它们:

    // 
    // 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM