CryoEnix Posted June 26, 2007 Posted June 26, 2007 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... Quote Chaos is merely logic beyond human comprehension
CryoEnix Posted June 26, 2007 Author Posted June 26, 2007 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 ^^) Quote Chaos is merely logic beyond human comprehension
mandelbrot Posted June 26, 2007 Posted June 26, 2007 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. Quote
CryoEnix Posted June 26, 2007 Author Posted June 26, 2007 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... ... } Quote Chaos is merely logic beyond human comprehension
mandelbrot Posted June 26, 2007 Posted June 26, 2007 I'm afraid I'm not sure on that. It may be a casting problem, but my C# isn't that hot... Let me have a look and I'll get back to you. Paul. Quote
CryoEnix Posted June 26, 2007 Author Posted June 26, 2007 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! Quote Chaos is merely logic beyond human comprehension
mandelbrot Posted June 27, 2007 Posted June 27, 2007 You're quite welcome, Cryo, and thanks for highlighting that - I'll remember it for when I do (eventually) switch to C#. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.