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?

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

@rene I have passed the null as it's second parameter, just to make the code compile-able.
Sorry I am new to this, how can I specify that driver is a new ChromeDriver as well using your code?
@S.Akbari you can pass EventArgs.Empty too there, it is preferable read this
@Daniell use ChromeDriver like this var driver = sender as ChromeDriver;
I cant seem to combine this to create the sender IWebDriver driver = new ChromeDriver
|
-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
           }
         
       }
    }

5 Comments

There is no "global scope" in C#.
I mean to say outside the button click event method
Could you please clean up your answer? The formatting is terrible. It's hard to read like it is.
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(); } } }
What's that? Please don't put code in comments - edit your answer instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.