Re: Piped delimited string to int
- From: "Nick Hounsome" <News@xxxxxxxxxxxxxxxxxx>
- Date: Mon, 17 Apr 2006 23:14:08 GMT
"Fariba" <Fariba@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:A65216F3-18EE-465F-8774-650AC3692AD0@xxxxxxxxxxxxxxxx
Hello Nick
1) Clearly the flags are separated by commas rather than "|" in the
string
format.
I cannot call the method using comma delimited strings, I have to call the
method with integral values which are bitwise or'ed.
My program is not about strings unless you actually want to use them - the
enums will just be displayed as strings in the debugger which makes them
easier to maintain.
You can just pipe the enums together and cast the enum to int whenever you
need it as an int.
here is the way I can call the method:
usrgrpService.AddRole("Group_Name", "Description", 0x10000000|0x00000002);
As you can see for the third paramtere ( 0x10000000|0x00000002), it is not
string and it is int32 hexadecimal format.
Whether it is hex or not is irrelevant unless you have a particular
requirement to display it as hex.
[Flags]
enum Perms
{
Perm1 = 0x10000000,
Perm2 = 0x00000002
}
Perms all = Perms.Perm1 | Perms.Perm2;
int x = (int) all;
x will have exactly the same value as 0x10000000|0x00000002
No strings are needed.
You can populate the CheckedListBox using Enum.GetNames.
When you need to call AddRole you can get the values of all checked items
using Enum.Parse on the item, put them
together with |=, cast the result to int and call the method.
Thanks
"Nick Hounsome" wrote:
Try playing around with things a bit. I did and I came up with the
following:
namespace FlagsTest
{
class Program
{
[Flags]
enum MyEnum :int
{
Flag1 = 0x01,
Flag2 = 0x02,
Flag3 = 0x10
}
static void Main(string[] args)
{
MyEnum e1 = MyEnum.Flag1 | MyEnum.Flag2;
Console.WriteLine(string.Format("e1={0}", e1));
e1 |= MyEnum.Flag3;
Console.WriteLine(string.Format("e1={0}",e1));
int i1 = (int)e1;
Console.WriteLine(string.Format("i1=0x{0:X}",i1));
string s1 = e1.ToString();
MyEnum e2 = (MyEnum)Enum.Parse(typeof(MyEnum), s1);
Console.WriteLine(string.Format("e2={0}", e2));
Console.ReadLine();
}
}
}
This gives:
e1=Flag1, Flag2
e1=Flag1, Flag2, Flag3
i1=0x13
e2=Flag1, Flag2, Flag3
I made a couple of errors in what I said before:
1) Clearly the flags are separated by commas rather than "|" in the
string
format.
2) I used a stupid example where the flags were not disjoint and this
causes
ToString() to revert to numeric format(which can still be written and
parsed
but isn't so readable). NB You CAN use compound flags such as
Flag4=Flag1|Flag2 and it will use them in the string.
[Flags] and [FlagsAttribute] both refer to the FlagsAttribute class. It's
just a shorthand. Most people, including me, use the shorthand but on the
whole I think that it should never have been introduced.
"Fariba" <Fariba@xxxxxxxxxx> wrote in message
news:%232s29IcYGHA.4836@xxxxxxxxxxxxxxxxxxxxxxx
Hello Nick,
I tried the enum way ,but "|=" does not seem to be working. It always
return Flag1 and never bitwaise other flags .Is it [Flags] or
[FlagsAttribute]?
Thanks
"Nick Hounsome" <News@xxxxxxxxxxxxxxxxxx> wrote in message
news:rHq0g.105659$zI1.98918@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
"Fariba" <Fariba@xxxxxxxxxx> wrote in message
news:%233isTPSYGHA.1196@xxxxxxxxxxxxxxxxxxxxxxx
Hello ,
I am trying to call a mthod with the following signature:
AddRole(string Group_Nam, string Description, int permissionmask);
Accroding to msdn ,you can mask the permissions using pipe symbol
.for
example you can use something like this
AddRole("My Group", "Test", 0x10000000|0x00000002);
Porblem is ,I am getting this permission mask from a checkboxlist and
I
have stored all these numbers (like 0x00000002) in its value
property.
When use selects multiple checkboxes I dynamically generate a pipe
delimited string and when I convert it to int (either using
int.Parse(my_pipe_delimited_string) or
Convert.Int32(my_pipe_delimited_string)) I get object reference set
"Object reference not set to an instance of an object".
if I just mention 0x10000000|0x00000002 in the method that's fine
,but
it seems I cannot dynamically generate it from the user's input.
Your help is much appreciate.
The right way to do this is to modity an int directly.
const int Flag1 = 0x100000000;
int flags;
// on check
flags |= Flag1;
// on uncheck
flags &= ~Flag1;
// better still
[Flags]
enum MyFlags
{
Flag1 = 0x1000000,
Flag2 = 0x1000001,
......
}
MyFlags flags;
// exact same code for check and uncheck then cast to int for the
method
call (which should ideally be changed to use the enum anyway.
Note that if you use an enum with the FlagsAttribute then ToString()
will
give you a string like "Flag1|Flag2" which makes debugging much
easier.
Also - if you set the Tag property of the check box to the flag value
then you can do it without any ugly conditional code or loads of event
handlers.
.
- References:
- Piped delimited string to int
- From: Fariba
- Re: Piped delimited string to int
- From: Nick Hounsome
- Re: Piped delimited string to int
- From: Fariba
- Re: Piped delimited string to int
- From: Nick Hounsome
- Re: Piped delimited string to int
- From: Fariba
- Piped delimited string to int
- Prev by Date: Consuming managed events within Internet Explorer
- Next by Date: How do I get Array elements from Reflection C#
- Previous by thread: Re: Piped delimited string to int
- Next by thread: Re: Piped delimited string to int
- Index(es):
Relevant Pages
|