I want to specify a global variable and read/write that variable in various contexts, do you guys know how I do this? because I want to read from various context I want to be able set this global var within the public static void Main(string[] args) and then read this, for example, from test2() below.

For example:

namespace Gathering_Tool
{
    public class Program
    {
        public string targetserver;

 public static void Main(string[] args)
        {
         this.targetserver = "whatever";
                     
        }
 public void Test2()
        {
         Console.writline(this.targetserver);
                     
        }
}
}

Welcome to Daniweb Steve! Please use code tags when posting code:

To answer your question I think you want to make the member as static:

namespace Gathering_Tool
{
public class Program
{
public static string targetserver;

public static void Main(string[] args)
{
targetserver = "whatever";

}
public void Test2()
{
Console.writline(targetserver);
}
}
} 

Please elaborate on your issue if a static member is not what you were after.

Edited 3 Years Ago by happygeek: fixed formatting

Wicked, thanks for the info that worked! I'm a pretty quick learning but fairly new to C#/OOP. Do you recommend a good book or a good site for me to read? For exmaple, I don't fully, obviously, understand the implications of settin static void as opposed to void so..! - So i'm prety basic as you can guess..!

Thanks
Steve

This question has already been answered. Start a new discussion instead.