Going back to my old c++ days at university where we had all our code littered with preprocessor directives - I thought it made the code ugly and could never understand why it was useful. Today though I found a use in my C# application.
The scenario – I had made various security levels in my application and tied my XAML to the levels by set by static accessors in code. An example of my XAML code for a Combobox to be enabled would be as follows…
<ComboBox IsEnabled="{x:Static security:Security.SecurityCanEditDebtor}" />
And then I would have a static method like this…
public static bool SecurityCanEditDebtorPostalAddress
{
get
{
if (SecurityCanEditDebtorPostalAddress)
{
return true;
}
else
{
return false;
}
}
}
My only problem was that my XAML did not like the if statement – which meant that while my code worked during runtime, during design time in VS2010 it gave some horrible error like…
NullReferenceException was thrown on “StatiucExtension”: Exception has been thrown by the target of an invocation…
If however my C# method was changed to something like this…
public static bool SecurityCanEditDebtorPostalAddress
{
get
{
return true;
}
}
My XAML viewer would be happy. But of course this would bypass my security…