|
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
|
|
|
|
|
That's exactly what I meant! To stop listening all you have to so is set that var to false and continue on your way.
The comment about waiting for the thread is because I was running that method in a separate Thread from the gui thread. When I was shutting down my gui I wanted to make sure that the listener thread exited so that it wouldn't hang up my application. The Thread.Join() method waits for a thread to exit, so my socket shutdown code looked like this:
UDPrunning = false;
udpSocketThread.Join();
udpSocket.Close();
|
|
|
|
|
Hi,
I would not like a Join in my closing code, it might sit and wait forever.
Isnt the essence of the problem that you want the UDP listener thread to be
a background thread, one that does not prevent your app from exiting ?
|
|
|
|
|
Hi
can you bit clear please as i am new this socket programming and any way i am calling the receive method in a thread...
Regards
DilipRam
|
|
|
|
|
That essentially was the idea but there are 2 reasons that I didn't set udpSocketThread.IsBackground = true; and forget about it:
1) The worker function is coded to avoid a blocking situation where the thread would hang so I wasn't worried about it waiting forever and
2) I didn't previously know about the Background vs. Foreground Thread property. That's a pretty nifty Property to have, thanks for the info!
|
|
|
|
|
Yes, but my point is when the listener is in its Receive() method, it needs
another packet to resume its course, and only after that it could exit the loop,
so your Join() is actually waiting for another UDP packet to arrive (which
may never happen).
|
|
|
|