Samantha J T Star
Samantha J T Star

Reputation: 32848

How can I declare a static function within a class?

I would like to code a method inside the following:

namespace WebUx.Helpers.CSharp
{
    public class getExceptionMessage
    {

        < Method here >

        var exError = e.Message;
                    if ( e.InnerException != null ) {
                        exError += "<br>" + e.InnerException.Message;
                        if (e.InnerException.InnerException != null ) {
                            exError += "<br>" + e.InnerException.InnerException.Message;
                        }
                    }
    }
}

Can someone tell me how I should declare the class and method. I get mixed up over how best to declare and I think it should be static but not sure if there is a better way. Note that this method I want to have callable from many places in my code. I think I should probably return a string with the value exError.

Upvotes: 0

Views: 171

Answers (3)

ken2k
ken2k

Reputation: 49013

In order to avoid those "helper classes" that could be used everywhere in the code I would write an extension method:

public static class ExceptionExtensions
{
    public static string GetFormattedErrorMessage(this Exception e)
    {
        if (e == null)
        {
            throw new ArgumentNullException("e");
        }

        var exError = e.Message;
        if (e.InnerException != null)
        {
            exError += "<br>" + e.InnerException.Message;
            if (e.InnerException.InnerException != null)
            {
                exError += "<br>" + e.InnerException.InnerException.Message;
            }
        }

        return exError;
    }
}

then:

try
{
}
catch (Exception ex)
{
    string msg = ex.GetFormattedErrorMessage();
}

I personally don't really like all those StringHelper, ExceptionHelper, FormHelper... classes when the only purpose is to provide static methods that are related to a particular type. Extension methods are made for this particular purpose.

Upvotes: 5

Paul Sasik
Paul Sasik

Reputation: 81557

You're looking to create a helper/utility class for use throughout your code. I think you're on the right track and static classes/methods are good for this concept.

Static classes and methods are declared like this:

public static class MyUtilClass
{
    public static string GetExceptionMsg(Exception ex)
    {
                var exError = e.Message;
                if ( e.InnerException != null ) {
                    exError += "<br>" + e.InnerException.Message;
                    if (e.InnerException.InnerException != null ) {
                        exError += "<br>" + e.InnerException.InnerException.Message;
                    }
                }

        return exError;
    }
}

And in your client code you would use the class like this:

try
{
    // do something
}
catch (Exception ex)
{
    var errMsg = MyUtilClass.GetExceptionMsg(ex);
    // display or log errMsg 
}

Upvotes: 4

user596075
user596075

Reputation:

public static void YourStaticMethod()
{

}

That is how you would declare a static method inside of your class. It seems as you may be unsure if it needs to be static or not. You need to decide whether or not this is functionality that should be called as part of an instantiated object. If that is the case, then it shouldn't be static. Otherwise, if it is a utility method or something along those lines, static may be the desired route.

If you wanted the whole class to be static, you'd do this:

public static class getExceptionMessage
{

}

Note that if you make the class static, then every member of that class will also need to be static.

Upvotes: 1

Related Questions