Bassie
Bassie

Reputation: 10400

Unable to call a variable from another class c#

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

Where sessId, server and sessName all have appropriate values. In my Form1.Designer, I want to call these details to display on the form (the text for 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?)

This also fails when using just sessName - can someone please point me in the right direction?

Many thanks

Upvotes: 0

Views: 105

Answers (1)

David Arno
David Arno

Reputation: 43264

The solution is to pass the required values to Form1 when creating it. For example, assuming you want access to sessID, server and sessName, change 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:

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

Upvotes: 3

Related Questions