Comparison between String and Dictionary's Key Value

Hi community,

I’ve a string value like : Value= “5”
and one dictionary of type (string, object) like: [ “No1 : 1”,
“No2 : 8”]

I need to check whether the given string exist between the value of dictionary’keys ( i.e. No1 & No2) or not.

Thanks.

If you’re trying to find out if there is a value of 5 in the dictionary…

For Each : yourDict.Keys (make sure to set TypeArgument of the For Each to String)

  • if yourDict(item).ToString = yourValue.ToString Then assign valueFound = True; Break

Hi @siddhi ,

Assuming that the comparison is in the numeric way, as mentioned we know the keys No.1 & No.2 in the Dictionary.

So, we can just access that values using the keys and then compare with the given value.

CDbl(dictVar("No1")) > CDbl(Value) andAlso CDbl(Value) < CDbl(dictVar("No2")) 

If the assumption is wrong, do let us know more details of what you want to perform.

This needs to be

CDbl(dictVar(“No1”).ToString)

And they don’t want to compare No1 to No2. They want to see if the value 5 is in the dictionary.

1 Like

@postwick ,

You are correct on the .ToString, did miss that out.

I did feel the requirement was a bit vague, so I did assume, hopefully we will be able to understand the correct requirement.

1 Like

@supermanPunch @postwick

yeah correct , I need to check whether my string value (i.e. 5 ) lies between dictionary or not.

NOTE:- dictionary is of type (string, object)

Thanks.

@postwick

you’re comparing dictionary keys .
But in my dictionary as i explained above keys are ‘No1 & No2’ and their respective values are ‘1 & 8’ . I need to check my string value (i.e 5 ) lies between the dictionary’keys value or not.

I hope you are more clear now what m trying to say.

Thanks.

Oh then you need to do what @supermanPunch said:

yourValue > CDbl(yourDict(“No1”).ToString) AND yourValue < CDbl(yourDict(“No2”).ToString)

This assumes yourValue is an integer variable. If it is a string, then you’ll have to use CDbl(yourValue) instead of just yourValue.

@supermanPunch @postwick
Thanks , It works.

Could you please tell me this too

Having two strings named
‘tryValue’ holds value E5
'AllowedCode ’ holds value C1,V3,E4,R3,E5.

I’ve applied this logic to check whether the “tryValue” is present in “AllowedCode” variable or not.
As it is present but still it’s going in the else part.

You have it backwards. It should be…

AllowedCode.Contains(tryValue)

Also, you don’t really need to do the AllowedCode variable. You can just do…

Var_config(“AllowedH…”).ToString.Contains(tryValue)

Also, since it’s a comma delimited list, I recommended splitting it into an array. Imagine you have…

66E,5E,44E

And your tryValue is 6E. If you just do AllowedCode.Contains(tryValue) then it will return true, because “6E” does exist in the string. What you really want to know is not “is it in this string” - you want to know “is it in this list of values” so do it like this.

Split(Var_config(“AllowedH…”).ToString,“,”).Contains(tryValue)

What that does is split the comma delimited string into an actual array, and then check within the array’s values for a match.

1 Like

Thanks @postwick for the help.

1 Like

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