Check if a QueueItem.SpecificContent("key") has a value

A. Not String.IsNullOrEmpty(io_TransactionItem.SpecificContent("Name").ToString)
B. io_TransactionItem.SpecificContent("Name").ToString <> ""
C.o_TransactionItem.SpecificContent("Name").Equals(Nothing)

Checking with these methods works in most cases but sometimes I still get with all of them an exception:

An exception has occurred: Object reference not set to an instance of an object. at Source: If Account

In the If account I check if I have set an element there, in this particular case I’m using C, but got the same for the rest as well…

io_TransactionItem.SpecificContent.ContainsKey("Name").ToString
Just checking for the key works but I would like to see if it is set or the key exists only if it is not empty?

What do you suggest for a best practice on checking content of a QueueItem?

2 Likes

Well the Best Practice would be to follow C option.

1 Like

Can you point out your reasoning or docs that would suggest it do it in C way?

1 Like

I would go for combination like this one:

tra.SpecificContent.Keys.ContainsKey("Condition") AndAlso Not String.IsNullOrWhiteSpace(tra.SpecificContent("Condition").ToString)

As with the first condition you check if the key exist, if does, it then checks if the value is filled.
Otherwise, it will return false (even by only checking the first statement.

Cheers

2 Likes

Cool, also… didn’t know about AndAlso :slight_smile:

1 Like

I’m using an if as you proposed:
io_TransactionItem.SpecificContent.ContainsKey("Accounts") AndAlso Not String.IsNullOrWhiteSpace(io_TransactionItem.SpecificContent("Accounts").ToString)

But this gives sometimes, pretty often this exception:

An exception has occurred: Object reference not set to an instance of an object.

I separated these two conditions to successive log messages and there is a case when the first validates to true and the second gives the above mentioned exception. The same exception is triggered if I just simply try to get io_TransactionItem.SpecificContent("Accounts").ToString. So this checking mechanism still should be fine tuned :smiley:… Any ideas on how to?

1 Like

Can you try this type of thing and see if it handles it?

  If io_TransactionItem.SpecificContent.ContainsKey("Accounts") Then
        assign strContent = If(io_TransactionItem.SpecificContent("Accounts") Is Nothing, "", io_TransactionItem.SpecificContent("Accounts"))
           assign  boolEmpty = String.IsNullOrWhiteSpace(io_TransactionItem.SpecificContent("Accounts").ToString)
    End If

The dictionary may have but containing a null value, which is making the .toString fail.

Actually maybe be best to go with this type of Linq:

io_TransactionItem.SpecificContent.Any(Function(x) x.Key = “Accounts” AndAlso x.Value IsNot Nothing AndAlso Not String.IsNullOrWhiteSpace(x.Value.ToString))

Cheers

5 Likes

Yes, checking also for Nothing ends the circle… a bit cumbersome to have 3 checks for value check but understandable… Maybe there should be an activity to Retrieve a Queue item to which variables can be imported as outputs that are filled if their given key exists.

1 Like

This last expression should check it all at once, right?

2 Likes

Right, answer accepted.

2 Likes

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