How to use custom function in several code modules

Ranorex Studio, Spy, Recorder, and Driver.
marasanov
Posts: 8
Joined: Thu Oct 30, 2014 2:40 pm

How to use custom function in several code modules

Post by marasanov » Mon Nov 10, 2014 5:34 pm

Hi all,

I have a custom function that returns true/false value and I would like to use it in several different Code Modules in the following way:
bool status = customFunction();
How can I implement this in my project?

Thanks

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

Re: How to use custom function in several code modules

Post by krstcs » Mon Nov 10, 2014 5:53 pm

If it will be used in more than one solution, you will need to create a library (DLL) that contains the method. A discussion on how to do that is a bit too long for this forum and you can probably find more information on MSDN or other .NET related sites.

If the method will only be used in one solution, then your best bet would be to create the method as a static method in the main project's Program.cs file.

Code: Select all

public static bool CustomFunction() {
    bool returnVal = true;
    
    //Do the work here and set returnVal to the appropriate value.
    
    return returnVal;  
}
You would then call this function in other places by using:

Code: Select all

bool value = Program.CustomFunction();
Note that you will have to use "Program." everywhere you use it because it is static.
Shortcuts usually aren't...

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to use custom function in several code modules

Post by odklizec » Mon Nov 10, 2014 7:37 pm

Hi,
One more possibility to achieve what you want is via inheritance. It's nicely described in this article:
http://www.ranorex.com/blog/custom-smart-actions
Scroll down to Spread your Custom Action by Inheriting from a Base Class
Such common class could also be used within different solutions. I'm personally using this approach together with svn externals. This allows us to have just one (always updated) common class shared with multiple projects.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

marasanov
Posts: 8
Joined: Thu Oct 30, 2014 2:40 pm

Re: How to use custom function in several code modules

Post by marasanov » Tue Nov 11, 2014 10:06 am

Thanks for the answers.

The DLL project works for me.