.net - using a pointer to a value type in a Dictionary value -


this small snippet of code increment count value (integer) stored in dictionary using referenced object key. when dictionary small, multiple lookups aren't big deal particular dictionary can quite large.

private refcount idictionary(of ilifetimemanaged, integer) ......... code here.....  private sub incrementrefcount(byval entity ilifetimemanaged)      dim prevcount integer      ''# if have no reference entry, add 1 , set count 1      if not refcount.trygetvalue(entity, prevcount)         refcount.add(entity, 1)     else         ''# otherwise increment count 1          refcount.item(entity) = prevcount + 1     end if  end sub 

i find corresponding dictionary entry increment int stored in value, or add new dictionary entry.

is bad idea use pointer dictionary value? can avoid second key lookup when have gotten value. how implement it? possible in .net4?

can using intptr think? http://msdn.microsoft.com/en-us/library/system.intptr.aspx

refcount.item(entity) = prevcount + 1 

you cannot make pointer given type in vb in c++. however, can wrap value type in reference type semantics want.

public class ref(of t structure)      public sub new()     end sub     public sub new(byval value t)         me.value = value     end sub      public property value t  end class 

this lets return "pointer" integer (more correctly, reference containing integer). write this:

private refcount idictionary(of ilifetimemanaged, ref(of integer)) ......... code here.....  private sub incrementrefcount(byval entity ilifetimemanaged)     dim count integer      ''# if have no reference entry, add 1 , set count 1     if not refcount.trygetvalue(entity, count)         refcount.add(entity, new ref(of integer)(1))     else         ''# otherwise increment count 1         count.value += 1     end if end sub 

you add conversion methods between t , ref(of t) ref class possibly simplify syntax (like in add call). in opinion, doing give closer c++ references c++ pointers. whether that's want or not you.

edit re edit: intptr designed represent pointer type in interop code calls. perhaps better name have been nativeptr. there no way use intptr in managed code way think want, pointer in c++.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -