I like to create a file full of custom functions which I have made, which I may use in another project or something. Now I don’t fully understand how to go about this, normally in a language like php, you’d just create the php file and then go include(“cust_lib.php”) or whatever the file is called.
Now I think that the process involves the library having its own namespace, then either go using custom_lib; or custom_lib:: within the script (I don’t want to get into a discussion over which is the best way to go here).
Is this right? Or should I create the library and convert it to a .dll, if so how do I go about this, what sort of syntax does a dll have inside it etc.
However if its just file within one project then I don’t need to go down that route do I? I can just create the namespace and use that?
This is what I’m working for at the moment, and thought it would be something like this
namespace Custom_Lib{
~~functions to go here~~
}
However the functions have to exist within a class don’t they? So that becomes something like
namespace Custom_Lib{
class custom_lib{
public string function1(string input){
return input;
}
}
}
So some help, pointers, examples would be appreciated so I can wrap my head around this
Thanks,
Psy.
(Yes I call them functions, that just comes from a long php/js etc background)
The normal approach would be to create a Class Library project, put your classes and methods in that project, making sure that those you want to expose are
public. Then you add a reference to the resulting dll file in the client projects and you will have the functionality from the class library available to you.Even if you decide to put it all into one single file, I would still recommend you to make it a class library since I imagine that will make it easier to maintain. For instance, consider the following scenarios:
In those two cases, keeping it as a separate project will facilitate things for you:
Regarding the syntax issues: yes all methods must exist within a class. However, if the class is merely a container of the methods, you can make it (and the methods
static):That way you will not need to create an instance of
CustomLib, but can just call the method: