?
Solved

Functions that don't require new class declaration

Posted on 2006-05-07
4
Medium Priority
?
224 Views
Last Modified: 2012-05-05
Hi.

I know this is a very simple thing to do, as I've done it loads of times before, but it's been soooooo long since I've coded in C# that I really need some refreshing on certain aspects!

Imagine that I've created a function in a class and I want to call that function from elsewhere in my project.  I would do one of the following....

       obj = new Namespace.Class();
       obj.myFunction();

       or

       new Namespace.Class().myFunction();


....but, what I really want to do is this....

       Namespace.Class.myFunction();


I'm pretty sure that this boils down to the declaration of the function itself, but can't for the life of me figure out (or succesfully Google) how to do it!

Can someone give me an example please?

Thanks.
0
Comment
Question by:Jaymol
  • 2
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 400 total points
ID: 16624525
Declare this function as static:

public class Class
{
    public static myFunction()
    {
        ...
    }
}

Call:

Class.MyFunction();

You can see many static functions in .NET like Console.WriteLine().

C# 8.0 has additional feature: you can declare whole class as static, such class contains only static functions and cannot be instantiated.

0
 
LVL 6

Author Comment

by:Jaymol
ID: 16624541
Thanks Alex,

Just one question though.  I did the above, whilst slapping myself for forgetting something so simple, but it wouldn't compile.  I only had to declare the function return type (in this case, void) before it would.  Did you just get the above code a little wrong or would you expect it to have worked as you showed?

Thanks again,

John.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16624588
Sorry, it was my mistake, you fixed it.
0
 
LVL 6

Author Comment

by:Jaymol
ID: 16624614
Cheers big ears.
0

Featured Post

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!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

840 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