|
Hello,
First let me say that I think it's a very good question!
And I have no idea if there is a nice solution for that.
I looked thre the Enum members and haven't found something obvious.
An (very ugly) solution (or let's say workaround), would be.
Use the ToString() method to get a string like: "Control, A";
Than Trim and Split it.
and with the help of Parse you will get the Enum Members:
iterate over the stringarray which is returned from the string.Split(',') method.
Keys actKey = (Keys)Enum.Parse(typeof(Keys), stringarray[x], true);
I hope there is a nicer solution out there! (But maybe it helps you)
All the best,
Martin
All the best,
Martin
|
|
|
|
|
Hi,
first of all I hope your enum behaves as a flags collection (and hence
it better have the [Flags] attribute).
There is a very nice trick to enumerate the individual bits in an int
without using a loop that checks each and every bit:
int work=val;
while (work!=0) {
int singleBit=work & -work;
work=work^singleBit;
... do whatever you need to do with singleBit
}
Applied to enums, this leads to:
[Flags]
enum myFlags {a=1, b=2, c=4, d=8}
public void Run() {
myFlags flags=myFlags.a | myFlags.c | myFlags.d;
int work=(int)flags;
while (work!=0) {
int singleBit=work & -work;
work=work^singleBit;
myFlags mySingleFlag=(myFlags)singleBit;
log(mySingleFlag.ToString());
}
}
this little example prints the characters a, c and d as it should.
|
|
|
|
|
Gah! Looks like you beat me to it while I was writing up my example, and you got the enumeration part down too! I bow to you.
-Phil
|
|
|
|
|
Great!
How do people come up with this stuff?!
I can never get myself to think outside of the box like that!
Anywayz... Why didn't Microsoft implement this kind of iteration too??
It can be very useful in some cases where you want to create something generic...
|
|
|
|
|
Hi,
casting back and forth between an enum and an int is standard practice;
examples are everywhere, even in MS documentation.
Tricky code snippets to manipulate bits (and do many other things), have been
discovered one by one over time; I started my collection of those many years ago...
I even have some books on them, some work only in assembly code, some only
on specific processors, and some (like the current one) can be applied everywhere
even in a high-level language.
|
|
|
|
|
shyagam wrote: Why didn't Microsoft implement this kind of iteration too??
You are absolutely right: they could have provided a GetEnumerator() method
that would throw an exception if the enum is not [Flags], and returns an
enumeration of the enum values that are present in the enum at hand.
But they didnt.
|
|
|
|
|
|
Hello,
So your solution would mean to iterate threw the enum members and do the bit test?
Luc Pattyn wrote: (and hence
it better have the [Flags] attribute).
Is there a rule in the Framework when they use the Attribute?
All the best,
Martin
|
|
|
|
|
Hi Martin,
[Flags] or [FlagsAttribute] is explained in the FlagsAttribute page on MSDN.
It is rather long and not very important.
One thing is it influences how ToString() works, which returns symbolic names
when possible; with Flags it returns a combintation of one or more symbolic names,
without Flags it does not.
Unfortunately, when specifying [Flags] it still is possible to assign values
that are NOT powers of 2 as in:
[FlagsAttribute]
enum myFlags3 { a=1, b=2, c=15, d=8 }
In fact they do recommend you do this with the example Read=1, Write=2, ReadWrite=3
but I would like the possibility to get an error when I do this accidentally.
Greetings.
|
|
|
|
|
Hi,
Martin# wrote: So your solution would mean to iterate threw the enum members and do the bit test?
No, my code does not even has to know what all the possible enum values are;
the only thing it is interested in is the fact that they are Flags, not arbitrary
values.
Watch my while-loop carefully, it does not perform an Enum.GetValues()
and it does not iterate over the bits (as in for(int bit=0; bit<32; bit++)...);
it automatically loops a number of times, equal to the number of bits set
in the enum value !
|
|
|
|
|
Fantastic, got it now!
Again my '5' for that!
All the best,
Martin
|
|
|
|
|
Well, I don't know how to iterate through all POSSIBLE values of an enum, but given that you say you do, I think I have a solution for you.
Keys keys = Keys.Control | Keys.A;
foreach(Keys key in POSSIBLEVALUES){
if(keys & key) == key){
}
}
Note: the enum value have to be lined up in powers of 2 in order for this to work properly so you can distinguish the values when you do a bitwise-or.
The following is a bit of code that shows this:
enum Keys
{
A = 1,
B = 2,
C = 4,
D = 8
}
static void Main(string[] args)
{
Keys keys = Keys.A | Keys.B | Keys.D;
if ((keys & Keys.A) == Keys.A)
{
Console.WriteLine("A");
}
if ((keys & Keys.B) == Keys.B)
{
Console.WriteLine("B");
}
if ((keys & Keys.C) == Keys.C)
{
Console.WriteLine("C");
}
if ((keys & Keys.D) == Keys.D)
{
Console.WriteLine("D");
}
|
|
|
|
|
Yeah, well I guess that's the first solution I came up with too, but this would mean iterating through the entire enum, which in my case is very "expensive", as the Keys enum is quite large...
|
|
|
|
|
FYI, here's another way to do it w/ Enum.Parse method, which is undoubtably slower and takes more memory, but improves readability (maybe )
[Flags]
enum Keys
{
A = 1,
B = 2,
C = 4,
D = 8
}
static void Main(string[] args)
{
Keys keys = Keys.A | Keys.B | Keys.D;
string[] strKeys = keys.ToString().Split(',');
foreach(string key in strKeys){
Keys newKey = (Keys)Enum.Parse(keys.GetType(),key);
Console.WriteLine(newKey.ToString());
}
}
|
|
|
|
|
System.Media won't do event with multi-threading
DirectX... well I just don't know much MDX...
[ still getting Loader-lock warnings... ]
thx
Murphy
|
|
|
|
|
You could try p/invoking sndPlaySound (in Winmm.dll) and test some of the flags available there.
Or look into the waveout API and perform playing yourself.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
if you wanna be not useful, combine both of the music files to a one file by reading the binary data, then play the new file. it's smart but unuseful
|
|
|
|
|
hi all,
i want to sort my datagridview but i am not able to do it so far.
predefined Sort function does the sorting for string values.
for example: let's say i have 1,8,5,4,10 in cell[0]
after sorting i get: 1,10,4,5,8
but i want: 1,4,5,8,10
is there anyone knows how to sort a numeric column in datagridview ?
thanks in advance...
MFA
|
|
|
|
|
here is a bubble sort for you...
for (i=0; i<n-1; i++)="" {
="" for="" (j="0;" j<n-1-i;="" j++)
="" if="" (a[j+1]="" <="" a[j])="" {="" *="" compare="" the="" two="" neighbors=""
="" tmp="a[j];" swap="" a[j]="" and="" a[j+1]="" }
}
=""
<div="" class="ForumSig">The Devil Lives in all of us, It's up to you to let HIM/HER out!!!!!
|
|
|
|
|
Hi,
sorting a DataGridView is no different than sorting any other collection:
- they have a Sort() method that applies a default sort order
- they have a Sort(IComparer) method that takes an object capable of
comparing two objects of the type you want to get sorted.
So read up on IComparer, and provide a little class that inherits from it.
The world must be full of examples of this.
|
|
|
|
|
Hi All,
I want to pass a vector from a C++ DLL to a .Net Client, can any one help me on how do i Marshal a vector using Pinvoke, I dint found any article related to this on the net.
The vector i want to pass is a vector of user defined structures
Regards,
Subodh
|
|
|
|
|
Calling all Crystal Report Gurus
I have this strange error coming up in one of my reports.
"One or more fields could not be found in the result set. Use verify Database to update report. Error in file ---------------- . the rowset column could not be found."
this is one of the last reports in my app and the delivery is delayed on account of this error.
There doesn't seem to be any apparent reason for this error.
Let me give you guys the background
1. I am using Visual studio 2005 and the CR verson which comes bundled with it.
2. I am using a CR viewer and the report source is an Reportdocument object
3. I have a record selection formula
as follows
if {?ClientSelect} = 'All' then true else {InvoiceSaleAnalysis;1.ClientID}={?ClientID}
I am setting the values of the above params thru front end code
When I view the report with 'All' (default it shows ok. However when i select a particular ClientID and change the param and refresh the report it gives me the above error the first time and when i refresh it again the required report is displayed
any ideas would be of help
Romi
|
|
|
|
|
Hi all.
I have created an application which access a send and receive method written in a class
now the problem which i am facing is strange when i call the recieve method of udp client object , and then if i want to stop the code of my application, it is not closing properly..when i checked ,,i found that recieve method of udp client creates all the problem.is there any way to play around..or any one can help me where i can found a good code for receiving...the main thing is that recieve method of udpclient should be always be listening...it should be in continous loop..
pls help me..
Regards
DilipRam
|
|
|
|
|
I had a similar situation not too long ago. To solve the problem I used the following snippet in a separate thread; to close the port just set UDPrunning to false, wait for the thread to exit and then you can close the port. I'm not sure if it's the best solution, but it is a solution that worked in my situation.
while (UDPrunning == true)
{
if (udpSocket.Available > 0)
{
try
{
IPEndPoint msgSenderInfo = new IPEndPoint(IPAddress.Any, 0);
Byte[] incomingData = udpSocket.Receive(ref msgSenderInfo);
}
catch (SocketException e)
{
}
}
else
{
Thread.Sleep(100);
}
}
|
|
|
|
|
Hi thanks for the reply
But i have doubt regarding one of ur suggestions.
You comment was
"to close the port just set UDPrunning to false, wait for the thread to exit and then you can close the port"
here the variable UDP running i have set to false in the close event of my application.and after seting the variable i am closing the port..is this the what you meant...if it is then no problem but i got confused that you were speaking abt the wait for the thread how should i wait for thread..
can you pls explain.
Regards
DilipRam
|
|
|
|