Solved

creating a library of functions

Posted on 2001-06-26
11
190 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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 100 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

Network it in WD Red

There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

Join & Write a Comment

Suggested Solutions

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Resolve DNS query failed errors for Exchange
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now