Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

creating a library of functions

Posted on 2001-06-26
11
Medium Priority
?
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Sometimes you might need to configure routing based not only on destination IP address, but also on a combination of destination IP address (or hostname) and destination port number. I will describe a method how to accomplish this with free tools. …
The Need In an Active Directory enviroment, the PDC emulator provide time synchronization for the domain. This is important since Active Directory uses Kerberos for authentication.  By default, if the time difference between systems is off by more …
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

670 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