Main Site Documentation

[Solved]Ref out and pointers


#1

Hi Everyone.

I have a Hashtable that consist in pair<condition to check, result of check>. This represents a hashtable of conditions.

I want to have a list of instruction that use these conditions. The instructions is therefore composed of a set of conditions, and I want to evaluates instructions to true if all the conditions that belong to the instructions are evaluated to true.

The list of conditions that composed an instruction is build with

ar_list_of_res_cond.Add(Hashtableofconditions[c]);

and the Hash table of conditions is initialized with:

Hashtableofconditions.Add(c,(bool)false);

c being a condition.

Finaly the hashtable of instruction is build with:

HashtableofInstructions.Add(i.ID,ar_list_of_res_cond);

i.ID being the unique key (in this case an integer).

Conditions are evaluated by a separate thread and printing out the elements that exist into the HashTable of Conditions shows correct results sometimes conditions are true when they should be and go back to false when conditions are not verified.

The problem is that the hashtable of instructions contains values (i.e. results of tested conditions) that are always False… This seems therefore that

ar_list_of_res_cond.Add(Hashtableofconditions[c]);

do not add reference to the value but the value itself. The problem is that

ar_list_of_res_cond.Add(ref Hashtableofconditions[c]);

is not allowed.

Do you have any tricks to advice?


#2

Could you store only the condition “c” in ar_list_of_res_cond, and then use it to lookup the current value of the condition in Hashtableofconditions ?


#3

Hello Jasdev and thanks for your reply to this tricky post. In fact I did the following:

I added the pairs<instruction_ID, Ar_list_of_conditions> to the HashTable of Instructions as you suggested. Therefore the hash table of instructions contains the list of conditions instead of the list of results of these conditions. Then I I look for the current value of conditions in Hashtable of conditions.

This works but seems to me totally unefficient due to the fact that the HashTable of instructions should handle ref to results of conditions and not the conditions, checking conditions in HashTable of conditions involves a big overhead… Any other idea?


#4

Hi leforban,

In your example the following


Hashtableofconditions[c]

returns a Value type not Reference type, now there is such a thing as boxing in .net, in which value types can be used like reference types, but it has its overheads.

I would suggest wrapping the Boolean value inside a class with a property and adding that to your Hashtableofconditions

so


Hashtableofconditions.Add(c,new WrapperObj() { Evaluated = false});

Then when using your results ar_list_of_res_cond, you’ll have to call the Evaluated property
Does that make sense


#5

Hello KartAssist, yes this make sense I think I will have time to check that today or tomorrow. I will let you know


#6

Hi Kart Assist

Before your comment, code was updating values in HtC with the following assignement

HtC[de.key]=true;

with de being a dictionnary entry of HtC, this was really slow. I follow your advice and encapsulate the boolean value in a class. Results are really better now.

Thanks for your help!!!