Daniell
Daniell

Reputation: 59

Can not use string in another method

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?

Upvotes: 0

Views: 85

Answers (3)

primo
primo

Reputation: 1472

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

Upvotes: -3

Salah Akbari
Salah Akbari

Reputation: 39946

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

Upvotes: 2

cozmic
cozmic

Reputation: 178

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.

Upvotes: 3

Related Questions