0

I want to start new browser Chrome process.

In the class ChromeDriver have a method to do this.

And tried to initialize ChromeDriver like this:

ChromeDriver browser;
private void OpenBrowser()
{
   browser = new ChromeDriver(Browsers.Chrome);
}

Problem is:

I start n process browser Chrome, and it runs only one browser, another process not run my code(although it initialized).

So, I tried to change code:

private void OpenBrowser()
{
   var browser = new ChromeDriver(Browsers.Chrome);
}

It's working, but another method was using the browser. But I can't declare var browser out of a method.

It will return error like:

The contextual keyword 'var' may only appear within a local variable declaration or in script code

Updated:

I saw all answer and know var is ChromeDriver in my situation.

But when to run it, have the problem.

I will describe more information.

Suppose, I need to start 2 Chrome process. After initialized two Chrome process, I will go to URL with:

browser.GoToUrl(link);

So, I will know it working or not working.

At first, the case using ChromeDriver it still opens 2 Chrome process, but it was only working with the second process, the first process not working.

At second, the case using var keyword, it opens 2 Chrome process, and it was also working with two processes.

Ave
  • 4,338
  • 4
  • 40
  • 67
  • 2
    All that `var` does is allow you to omit *specifying* the type of the variable. It would have been exactly the same had you written `ChromeDriver browser = new ChromeDriver(Browsers.Chrome);` inside of `OpenBrowser` (this also changes `browser` from a field to a local variable). Whatever your issue is, it's broader than just these few fragments of code – Damien_The_Unbeliever Aug 04 '16 at 08:50
  • 1
    Why not simply return `ChromeDriver` (instead of `void`) from your `OpenBrowser()` method? – Filburt Aug 04 '16 at 08:50
  • @Filburt, have more code, I know `var browser` is `ChromeDriver browser`. But I don't know it not working. If I tried with `var` it working. – Ave Aug 04 '16 at 08:54
  • http://stackoverflow.com/questions/209199 see this – Hakan Fıstık Aug 04 '16 at 08:54
  • I'm afraid I am struggling to understand exactly what your problem is. The edit has added in a bunch of code that has no obvious relationship to the first bit of code that you gave. Can you try to clearly state the problem in terms of "This is what I am doing. This is what I want to happen. This is what actually happens."... – Chris Aug 04 '16 at 09:21
  • @Chris I explain all problem I meet. You can see at updated question. – Ave Aug 04 '16 at 09:26
  • 1
    Ah, I think I see what your problem is though it feels a long way from the original question about use of var keyword... Waht you need to do is find a way to store your multiple chrome processes and then access the right one in particular. At some level this will probably mean creating multiple of one type of object and storing them (in an array, ad dictionary or something like that). You would then have an openbrowser method that opened one instance of a browser and that gets stored in your dictionary. Then when you want that specfific browser you fetch it from the dictionary. – Chris Aug 04 '16 at 09:29
  • Possible duplicate of [Use of var keyword in C#](http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp) – Florent B. Aug 04 '16 at 09:36
  • @Chris. Such close. But my problem is simple. Create multiple chrome processes and each process must execute line: `browser.GoToUrl(link);`. In my case using `var browser` it works. But it is only valid for one method. Another method also using `browser` to continues. – Ave Aug 04 '16 at 09:47

4 Answers4

3

I'm not 100% clear on what you are trying to do but I can answer the question as asked:

var is just syntactic sugar to save having to write the full name of the type. As you can see from the error it can only be used with local variables.

What this means is that the following two lines are equivalent:

ChromeDriver browser = new ChromeDriver(Browsers.Chrome);
var browser = new ChromeDriver(Browsers.Chrome);

When compiled the compiler will see that browser is being set to an object of type ChromeDriver and thus act as if it was declared as ChromeDriver.

The main advantage of this is when working with ugly long variable names (often with generics) such as:

Dictionary<string, Dictionary<int, List<double>> myObject = new Dictionary<string, Dictionary<int, List<double>>();

This could be written more readably as:

var myObject = new Dictionary<string, Dictionary<int, List<double>>();

As an additional note it may be implicit in what I've said but to be explicit you need to have an assignment to use var. You couldn't for example do:

var myObject;

Because the compiler would complain that it can't work out the type of myObject (which is pretty obvious looking at that line of code!).

Chris
  • 27,210
  • 6
  • 71
  • 92
  • Can you see my question is updated? Like you said, a problem can be at class `ChromeDriver`. So, I added more code. – Ave Aug 04 '16 at 09:04
1

var is a keyword that can only be used within the scope of a method. Using it within the scope of a class will throw an error.

If you could use it outside a method the result would be the same as your first example, as all it does is infer the type, which would mean it'd be the same as declaring it as ChromeDriver.

1

How about to return your reference from method?

private ChromeDriver OpenBrowser()
{
   return new ChromeDriver(Browsers.Chrome);
}

Also, according to example ChromeDriver class implements IWebDriver interface so it's better to write like this:

private IWebDriver OpenChromeBrowser()
{
   return new ChromeDriver(Browsers.Chrome);
}

In your another method (in same class, couse your method is private) you can use it like this:

var driver = OpenChromeBrowser();
teo van kot
  • 12,350
  • 10
  • 38
  • 70
  • When I am using `driver`, I must initialize? In other class, I am also using `driver` variable. Your variable `driver` in a method, not global variable. – Ave Aug 04 '16 at 09:09
1

When you declare a var variable .net needs to know what kind of var you are declaring, so you can use var when you have on the other side the type of var you are declaring.

var cool = new MySuperBrowserClass();
var lol = 69;

In both cases .net knows that on the other side there is a class of type MySuperBrowserClass or just an int.

In other words: if you want to declare a var, no matter the context, you have to clarify its type.

Perhaps (maybe is not the case because of other reasons!) you can change in your first code sample where you have:

ChromeBrowser browser;

doing something like:

var browser = new ChromeDriver(Browsers.Chrome);
juagicre
  • 1,065
  • 30
  • 42