Click here to Skip to main content
15,914,642 members
Home / Discussions / C#
   

C#

 
Questionhelp me Pin
daniel_leto2322-Aug-11 18:09
daniel_leto2322-Aug-11 18:09 
AnswerRe: help me Pin
thatraja22-Aug-11 18:22
professionalthatraja22-Aug-11 18:22 
AnswerRe: help me Pin
daniel_leto2322-Aug-11 18:33
daniel_leto2322-Aug-11 18:33 
GeneralRe: help me Pin
Not Active22-Aug-11 19:01
mentorNot Active22-Aug-11 19:01 
AnswerRe: help me PinPopular
RobCroll22-Aug-11 19:09
RobCroll22-Aug-11 19:09 
GeneralRe: help me Pin
Smithers-Jones22-Aug-11 20:58
Smithers-Jones22-Aug-11 20:58 
QuestionStackOverflowException Pin
abolfazl delbari22-Aug-11 13:08
abolfazl delbari22-Aug-11 13:08 
AnswerRe: StackOverflowException [modified] PinPopular
Luc Pattyn22-Aug-11 13:32
sitebuilderLuc Pattyn22-Aug-11 13:32 
Hi,

1.
at the very bottom of your cycle() method is a call to method cycle(), so when all the i,j combinations make your test
if (i == Math.Pow(2, j))...

fail, your method calls itself with the same input value, clearly ending up in an "infinite loop". This will happen for the majority of f-values, as most numbers simply aren't a power of 2.

2.
I don't know why you would want to call cycle again; it is even unclear to me why you would need two for loops. And if your supported input range is limited (as the presence of a switch construct suggests), you also !! don't need a switch !!

I think the code below would come close to what you want (it also clears some checkboxes, something your code didn't do at all):

void myBinaryCheckboxes1(int f) {
    cbx1.Checked= (f & 0x0001)!=0;
    cbx2.Checked= (f & 0x0002)!=0;
    cbx3.Checked= (f & 0x0004)!=0;
    cbx4.Checked= (f & 0x0008)!=0;
    cbx5.Checked= (f & 0x0010)!=0;
    cbx6.Checked= (f & 0x0020)!=0;
    cbx7.Checked= (f & 0x0040)!=0;
    cbx8.Checked= (f & 0x0080)!=0;
    cbx9.Checked= (f & 0x0100)!=0;
    cbx10.Checked=(f & 0x0200)!=0;
}

BTW: in binary (and in many other situations), things would get numbered starting by zero (so cbx0 up to cbx9).

3.
For the really advanced implementation, you could further simplify if you would have a collection (array, list, whatever) holding those checkboxes; it would suffice to put them in their own GroupBox (nothing else in that GroupBox is easiest). And then have the checkbox tags hold their bit value (the hex numbers in the above code). The final code could then be as simple compact as:
void myBinaryCheckboxes2(int f) {
    foreach(Control c in myGroupBox.Controls) {
        ((CheckBox)c).Checked=(f & (int.Parse((string)f.Tag)!=0;    // note 1
    }
}

BTW: If that looks too weird/convoluted, just stick with the code in myBinaryCheckboxes1()!

[ADDED] Note 1, triggered by lukeer's reply: depending on how the Tag gets assigned, one might not need the int.Parse, one could just assign a numeric tag right away, then use ((CheckBox)c).Checked=(f & (int)f.Tag)!=0; [/ADDED]

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
modified on Tuesday, August 23, 2011 8:11 AM

GeneralRe: StackOverflowException Pin
abolfazl delbari22-Aug-11 14:48
abolfazl delbari22-Aug-11 14:48 
AnswerRe: StackOverflowException Pin
Luc Pattyn22-Aug-11 15:23
sitebuilderLuc Pattyn22-Aug-11 15:23 
SuggestionRe: StackOverflowException Pin
lukeer22-Aug-11 20:46
lukeer22-Aug-11 20:46 
AnswerRe: StackOverflowException [modified] Pin
Luc Pattyn23-Aug-11 1:47
sitebuilderLuc Pattyn23-Aug-11 1:47 
GeneralRe: StackOverflowException Pin
PIEBALDconsult23-Aug-11 3:13
mvePIEBALDconsult23-Aug-11 3:13 
AnswerRe: StackOverflowException Pin
Luc Pattyn23-Aug-11 3:28
sitebuilderLuc Pattyn23-Aug-11 3:28 
GeneralRe: StackOverflowException Pin
PIEBALDconsult23-Aug-11 3:34
mvePIEBALDconsult23-Aug-11 3:34 
QuestionRe: StackOverflowException Pin
Luc Pattyn23-Aug-11 5:46
sitebuilderLuc Pattyn23-Aug-11 5:46 
AnswerRe: StackOverflowException Pin
PIEBALDconsult23-Aug-11 14:16
mvePIEBALDconsult23-Aug-11 14:16 
GeneralRe: StackOverflowException Pin
GParkings1-Sep-11 7:35
GParkings1-Sep-11 7:35 
GeneralRe: StackOverflowException Pin
Luc Pattyn1-Sep-11 8:11
sitebuilderLuc Pattyn1-Sep-11 8:11 
JokeRe: StackOverflowException Pin
PIEBALDconsult22-Aug-11 14:47
mvePIEBALDconsult22-Aug-11 14:47 
GeneralRe: StackOverflowException Pin
abolfazl delbari22-Aug-11 15:00
abolfazl delbari22-Aug-11 15:00 
Questionvirtual USB device Pin
Grimes22-Aug-11 11:03
Grimes22-Aug-11 11:03 
AnswerRe: virtual USB device Pin
Rutvik Dave22-Aug-11 11:26
professionalRutvik Dave22-Aug-11 11:26 
GeneralRe: virtual USB device Pin
Grimes22-Aug-11 11:41
Grimes22-Aug-11 11:41 
AnswerRe: virtual USB device Pin
Bernhard Hiller22-Aug-11 22:29
Bernhard Hiller22-Aug-11 22:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.