Re: Piped delimited string to int
- From: Fariba <Fariba@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 17 Apr 2006 10:27:21 -0700
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.
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.
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.
- Follow-Ups:
- Re: Piped delimited string to int
- From: Nick Hounsome
- Re: Piped delimited string to int
- 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
- Piped delimited string to int
- Prev by Date: Re: DrawString() and lines measures
- Next by Date: Re: Why finally?
- Previous by thread: Re: Piped delimited string to int
- Next by thread: Re: Piped delimited string to int
- Index(es):
Relevant Pages
|