0
private void Button_Click(object sender, RoutedEventArgs e)
{
    IWebDriver driver = new ChromeDriver
    {
        Url = filename
    };
    driver.Manage().Window.Maximize();
    Watcher_Changed(driver);
}

private void Watcher_Changed(object sender, FileSystemEventArgs e)
{
    driver.navigate().refresh(); // Can not use driver
}

I tried to use driver in another method using the above code but it does not work, what can I do to make it work?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Daniell
  • 59
  • 6

3 Answers3

3

You create driver as a local variable inside a method, this will only be accessible inside this method. To be a little more precise it is actually visible in the scope it is defined in, you should definitely read on that.

To make driver accessible to all functions you should either pass it around or better create it inside of the class your methods are in.

cozmic
  • 178
  • 2
  • 7
2

You can use sender. Something like this:

private void Watcher_Changed(object sender, FileSystemEventArgs e)
{
    var driver = sender as IWebDriver;//Or sender as ChromeDriver
    driver.navigate().refresh(); 
}

You need however specify the FileSystemEventArgs as your second parameter. For example:

Watcher_Changed(driver , null);
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
-3

I'm not sure but you can try using this

  1. define driver in global scope

What you are doing is defining and declaring driver variable inside the scope of button click.so,it will not be accessible from any other method...

  1. Secondly,if you are making a function call within the button click event then use its function parameters. Like in watcher_changed method you can try using this

assuming you are calling watcher_changed function as watcher_changed(driver,"") else it will give you error

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

    namespace MyStuff
    {
        public class MyClass : Page
        {
            **`define your IwebDriver here`**

           public void MyButton_Click(Object sender, EventArgs e)
           {
               **access IwebDriver here**
           }

           private void Watcher_Changed(object sender, FileSystemEventArgs e)
           {
               sender.navigate().refresh(); // Can not use driver
           }
         
       }
    }
primo
  • 1,340
  • 3
  • 12
  • 40
  • There is no "global scope" in C#. – Enigmativity May 27 '18 at 11:04
  • I mean to say outside the button click event method – primo May 27 '18 at 11:06
  • Could you please clean up your answer? The formatting is terrible. It's hard to read like it is. – Enigmativity May 27 '18 at 11:06
  • using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace MyStuff { public class MyClass : Page { protected System.Web.UI.WebControls.Label MyLabel; protected System.Web.UI.WebControls.Button MyButton; protected System.Web.UI.WebControls.TextBox MyTextBox; **// define your IWebDriver here** public void MyButton_Click(Object sender, EventArgs e) { MyLabel.Text = MyTextBox.Text.ToString(); } } } – primo May 27 '18 at 11:13
  • 1
    What's that? Please don't put code in comments - edit your answer instead. – Enigmativity May 27 '18 at 11:15