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

Recommended Answers

All 3 Replies

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.

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

I recommend www.codeproject.com for good articles on how to do certain tasks but static vs instance is a basic concept for OOP. Basically static variables are shared across the entire application, where instance variables belong to a single instance.

This thread explains static vs instance:
http://www.daniweb.com/forums/thread96978.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.