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) 
5
  • Add a bunch of overloads? Commented Feb 17, 2017 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 ? Commented Feb 17, 2017 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. Commented Feb 17, 2017 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.... Commented Feb 17, 2017 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... Commented Feb 17, 2017 at 12:32

1 Answer 1

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"); 
Sign up to request clarification or add additional context in comments.

8 Comments

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.
@lukegv That's what Jon means by constant values.
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: 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.
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 :(
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.