Pass a parameter by reference in UserCode

Best practices, code snippets for common functionality, examples, and guidelines.
daniel.maciasperea
Posts: 5
Joined: Fri Jul 08, 2016 1:07 pm

Pass a parameter by reference in UserCode

Post by daniel.maciasperea » Thu Sep 29, 2016 3:18 pm

Is it possible to pass in a User Code Method a parameter by reference?
I have declared in my code the parameter as ref but i cannot find the option in the Argument Editor of Ranorex, so I get a compiler error.

Code: Select all

public void addTimestampToVendorName(ref var)	{
	var= var.ToString() + "-" + System.DateTime.Now.Ticks.ToString();
}

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Pass a parameter by reference in UserCode

Post by krstcs » Thu Sep 29, 2016 3:49 pm

No, it is not possible with the current implementation of Ranorex. Currently Ranorex only supports strings and some other basic types (bool, int, etc.).

In this case, you should set the module variable's value directly in user-code, no need to pass anything.

Assuming VendorName is the module variable (given your method name):

Code: Select all

public void addTimestampToVendorName() {
    VendorName = VendorName + "-" + System.DataTime.Now.Ticks.ToString();
}
Shortcuts usually aren't...

daniel.maciasperea
Posts: 5
Joined: Fri Jul 08, 2016 1:07 pm

Re: Pass a parameter by reference in UserCode

Post by daniel.maciasperea » Thu Sep 29, 2016 3:58 pm

Thanks for the answer. Using directly the variable in the code is the solution I was implementing, however I was trying to code something easier to be reused in other Test Cases.

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Pass a parameter by reference in UserCode

Post by krstcs » Thu Sep 29, 2016 6:28 pm

If you want to do something like that in a library, then the best way is to return the desired string from the library method like this:

Code: Select all

public string AppendDateTime(string input) {
    return input + "-" + System.DataTime.Now.Ticks.ToString();
}
//or you can make it an extension method:
public string AppendDateTime(this string input) {
    return input + "-" + System.DataTime.Now.Ticks.ToString();
}
Then in your user-code modules you would do this:

Code: Select all

VendorName = MyLib.AppendDateTime(VendorName);
// as extension method:
VendorName = VendorName.AppendDateTime();
But, one-way or another, you always have to have some concrete implementations for the test to use, either by doing it this way, or the way I showed before.

The thing about putting something this simple in a library is that it usually is only really useful for some specific cases, and even then, doing it the first way I suggested is usually preferred because the library call can hide what the real intention of the code is if it's not named really well. Plus, it frees you from always having to include the library for just the one line of code.

The rule-of-thumb is, if you can do it in a single, simple statement, you should leave it. If you can't, then make it a method.

Usually the simplest way is the best.
Shortcuts usually aren't...

daniel.maciasperea
Posts: 5
Joined: Fri Jul 08, 2016 1:07 pm

Re: Pass a parameter by reference in UserCode

Post by daniel.maciasperea » Fri Sep 30, 2016 8:16 am

Thank you very much for your answer krstcs! You are right and maybe for such short piece of code it is not worthly to create a library.