Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Maybe I'm being an airhead today. I don't want to keep typing out the namespace and class name, every time I call this class:
namespace MyNamespace
{
    public class DataHelpers
	{
		public string SysnamePath ( string db , string schema , string table )
		{
			return "[ " + db + " ].[ " + schema + " ].[ " + table + " ]";
		}
    }
}

Can this be written so that I don't have to keep typing the fully qualified path...
dbpath = DataHelpers.SysnamePath(...);



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

What I have tried:

I've tried doing this as an extension method and by pasting it into my calling method.
Posted
Updated 21-Dec-17 15:50pm

So first off, you shouldn't have to type the namespace as that would end up being a using statement at the top of your code.

For example, this should be an allowable usage for your format.
C#
using MyNameSpace;

namespace MyProgram
{
   public class Program
   {
      public void Main(string[] args)
      {
         var helper = new DataHelpers();
         helper.SysnamePath("");
      }
   }
}


However, you mention that you'd like to do this as an extension method, in which case you need to label your class and method as both static. So it would look something like this

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


However your extension method needs to reference a variable that it extends off of using the this keyword. So your SysnamePath method needs to use 1 variable with the this keyword.

So maybe you'd implement it something like this

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


So now your usage could look something like this

C#
using MyNameSpace;

namespace MyProgram
{
   public class Program
   {
      public void Main(string[] args)
      {
         var myDb = "Databasename";
         //SysnamePath is now an extension method for types of "string"
         myDb.SysnamePath("dbo", "my table name");
      }
   }
}


I would caution not extenion method-anizing all the things as it can be hard to understand propery usage if they are used on common types like string...in my opinion of course.
 
Share this answer
 
Comments
[no name] 21-Dec-17 22:05pm    
There are great points. I should have mentioned that I ruled against an extension method early on. What I was missing is that I could have simply made it a static method. I'm not sure why I didn't think of that! Thank you for responding!
This option is not very well known and, as far as I know, is not used much:
make your DataHelpers class/SysnamePath method static and then use (at the top of the file):
C#
using static MyNamespace.DataHelpers;
you can then only use the method name:
C#
dbpath = SysnamePath(...);
 
Share this answer
 
Comments
[no name] 23-Dec-17 1:14am    
I actually learned about that since posting the question. Others are saying that it's pretty rare too so I'd rather stick with common practice. I'll just stick to an instance method. Thanks, Peter.
[no name] 23-Dec-17 6:08am    
You're welcome. I think I saw this construction somewhere as a suggestion from Microsoft, so it can be that bad. Thanks for the reply.
[no name] 24-Dec-17 13:32pm    
Sorry, did you mean to say "can't" be that bad or "can" be that bad? I'm just wondering because of the context and I would like to have the best answer.
[no name] 24-Dec-17 15:21pm    
It is a typo, it must be "can not be so bad". I usually check what I have written but not good enough this time. Sorry about that!
Incidentally, I myself have no objection to static methods and use them whenever I deem it necessary.
[no name] 24-Dec-17 17:55pm    
Ok, thank you. It seems that everyone has an opinion and what I'm finding online is often outdated, which can be very misleading.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900