C# "var" in V9 code action

Is there some reason that I can't use "var" to declare an implicitly typed variable in C# code within BPM V9? The intellisense highlights it properly, but when I validate, I get an error:  

 

"The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?"

If I replace the "var" with an actual type, it validates OK.

 

"var" did come in with .Net 3.5, which is what Metastorm uses, did it not?

 

Tagged:

Comments

  • What is the "Type" you are trying to implicitly declare?

     

    I just tried with these 3 and they all validated and deployed fine.

     

    var s = new List();

    var sb = newSystem.Text.StringBuilder();

    var x = System.DateTime.Now;

     

    Maybe there is another error, elseware causing the validation error?

  • It's actually referencing a class in one of our own .Net .dll's, which we've put in the dotnetlib folder. I wanted to type

     

    var oMailMerger = new Amec.OpenXML.MailMerger(Local.txtTemplate);

     

    but this produces the error. If I type

     

    Amec.OpenXML.MailMerger oMailMerger = new Amec.OpenXML.MailMerger(Local.txtTemplate);

     

    it works! It's not a big deal, but it does seem a curious anomaly. We're on BPM 9.1.2, by the way.

     

  • Did you add a using statement up top?

     

    using Amec.OpenXML;

     

    It looks like the compiler just isn't aware of your custom type.

     

    Also, if you haven't already done so, "activate" your DLL by going into the Designer Options > Scripting > Namespaces.

  • That's interesting! I can't use a "using" statement as this is in a Code Action, not in a script. If I try exactly the same thing in a method in a Server Script, even without a "using" statement, it accepts "var" with no problem. In the Code Action, it won't have it at any price.

     

    Activating the DLL via the Namespaces tab in Options does not seem to affect the behaviour at all.

     

  • That makes sense... A Code Activity only exposes a slice of an existing transaction context for you to drop your code into. It does not give you a full coding environment like a server side script where you can define an entire class. As a matter of fact, if you step in the Debugger, you'll find that your code gets injected into an existing "Execute" method, so your “using” statements would not get compiled since they are normally defined outside of the encompassing class (this may differ slightly depending on which version you are running against).

     

    If this causes you problems in your Code Activity, try using the full namespace of whatever class/type you are trying to implicitly declare or otherwise work with. The other option which you already know would be to just put it in a static method in a server side script and call that from your Code Activity.