Member Avatar for cool_intentions

Hi guys,

I’m working on my assignment for school, end I got stuck. I have workflow end my host application. On my host app is one combo box. I need to write code for my ifElseBranchActivity in workflow.
Something like this:

if (comboBox1.SelectedItem == "option1")
            {
                //do some code
            }

The problem is, it doesnt work this way in workflow, so how can I do that?

//Host
            Dictionary<string, object> parametars = new Dictionary<string, object>();
            parametars.Add("option1",parametars);
            parametars.Add("option2", parametars);
            parametars.Add("option3", parametars);

            comboBox1.DataSource = new BindingSource(parametars, null);
            comboBox1.DisplayMember = "Key";
            comboBox1.ValueMember = "Value";
  //WorkFlow
            public string option1 { get; set; }
            public string option2 { get; set; }
            public string option3 { get; set; }

When working with Object properties, you can determine the type using something like:

System.Diagnostics.Debug.WriteLine(comboBox1.SelectedItem.GetType());

Assuming you are using an IDE. Otherwise you can just show it in a MessageBox. In this case it is returning a KeyValuePair<string, object>, so you can just cast it and check the Key property:

KeyValuePair<string, object> pair = (KeyValuePair<string, object>)comboBox1.SelectedItem;
if (pair.Key == "option1")
{
    ...
}

Out of curiosity, why are you using the Dictionary itself as a Value? Couldn't you just use a List or HashSet?

Member Avatar for cool_intentions

Out of curiosity, why are you using the Dictionary itself as a Value? Couldn't you just use a List or HashSet?

I don’t know. Till now, I worked only with Dictionary. I do not understand this very well.

Maybe we misunderstood. I’m having ifElseBranchActivity in my workflow and I’m using Code Condition. I need some code to place in Code Condition.

private void MyCodeCondition(object sender, ConditionalEventArgs e)
        {
           // e.result = comboBox1.SelectedItem == "option1";
        }

How can I write something like this?

Thanks for your answer, but I’m new in this end I didn’t understood it.

I have never actually worked with it, but to get your result:

private void MyCodeCondition(object sender, ConditionalEventArgs e)
        {
           e.result = ((KeyValuePair<string, object>)comboBox1.SelectedItem).Key == "option1";
        }

Or to make your code slightly more readable:

private void MyCodeCondition(object sender, ConditionalEventArgs e)
        {
           KeyValuePair<string, object> pair = (KeyValuePair<string, object>)comboBox1.SelectedItem;
           e.result = pair.Key == "option1";
        }

Basically, just because a ComboBox displays a string, doesn't mean that the SelectedItem is a string. The Dictionary<> class is a collection of KeyValuePair<> which is what the ComboBox uses for each item. If you look at the Dictionary class, notice it inherits ICollection<KeyValuePair<TKey, TValue>> and IEnumerable<KeyValuePair<TKey, TValue>>. I believe it uses ICollection to determine each item for the ComboBox (though it could be IEnumerable, I can't remember off the top of my head).

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.