Hi
I've trying to create a shorter way to write - to save time :-
Code
using wl = Debug.WriteLine;I've declared this at the top of the project but I get a compile error. Can anyone tell me how to set the alias properly :-
Code
Debug.WriteLine("");I've like to be able to write such commands like this if possible:-
wl("");
Thanks
Matt

VulpesRank #55 · 29.2K reputationPosted Nov 12, 2012, 4:01 AM EST
using D = System.Diagnostics.Debug;
unfortunately, there's no way to create a file-wide alias for a method in C#.
You can effectively do it at type level e.g.
class MyClass
{
static void wl(string s)
{
Debug.WriteLine(s);
}
public void SomeOtherMethod(string s)
{
wl(s);
// ....
}
// ...
}
but it's tedious to do this in more than a couple of classes.
Muhammad Imran6 reputationPosted Nov 12, 2012, 8:48 AM EST
Action
then use anywhere in that class...
wl("Your text here");
Matt Higgins32 reputationPosted Nov 12, 2012, 11:15 PM EST
This command looks good but it doesn't compile :-
Action
I get a compile error:-
Error 1 Cannot create delegate with 'System.Diagnostics.Debug.WriteLine(string)' because it has a Conditional attribute
Any idea why this error appears
Thanks
Matt
VulpesRank #55 · 29.2K reputationPosted Nov 13, 2012, 4:24 AM EST
Muhammad Imran6 reputationPosted Nov 14, 2012, 7:00 AM EST
using System.Collections.Generic;
public static void ToDebug
{
System.Diagnostics.Debug.WriteLine(obj.ToString());
}
you may call it as:
string s="This is a text";
s.ToDebug();
etc..
VulpesRank #55 · 29.2K reputationPosted Nov 14, 2012, 7:05 AM EST