Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

creating a library of functions

Posted on 2001-06-26
11
Medium Priority
?
200 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:chrisshebib
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 4

Expert Comment

by:saar2
ID: 6228633
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

0
 
LVL 2

Expert Comment

by:preinsko
ID: 6228678
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);
 
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6228794
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chrisshebib
ID: 6228941
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
0
 

Author Comment

by:chrisshebib
ID: 6229038
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
0
 
LVL 2

Expert Comment

by:preinsko
ID: 6229074
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.


0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6230263
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;
        }
    }
}


0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6230264
ps. haven't found any macro-support either
0
 
LVL 4

Accepted Solution

by:
saar2 earned 300 total points
ID: 6233242
The .NET framework does not support preprocessing, hence you cannot use macros.

Saar
0
 

Author Comment

by:chrisshebib
ID: 6233288
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
0
 
LVL 4

Expert Comment

by:saar2
ID: 6235918
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...
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nslookup is a command line driven utility supplied as part of most Windows operating systems that can reveal information related to domain names and the Internet Protocol (IP) addresses associated with them. In simple terms, it is a tool that can …
Resolve DNS query failed errors for Exchange
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Screencast - Getting to Know the Pipeline

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question