Global User Code

Best practices, code snippets for common functionality, examples, and guidelines.
sunny1180
Posts: 4
Joined: Thu Jan 18, 2018 1:51 am

Global User Code

Post by sunny1180 » Thu Mar 01, 2018 12:45 am

Hi expert,

I am trying to create the Global User Code so all my recording can use the same method. I have no issue creating the method in local recording, everything works as expected. I then try to reproduce the same user code and place it under the user code collection so my other recording can use it. I understand that I can copy the user code action from 1 recording to another and that will automatically generate the user code, but I would like to instead create the global if possible.

Under the local recording, I have the user code inside the method as the following:

string NewVariable = repo.WebInterface1.SearchFunction.RegisterCheckRow01Value.Element.GetAttributeValueText("InnerText");

I did some range check and report back the result, no issue, work perfect.

I tried to do the same thing duplicate the code under the Global user collection, during build process, I have the following error:

The name 'repo' does not exist in the current context (CS0103)

Any idea? Thanks in advance!!!

asdf
Posts: 174
Joined: Mon Mar 21, 2016 3:16 pm

Re: Global User Code

Post by asdf » Fri Mar 02, 2018 12:41 pm

Hi Sunny,

This error message means that the repository is not defined within the user code collection.
Just declare the repository directly in the user code collection with the following code.

Code: Select all

var repo = NameOfYourRepository;
After you have done this, you can access all elements in the repository like the way you did in the user code method.

Hope that helps.

sunny1180
Posts: 4
Joined: Thu Jan 18, 2018 1:51 am

Re: Global User Code

Post by sunny1180 » Fri Mar 02, 2018 6:06 pm

Thanks, I did a test on it, it seems that I will have to create the NameOfYourRepository.Instance to get it working, If I don't have the '.Instance', it will report as an error 'NameOfYourRepository' is a 'type' but is used like a 'variable' (CS0118). It does gave me hint though and now my problem is solved.

On the other hand, I did try to bring in an Adapter as one of the Method parameter, it gives me flexibility to bring in any item from Repository.
asdf wrote:Hi Sunny,

This error message means that the repository is not defined within the user code collection.
Just declare the repository directly in the user code collection with the following code.

Code: Select all

var repo = NameOfYourRepository;
After you have done this, you can access all elements in the repository like the way you did in the user code method.

Hope that helps.