Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...