Search code examples
c#classcode-readability

Avoiding typing fully qualified name


Let's say I have an helper class, like so:

namespace MyNamespace
{
    public class DataHelpers
    {
        public string SysnamePath ( string db , string schema , string table )
        {
            return "[" + db + "].[" + schema + "].[" + table + "]";
        }
    }
}

How can I rewrite this or what can I do to make it so that I don't have to keep typing the fully qualified name...

string dbpath = DataHelpers.SysnamePath(...);

I don't want it to be an extension method or I don't understand how that could help and I don't want to paste the method in the same page as the caller.

It's possible that I don't understand something basic, as I am teaching myself. My understanding is that the method has to be in a class. I just don't want to type that class name very time.

Like I said, I'm sure this is something basic.


Solution

  • Make your class and method static:

    namespace MyNamespace
    {
        public static class DataHelpers
        {
            public static string SysnamePath ( string db , string schema , string table )
            {
                return "[ " + db + " ].[ " + schema + " ].[ " + table + " ]";
            }
        }
    }
    

    add in the header of file, where you want to use it:

    using static DataHelpers;
    

    use:

    string dbpath = SysnamePath(...);
    

    Reference