0

May be this is a stupid question but: I wonder if there is something like default parameter but not by value - but by name.

Example:

I must use a parameter "IWebDriver driver" in a lot of my methods. And I know that always when I use it I will use the name "driver" - the "object" behind the name can be different(IE, FF, Chrome..) but the name will always be the same.

So is this possible to have a function

public void CheckName(string nameToCheck, string expectedValue, IWebDriver driver = driver with name "driver")  
{
   some code....
}

And when I use it NOT to do:

CheckName("MyName", "MyName", driver)  

but to do:

CheckName("MyName", "MyName")

... and the method knows that must get the object with name(string) "driver".

If I want to use other name than default just to specify it:

CheckName("MyName", "MyName", driverOtherName)
klashar
  • 2,519
  • 2
  • 28
  • 38
Stanislava
  • 91
  • 2
  • 9
  • Add a bunch of overloads? – Anton Gogolev Feb 17 '17 at 11:57
  • From where the method will get the driver object if don't want to pass it explicitly? Can you provide more information ? – Chetan Feb 17 '17 at 11:58
  • If I understand your question right, you want to check the name of the variable that stores your (optional) parameter value. And no, that is not possible. Variable names (should) have absolutely no meaning for your code, they could be anything. – Lukas Körfer Feb 17 '17 at 12:07
  • Yes lukegv this is what I want to do... I know that I can put anything for name of a variable, but way not to have this possibility.??? Write now I must write in every method parameter (WebDriver driver) and i know that the name of the driver will be "driver" every time. if this was possible my methods will look clear - now I see driver word every where.... and I see that if I can some how not show it it will be better...Something like default parameter by name... And if there is no such name we will see error like usual if we enter the name that is not declared before.... – Stanislava Feb 17 '17 at 12:23
  • @ Chetan Ranpariya: I want to pass it. And i will pass it. But I know that every time the name of my object will be "driver" and i don't want to write this every time. If it is possible to create my method something like: public void CheckName(string nameToCheck, string expectedValue, IWebDriver driver = Object.WithName["driver"]) - something like this. And if there is no such object just to see an error... – Stanislava Feb 17 '17 at 12:32

1 Answers1

2

No, there's no way of doing that. Default parameters have to have constant values - they can't depend on a value taken from a local variable.

It sounds like you should probably construct an instance which stores a driver reference in a field, then you can just call the methods on that instance and it can use the value from the field:

public class FooChecker
{
    private readonly IWebDriver driver;

    public FooChecker(IWebDriver driver)
    {
        this.driver = driver;
    }

    public void CheckName(string nameToCheck, string expectedValue)
    {
        // Use driver here
    }
}

Then you can use:

var checker = new FooChecker(driver);
checker.CheckName("MyName", "MyName");
checker.CheckName("MyName2", "MyName2");
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Well, you can easily use `null` as default parameter and something like `driver = driver ?? myLocalVariable` in the method. But I assume this is not OPs question. – Lukas Körfer Feb 17 '17 at 12:02
  • @lukegv That's what Jon means by constant values. – EpicKip Feb 17 '17 at 12:09
  • Ok if I want a class FooChecker(driver) to be part of nuget package with name Test? Is this possible to write "using Test.FooChecker(driver)? – Stanislava Feb 17 '17 at 12:11
  • @Stanislava: Only `FooChecker` is the class - you need to construct an *instance* of that class within your code - the using directive you've described is entirely bogus syntax. – Jon Skeet Feb 17 '17 at 12:18
  • Ok, so I can not add FooChecker in nuget package. If I want this to be in nuget package i must pass the driver like parameter... And there is my problem... I want not to write every time the word "driver" in my methods if it can be skipped with something like default parameter by name it will be great. But for now I see that this is not possible :( – Stanislava Feb 17 '17 at 12:28
  • @Stanislava: You can add a type to a nuget package, assuming you're publishing the nuget package - although we've no idea which package you're talking about. But that's just adding a type like any other type... you'd still need to instantiate it. You really haven't given us much context here. – Jon Skeet Feb 17 '17 at 12:42
  • @Jon Skeet: I just don't want to write a word "driver" every time because I know that my object will be named driver. And I must pass this driver object throw all my methods and this is a lot of code and I see this word driver everywhere. Some of the methods I put in nuget package too. And if it is possible just to tell to the parameter use as default the object with name "driver" or "x" or... it will be great. But for now I see that this is only in my fantasy and may be this is not possible... – Stanislava Feb 17 '17 at 12:49
  • @Stanislava: It's not just that it's a fantasy: it would be a bad idea. If you need to use the driver all through your code, you should make it a field which is used by default, as per my answer. Note how I only need to refer to `driver` *once* in the usage code, then both of the calls to `CheckName` use the driver automatically. Unfortunately as you've hardly given us any context, it's hard to help you out more than that - and we've no idea which nuget package you've been mentioning. – Jon Skeet Feb 17 '17 at 13:39