locked
How to set InArgument value in code [using a property editor] RRS feed

  • Question

  • I have code activity that has an input argument InArgument<string>.

    During design time I have a property editor hooked up in metadata that shows a folder browser dialog. When I try and set the results of the dialog back to the InArgument it fails with "End Of Expression Expected". It does not insert the double quotes like "C:\Temp". I can enter "C:\Temp" manually and it works fine as it has the double quotes. Any ideas here?

    Thanks

    public class DirectoryPickerEditor : DialogPropertyValueEditor
     {
      public DirectoryPickerEditor()
      {
       base.InlineEditorTemplate = EditorResources.GetResources()["DirectoryPickerEditorDataTemplate"] as DataTemplate;
      }
    
      public override void ShowDialog(PropertyValue propertyValue, IInputElement commandSource)
      {
       using (System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog())
       {
        dialog.SelectedPath = propertyValue.Value == null ? null : (propertyValue.Value as InArgument<string>).Expression.ToString();
        System.Windows.Forms.DialogResult result = dialog.ShowDialog();
        if (result == System.Windows.Forms.DialogResult.OK)
        {
         propertyValue.Value = new InArgument<string> { Expression = new VisualBasicValue<string>(dialog.SelectedPath) };
        }
       }
      }
     }
    Thursday, June 24, 2010 12:54 PM

Answers

  • I think I found the answer:

    propertyValue.Value =

     

    new InArgument<string> { Expression = new Literal<string>(dialog.SelectedPath) }

    • Marked as answer by T Rex Thursday, June 24, 2010 1:27 PM
    Thursday, June 24, 2010 1:27 PM

All replies

  • In you anonymous method, just add character 34 around the dialog.SelectedPath ...
    Thursday, June 24, 2010 1:21 PM
  • I think I found the answer:

    propertyValue.Value =

     

    new InArgument<string> { Expression = new Literal<string>(dialog.SelectedPath) }

    • Marked as answer by T Rex Thursday, June 24, 2010 1:27 PM
    Thursday, June 24, 2010 1:27 PM
  • So to give the complete answer here:

    When setting an InArgument<string> to a string value can use:

    InArgument<string> arg = new InArgument<string>(stringVariable) and internally it creates a Literal<string>, this takes care of the double quote problem. Obvious really!

    To get the expression out of an InVariable I created an extension method that can be used as arg.GetExpressionText<string>(). I would welcome any comments on how to improve it.

    public static string GetExpressionText<T>(this Argument argument)
      {
       VisualBasicValue<T> vbValue = argument.Expression as VisualBasicValue<T>;
       if (vbValue != null)
       {
        return vbValue.ExpressionText;
       }
    
       VisualBasicReference<T> vbReference = argument.Expression as VisualBasicReference<T>;
       if (vbReference != null)
       {
        return vbReference.ExpressionText;
       }
    
       Literal<string> literal = argument.Expression as Literal<string>;
       if (literal != null)
       {
        return literal.Value;
       }
    
       return null;
      }
    Thursday, June 24, 2010 1:52 PM