locked
Acces a varaible by name in WF4 RRS feed

  • Question

  • Hi all,

    Context : Workflow 4

    I have to acces a variable by here name in a custom activity (in the execute method).
    I know only the name of this variable

        public sealed class MyActivity : NativeActivity //, IManageVariable<String>
        {
            protected override void Execute(NativeActivityContext context)
            {
                    String myVaraibleName = "varaibleX";
                    // Here i would like to use my variable,
                    // How can i do?
            }        
        }
    • Moved by Andrew_Zhu Monday, February 8, 2010 10:30 AM (From:Windows Workflow Foundation)
    Tuesday, February 2, 2010 9:41 AM

Answers

  • Hi Scott,

    Thanks for help but i know the IExecutionProperty and that isn't what i need.

    My users have to edit the variable with variable's designer and my ActivityDesigner permit somone to choose Variables in a combobox. I tryed many solution but the best way i found was to set the argument when an user change a property (varaibleName)

    Like in this code :

    public abstract class ArgumentActivity<T> : NativeActivity
    {
        #region "Déclarations"
        
        private String m_VariableName;
        private InOutArgument<T> m_Argument;
    
        #endregion
    
        #region "Propriétés"
    
        /// <summary>
        /// Nom de la variable
        /// </summary>
        [Browsable(false)]
        public String VariableName
        {
            get
            {
                return this.m_VariableName;
            }
            set
            {
                this.m_VariableName = value;
    
                // Mise à jour de l'argument en fonction du nom choisi
                this.m_Argument = this.m_VariableName == null
                    ? null
                    : new InOutArgument<T>(new VisualBasicReference<T>(this.m_VariableName));
            }
        }
    
        /// <summary>
        /// Varaible utilisé en entrée / sortie
        /// </summary>
        [Browsable(false)]
        public InOutArgument<T> Argument
        {
            get
            {
                return this.m_Argument;
            }
            set
            {
                this.m_Argument = value;
            }
        }
        
        #endregion
    }

    Tuesday, February 9, 2010 9:10 AM

All replies

  • The only way to access data external to an activity is through the use of Arguments on the activity. So in this case you'll need to define an InArgument on the activity, bind the variable to the argument when configuring the activity, then use the Get method on InArgument<T> to get the value.
    Vishal - This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, February 2, 2010 10:29 PM
  • Hi Vishal,

    Thanks for your reply but i know that. I use a code to affect the argument during the desing thime (when the varaible's name is changed, the argument is affected) 

    This is my sample to affect an InOutArgument when a property VariableName change :

    public abstract class ArgumentActivity<T> : NativeActivity
    {
        #region "Déclarations"
        
        private String m_VariableName;
        private InOutArgument<T> m_Argument;
    
        #endregion
    
        #region "Propriétés"
    
        /// <summary>
        /// Nom de la variable
        /// </summary>
        [Browsable(false)]
        public String VariableName
        {
            get
            {
                return this.m_VariableName;
            }
            set
            {
                this.m_VariableName = value;
    
                // Mise à jour de l'argument en fonction du nom choisi
                this.m_Argument = this.m_VariableName == null
                    ? null
                    : new InOutArgument<T>(new VisualBasicReference<T>(this.m_VariableName));
            }
        }
    
        /// <summary>
        /// Varaible utilisé en entrée / sortie
        /// </summary>
        [Browsable(false)]
        public InOutArgument<T> Argument
        {
            get
            {
                return this.m_Argument;
            }
            set
            {
                this.m_Argument = value;
            }
        }
        
        #endregion
    }
    i was looking for a way to find variable during the execution time ;)
    Wednesday, February 3, 2010 9:05 AM
  • The only other thing I can think of that may be of use here is the ExecutionProperties:
    http://msdn.microsoft.com/en-us/library/ee358743(VS.100).aspx

    It's not a Variable type that it will contain, but it will be able to access a named property.
    Please take a look at this and see if this is what you're looking for.
    Thanks,
    Scott
    MS Developer Support
    Monday, February 8, 2010 10:36 PM
  • Hi Scott,

    Thanks for help but i know the IExecutionProperty and that isn't what i need.

    My users have to edit the variable with variable's designer and my ActivityDesigner permit somone to choose Variables in a combobox. I tryed many solution but the best way i found was to set the argument when an user change a property (varaibleName)

    Like in this code :

    public abstract class ArgumentActivity<T> : NativeActivity
    {
        #region "Déclarations"
        
        private String m_VariableName;
        private InOutArgument<T> m_Argument;
    
        #endregion
    
        #region "Propriétés"
    
        /// <summary>
        /// Nom de la variable
        /// </summary>
        [Browsable(false)]
        public String VariableName
        {
            get
            {
                return this.m_VariableName;
            }
            set
            {
                this.m_VariableName = value;
    
                // Mise à jour de l'argument en fonction du nom choisi
                this.m_Argument = this.m_VariableName == null
                    ? null
                    : new InOutArgument<T>(new VisualBasicReference<T>(this.m_VariableName));
            }
        }
    
        /// <summary>
        /// Varaible utilisé en entrée / sortie
        /// </summary>
        [Browsable(false)]
        public InOutArgument<T> Argument
        {
            get
            {
                return this.m_Argument;
            }
            set
            {
                this.m_Argument = value;
            }
        }
        
        #endregion
    }

    Tuesday, February 9, 2010 9:10 AM
  • Hi All,

    You can use the following code to access the variables or arguments defined in the workflow inside custom activity.

        public sealed class MyActivity : NativeActivity //, IManageVariable<String>
        {
            protected override void Execute(NativeActivityContext context)
            {
                    String myVaraibleName = "varaibleX";
                    // Here i would like to use my variable,
                   String MyVariableValue=context.DataContext.GetProperties()[myVaraibleName].GetValue(context.DataContext).ToString ();

            }        
        }
    Cheers,
    Bahram

    Wednesday, September 15, 2010 6:37 AM