Reputation: 38190
In C# (within the context of T4 template see http://www.olegsych.com/2008/03/how-to-generate-multiple-outputs-from-single-t4-template/) I want to do this
<# String myTemplateVar;
#>
<# if (string.IsNullOrEmpty(myTemplateVar)) {
myTemplateVar= "name";
};
#>
I want to give a value to myTemplateVar if myTemplateVar has not already been setup by an external call from T4 engine in another template which would have this instruction:
CallContext.SetData("myTemplate.myTemplateVar", ExternalTemplateVar);
But I cannot even compile in C# why ? How to fix this ?
This kind of stuff is easy to do in PHP I don't understand why in C# it seems so complicated.
Update: the problem is that if I initialize to either Null or Empty to avoid compilation error, how can I detect that the variable has been setup by an external call ?
Upvotes: 2
Views: 1781
Reputation: 6606
The myTemplateVar variable as defined above is at method scope inside the class underlying the template (inside the TransformText method).If you'd like a class-level, then you could use a class feature block, like so.
<#+ public string myTemplateVar; #>
or even a class static:
<#+ public static string myTemplateVar; #>
However, there's still no way to tell whether the variable has been set, other than by its null value or not though for a reference type in C#.
Upvotes: 3
Reputation: 48547
Try:
<# String myTemplateVar = String.Empty; #>
in order to resolve the compile error.
Upvotes: 2
Reputation: 16687
Have you tried giving your string a default value? If it does not get in the if block, it will remain unassigned.
Upvotes: 1