Update Dictionary Value if Key exists

So I have this Dictionary<Uielement, Double>. I’m looping through a bunch of rows, and adding things to this dictionary like the label of the product And the price . Sometimes I’ll have more than one row of the same product. If that’s the case, If that product is already in the dictionary I want to only add the double portion to whatever value the product key already has. So for example.

If I have Apple—> 3.00
And I come across another apple row, I’m checking to see if that key exists, if it does, then I want to add the extra value,
Apple---- > 2

Now the apple in my dictionary should contain the value 5.00.

How do I add this value onto the existing one. Dictionary.UpdateValue?

Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary[“test”] = 1;

If I understand, you want to basically just add the current value with the new value.

First, you need to initialize your dictionary, which I assume you are already.
Then, I think you can just use one assign activity with inline If condition (or use an If activity if you choose)
Assign dict("Apple") = If(dict.Keys.Contains("Apple"), dict("Apple") + newvalue, newvalue)

2 Likes

So I’ve actually run into a different issue, that I have to solve before I can move on to this part. So I’m looking through a shopping cart, and I’m taking each row in the shopping cart, and adding it to the dictionary. The key is the products Label saved as a UiElement And the Value is the product Price.

I need to check if the product label already exists in the dictionary.

Because the product label Uielement on row 1 is not equal to the product label on row 2, when I use the “Key Exists” Activity, it says, those two keys are not equal. Even if the product label both says
Row 1: Apple---- 1.00
Row 2: Apple----2.00

How can I check to see if the UiElement exists in the dictionary, by checking the Uielements aaname or something… I want to check that each product, although on different rows, they are technically the same product, but their Uielements are not the same. I’m totally stuck here…

I need to search through the dictionary by using the Uielements “aaname”. Because even though the elements are different elements because they are on different rows, they should have the same aaname I believe. I’m just not sure how to search through the dictionary to see that a key---->UiElement----->With aaname that is equal to the current Elements aaname is either true or false. What is the best way to word that… Or is it not possible.

Just wondering, but do you really need to use a dictionary with uielement as the keys. It might make more sense to use the aaname instead as a string. Instead of doing dict(element), you could do dict(aaname) or dict(“Apple”) or dict(row(“Fruit”).ToString.Trim)

If you do use the uielement, though, you will need to identify the element in the keys that contains the aaname, which might be more technical.
For example
element = dict.Keys.Where(Function(e) e.GetType("aaname").Equals(row("Fruit").ToString.Trim) ).First
…or something like that apologies, it might have a mistake
Then, use that element as your key, like dict(element)

Another way, is if you already have an uielement from Find Element activity or other steps, then you can simply use that in both the dict, and filter the row using the aaname of that element to find the value that goes with the aaname.

However, like I said, I kind of think it might be simpler to use a string as the key rather than an uielement. Here is an example:

For each row In dt1
  Assign dict(row("Fruit").ToString.Trim) = CDbl(row("Value").ToString)

Then, to use the element create a selector that uses the string for the aaname.
< aaname='"+row("Fruit").ToString.Trim+"' />
…or whatever the selector is, but you can use that string in the aaname attribute.

Hopefully, that makes sense.

If you show part of your workflow, I might be able to provide further advice.

Regards.

I’ll have to think about it. I have been on vacation for a few days, I remember I tried to change it to the aaname as the key before, but I changed it back to the uielement for some reason. I’ll have to check over it again, and see If I can get away with it.

This is what I have right now. This is just the part I’m working on. There’s a ton more above this, but this is inside of a few for each loops. It’s looping through the row labels, and the row prices, and if they are on the same row, it applies and calculates a credit for each row, if there isn’t enough Money left to credit, it adds that amount to my noEbillDictionary, which means, We’re saving this amount to apply the credit in a different way. Like when your internet and phone are out of service. Credits for time out of service. We can only credit what’s available on the e-bill, after that if more credit is required it goes through the no-ebill process. So I need to save the product name, and the amount for later use. Still thinking about if I want to use the aaname. It certainly does seem easier.

I don’t completely understand your screenshot, but looks like product is your key.
So, the condition can be like:
noEbillDictionary.Keys.Contains(product)
Then, assign the existingValue + leftOverCredit to that key, like you were doing.

I’m assuming product is an element so it should already contain the aaname as the product that you are wanting to change. However, I don’t know how you are setting that element variable because it is above your screenshot.

Let’s say you use a Find Element activity, then you can identify the element using the aaname string. This will store the element to an UiElement variable that contains the aaname, so there’s no need to look for the aaname in the element after you have stored it to the variable… if that makes sense.

If you choose to use the aaname the key, the only difference will be that you won’t need to use an UiElement variable, and instead would use a Selector string to identify the element.

EDIT: correction to condition.

Regards.

2 Likes

I’m in the process of converting all of the Dictionary activities to handle the <string, Double> Instead of using the Uielement. I’m hoping it will work.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.