Link to home
Start Free TrialLog in
Avatar of chrisshebib
chrisshebib

asked on

creating a library of functions

I would like to create an assembly that I can include in a series of other assemblies.  The purpose of this would be to provide a library of functions and utilities across a project.  

I would prefer not to have to instantiate an object and call methods on it if possible.

Is there a way to code an object so that you can just call it directly?  Something like the macros that can be coded in C++, they can be called from anywhere in the project.

thanks,
Chris
Avatar of saar2
saar2

You cannot define a method outside a class.

What you can do is define only static function in the class so this way you don't need to create an object.

public class A
{
   public static void Foo () {}
}

public class   B
{
   B ()  { A.Foo; }
}

Saar

Create a class called fooUtils within your namespace and place the public utility methods in there.  Create an object
yourNamespace.yourUtils clsUtils = new YourNamespace.yourUtils() when your application loads and once that is done you have access to all the public utility functions (methods) you created within your application.  

For example this is a public function called isDate something that was missing in C# and I use it to determine if the submitted form has a valid date.

if(clsUtils.isDate(txtEventDate.Text))
     objNewEvent.EventDate=DateTime.Parse(txtEventDate.Text);
 
static functions can always be called with their classname.functionname(...). And another way is to create your own namespace, and import that one. It's the way whole C# works.

Regards,
CJ
Avatar of chrisshebib

ASKER

So if I were to create a new project with its own namespace and then add a class that has some static methods, I would then add a reference to my project from another assembly and import the namespace, (using myNamespace).

And the most elegant way to reference those methods from my new assembly would be then to use

className.methodName(parms);

?

In C++ you can create macros that are defined in your project, for error handling for example.

Then you can just wrap any method calls that return an HRESULT in your macro and have it handle all of your exceptions.  

MACRONAME(objectInstance->methodCall(parm))

If I'm understanding correctly, there is no equivilant in C#?

Chris
So if I were to create a new project with its own namespace and then add a class that has some static methods, I would then add a reference to my project from another assembly and import the namespace, (using myNamespace).

And the most elegant way to reference those methods from my new assembly would be then to use

className.methodName(parms);

?

In C++ you can create macros that are defined in your project, for error handling for example.

Then you can just wrap any method calls that return an HRESULT in your macro and have it handle all of your exceptions.  

MACRONAME(objectInstance->methodCall(parm))

If I'm understanding correctly, there is no equivilant in C#?

Chris
Ahhhh I was referring to using it on the winform or web form itself.

I added object instances to the class using the utility and referenced the method the same way.  No need to import the namespace since the assembly is part of the namespace.  I haven't seen anything like macros yet.


example in which there a new library has been created (which you can extend all the way), and no object has been created:

namespace myNS
{
     using System;
     public class c
     {
          public static void cw(string x){Console.WriteLine(x);}
     }
}

namespace ConsoleApplication1
{
    using System;
    using myNS;

    public class Class1
    {
        public Class1()
        {
           
        }

        public static int Main(string[] args)
        {
         c.cw("test");
            return 0;
        }
    }
}


ps. haven't found any macro-support either
ASKER CERTIFIED SOLUTION
Avatar of saar2
saar2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Saar,

Thanks very much for your help.  Its too bad that there aren't macros in .net, they are very nice to use.

Anyway, thanks again,
Chris
I don't like macros at all.

Look at MFC code, it's so unreadable with the macros ("BEGIN_MESSAGE_MAP, END_MESSAGE_MAP, etc..")

It's so not OOP...