Public Enum ERight
ECanInvite = 0
ECanCreate = 1
ECanDelete = 2
etc...
End Enum
Public Enum EUserType
EAdministrator = 0
EPartner_level1 = 1
EPartner_level2 = 2
ENormalUser = 3
...etc
End Enum
This below sub, on rights.add, sometimes throws this error: An item with the same key has already been added.
How is this even possible?
Private Shared rights As Dictionary(Of ws_garuda.EUserType, List(Of ERight)) = Nothing
Private Sub initRoles()
rights = New Dictionary(Of EUserType, List(Of ERight))
rights.Clear()
' Set all rights to false for all roles
For Each usertype As EUserType In DirectCast([Enum].GetValues(GetType(EUserType)), EUserType())
rights.Add(usertype, New List(Of ERight))
Next
End sub
The error is happening because you’re writing a
Sharedobject in a multithreaded environment.The fix is to add a lock (in which case the call to
Clear()becomes redundant):