User Code Collections in other Projects

Ask general questions here.
Tomsk666
Certified Professional
Certified Professional
Posts: 34
Joined: Thu Mar 28, 2013 12:59 pm
Location: UK
Contact:

User Code Collections in other Projects

Post by Tomsk666 » Wed Jul 05, 2017 4:03 pm

I love the recent addition of User Code Collection libraries in Ranorex. However, I found a quirk that could be resolved.
1. Create a user code collection in one project & build the project
2. Create a new project and add the previous project from step 1 as a .NET assembly reference
3. In the new project, open a recording module, click the Add New Action ->User Code->Select from library
You will see that the static user code methods from your referenced user code project do not show up!

The fix I found for this is to change the user code collection class to explicitly make it static, e.g. from:

Code: Select all

[UserCodeCollection]
    public class UserCodeCollection1
    {
        [UserCodeMethod]
        public static void Invoke_KP()
        {
        	Host.Local.RunApplication(@"C:\Program Files (x86)\KeePass Password Safe 2\KeePass.exe");
        }
    }
to:

Code: Select all

[UserCodeCollection]
    public static class UserCodeCollection1
    {
        [UserCodeMethod]
        public static void Invoke_KP()
        {
        	Host.Local.RunApplication(@"C:\Program Files (x86)\KeePass Password Safe 2\KeePass.exe");
        }
    }
Then re-build the project, and refresh the reference to it in the new project.
It's strange as in a user code collection all the methods are static, and calls to methods do not use instantiation, so could Ranorex get it to add the static declaration automatically, as this would make it easy for users to use user code methods in other projects point and click?

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: User Code Collections in other Projects

Post by Support Team » Fri Jul 07, 2017 8:00 pm

Hello Tomsk,

I followed your exact steps but I am able to use usercodecollection methods in a recording module from my referenced project with or without the class being static. This was tested in Ranorex 7.1.0. What version of Ranorex are you using?

Cheers,
Ned

scrushmaster
Posts: 13
Joined: Thu Dec 08, 2016 1:46 am

Re: User Code Collections in other Projects

Post by scrushmaster » Thu Sep 28, 2017 12:17 am

Tom,

Thanks for solving this for me. I was also passing it via a .dll to other projects and it was inaccessible until i added the static stuff.

What I do to get the issue:
Create a common .dll file with the usercode.cs file
Create a new project link the dll to it
Try and use the user code within code like Dllname.usercodeclass.usercodemethod();