Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hey guys, long time no speak - I've got a bit of an issue here, but I'm sure one of you will be able to help...

 

 

Lets say that I've got several applications with a number of permissions allowed for each, stored within a single long data type:

 

 

App A is worth 1

App B is worth 2

App C is worth 4

App D is worth 8

App E is worth 16

App F is worth 32

 

And so on, ad infinitum.

 

Now the problem is, say if I need to know if the user has an access code of 45, how can I calculate whether he has access to application C or not - I know there's an algorithm for it, I just can't put my finger on it...

Chaos is merely logic beyond human comprehension
Posted
Sorry for not clarifying, but I'm working in C# for this one - although the algorithm itself shouldn't be too difficult to translate should it be in any other language (just no PERL please ^^)
Chaos is merely logic beyond human comprehension
Posted

Hi Cryo,

 

 

Look up the <Flags()> attribute for enumerations - very very useful...

<Flags()> _
Public Enum Apps
   None = 0
   AppA = 1
   AppB = 2
   AppC = 4
   AppD = 8
End Enum

After this, you simply need to set the property to point to this enumeration and use logical operators to manage the checking...

Public Class MyApp
   ...
   Private _AppType As Apps
   ...
   Public Property AppType() As Apps
       Get
           Return _AppType
       End Get
       Set(ByVal value As Apps)
           _AppType = value
       End Set
   End Property
   ...
End Class
...
...
   If (thisApp.AppType And Apps.AppA) = Apps.AppA Then
       'Do your stuff...
       ...
   End If

Does that help?

 

 

Paul.

Posted

Cheers paul, I have that in VB, but the C# port doesn't seem to like this - is disagrees with the double ampersand. Any ideas?

 

if ((thisApp.AppType && Apps.AppA) == Apps.AppA ){//'Do your stuff...        ...   
}

Chaos is merely logic beyond human comprehension
Posted

It's alright Paul, I've figured it out. Turns out that the double ampersand is just for boolean comparisons - a single is for binary, which is why it didn't work. It seems that the Basic 'And' statement does either, depending on the situation.

 

Thanks anyway!

Chaos is merely logic beyond human comprehension

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...