Page 1 of 1

How to use custom function in several code modules

Posted: Mon Nov 10, 2014 5:34 pm
by marasanov
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

Re: How to use custom function in several code modules

Posted: Mon Nov 10, 2014 5:53 pm
by krstcs
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.

Re: How to use custom function in several code modules

Posted: Mon Nov 10, 2014 7:37 pm
by odklizec
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.

Re: How to use custom function in several code modules

Posted: Tue Nov 11, 2014 10:06 am
by marasanov
Thanks for the answers.

The DLL project works for me.