Solved

creating a library of functions

Posted on 2001-06-26
11
193 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Downtime reduced, data recovered by utilizing an Experts Exchange Business Account Challenge The United States Marine Corps employs more than 200,000 active-duty Marines with operations in four continents, all requiring complex networking system…
This is the first one of a series of articles I’ll be writing to address technical issues that are always referred to as network problems. The network boundaries have changed, therefore having an understanding of how each piece in the network  puzzl…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

785 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