简体   繁体   中英

How to pass a Global variable into a class in C#

I am struggling to find a solution to pass a global variable into a class.

I've tried to create a public function that get the global variable's value it seem like I can't call that function from the class.

Below is my code:

    string latitude = null;
    string longitude = null;
    public string geturl() {
        return  latitude + "," + longitude;
    }

    public Form1() {
        Watcher = new GeoCoordinateWatcher();

        Watcher.StatusChanged += Watcher_StatusChanged;
        InitializeComponent();
        Watcher.Start();
        ChromiumWebBrowser a = new ChromiumWebBrowser();
        panel1.Controls.Add(a);
        a.Dock = System.Windows.Forms.DockStyle.Fill;
        a.Load("https://drive.google.com/open?id=12j590lCugajX1T64DHKcSiX9RwwBkAeF&usp=sharing");
        CefSettings settings = new CefSettings();
        BrowserLifeSpanHandler blsh = new BrowserLifeSpanHandler();
        a.LifeSpanHandler = blsh;      
    }

    public GeoCoordinateWatcher Watcher = null;

    private void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e) {
        if (e.Status == GeoPositionStatus.Ready){
            // Display the latitude and longitude.
            if (Watcher.Position.Location.IsUnknown){
                latitude = "Cannot find location data";
            } else {
                latitude = Watcher.Position.Location.Latitude.ToString();
                longitude = Watcher.Position.Location.Longitude.ToString();
            }
        }
    }

    public class BrowserLifeSpanHandler : ILifeSpanHandler {
        public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName,
            WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo,
            IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser) {               
            newBrowser = null;
            MessageBox.Show(targetUrl);
            ChromiumWebBrowser b = new ChromiumWebBrowser(targetUrl);
            return false;
        }

        public void OnAfterCreated(IWebBrowser browserControl, IBrowser browser) {
            //
        }

        public bool DoClose(IWebBrowser browserControl, IBrowser browser) {
            return false;
        }

        public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser) {
            //nothing
        }
    }

How can I pass the values of the string geturl() function into the BrowserLifeSpanHandler class?

C# does not support the concept of a "global variable" as you may know it for instance from javascript.

If you really need something like this (you should reconsider your design first), you can define a static class .

public static class MyGlobals {
    public static int p1 {get;set;}
    public static string p2 {get;set;}

    public static string getValue() {
        return "somevalue";
    }


}

Then you can access the property values and methods from anywhere in the code

int v = MyGlobals.p1;
string s = MyGlobals.getValue();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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