-
February 10th, 2010, 05:44 PM
#1
Feels like too many If's!
I am developing an app for a mobile device and it feels like I am using way too many If / else statements.
The scenario is I have a number of picture boxes which change colour on being clicked in. I check the coolour of each picture box via the colour whether or not a number is stored into an int array.
i.e:
for (int i = 0; i < 6; i++)
{
if (PictureBox1.BackColor == Color.Red)
{
Number[i] = 1;
}
else if (PictureBox2.BackColor == Color.Red)
{
Number[i] = 2;
}
else if (PictureBox3.BackColor == Color.Red)
{
Number[i] = 3;
}
else if (PictureBox4.BackColor == Color.Red)
{
Number[i] = 4;
}
}
Is there any other way I could go about this? or is this my only bet?
Thanks.
P.S - the if list is a lot bigger than what is pasted here
-
February 10th, 2010, 06:03 PM
#2
Re: Feels like too many If's!
P.S - I just noticed my code will not work properly, and simply flood the array with the first value it goes too.. hehe oops.. either way the question still remains. Thanks.
-
February 10th, 2010, 06:42 PM
#3
Re: Feels like too many If's!
What do You want to think of doing here please explain us better...
-
February 10th, 2010, 10:19 PM
#4
-
February 11th, 2010, 04:17 AM
#5
Re: Feels like too many If's!
Originally Posted by demise
I check the coolour of each picture box via the colour whether or not a number is stored into an int array.
do you really understand your code?
I have no idea what you are going to do. to me it looks like that you are setting the value in the "Number" array accordingly to the BackColor of you picture boxes.
@demise: is it really so hard to use the code tags?
Code:
for (int i = 0; i < 6; i++)
{
if (PictureBox1.BackColor == Color.Red)
{
Number[i] = 1;
}
else if (PictureBox2.BackColor == Color.Red)
{
Number[i] = 2;
}
else if (PictureBox3.BackColor == Color.Red)
{
Number[i] = 3;
}
else if (PictureBox4.BackColor == Color.Red)
{
Number[i] = 4;
}
}
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 11th, 2010, 04:47 AM
#6
Re: Feels like too many If's!
The code does the following:
There is a number of picture boxes, when some are selected they will change their back colour to red. Then I need to know the positions of which were changed to red. So the following code would look to see if seperately each picturebox does have the red colour. If it does, it stores it's number (position) into the number array.
Note: Yes, I know this does not work, I posted it very late. It does work however when I amend it as so:
Code:
for (int i = 0; i < 6; i++)
{
if (PictureBox1.BackColor == Color.Red)
{
Number[i] = 1;
PictureBox1.BackColor = Color.Black;
}
else if (PictureBox2.BackColor == Color.Red)
{
Number[i] = 2;
PictureBox2.BackColor = Color.Black;
}
else if (PictureBox3.BackColor == Color.Red)
{
Number[i] = 3;
PictureBox3.BackColor = Color.Black;
}
else if (PictureBox4.BackColor == Color.Red)
{
Number[i] = 4;
PictureBox4.BackColor = Color.Black;
}
}
I just feel the method is messy.. hence asking for a better solution and advice in any form.
Thanks.
-
February 11th, 2010, 04:54 AM
#7
Re: Feels like too many If's!
Originally Posted by demise
There is a number of picture boxes, when some are selected they will change their back colour to red.
ok, so you handle some event to change the back color to red?
Originally Posted by demise
Then I need to know the positions of which were changed to red.
I don't understand this part. do you just want to know which picture box is selected (red) ?
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 11th, 2010, 05:02 AM
#8
Re: Feels like too many If's!
Originally Posted by memeloo
ok, so you handle some event to change the back color to red?
Yes, there is a click event, which will change the backcolour to red if it is black. Or black if it is red.
Originally Posted by memeloo
I don't understand this part. do you just want to know which picture box is selected (red) ?
Yes, I want to know which picture boxes are selected. For example:
pb1 pb2 pb3
pb4 pb5 pb6
Assuming the above are picture boxes.
If pb1 and pb6 were red, I would want to remember these picture boxes were red for a future use. Therefore I want to create the number array and store which are red. In this case, pb1 and pb6 (1 and 6).
-
February 11th, 2010, 05:08 AM
#9
Re: Feels like too many If's!
here's my suggestion:
Code:
// create a helper list
HashSet<PictureBox> selectedPictureBoxes = new HashSet<PictureBox>();
// and update it in your selected event handler
void your_picturebox_selected_eventhandler(...)
{
if(PictureBox1.BackColor == Color.Red)
selectedPictureBoxes.Add(PictureBox1)
else
selectedPictureBoxes.Remove(PictureBox1);
}
then you can use it somewhere else with the "for each" loop
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 11th, 2010, 05:14 AM
#10
Re: Feels like too many If's!
That seems like a good idea I will give that a go. Thank you very much.
-
February 11th, 2010, 05:17 AM
#11
Re: Feels like too many If's!
I have just tried to find the HashSet in its namespace using System.Collections; but it seems to only find HashTable. (I dont know if HashSet is part of the .Net Compact Framework?)
-
February 11th, 2010, 05:34 AM
#12
Re: Feels like too many If's!
Originally Posted by demise
I have just tried to find the HashSet in its namespace using System.Collections; but it seems to only find HashTable. (I dont know if HashSet is part of the .Net Compact Framework?)
Hi there.
The class is defined in System.Collections.Generic. I've not come across it before so I'll have to look into it. It appears to be new in .NET 3.5. It doesn't seem to be supported in the Compact Framework though. At least it's not stated on MSDN (HashSet of T).
-
February 11th, 2010, 05:38 AM
#13
Re: Feels like too many If's!
Originally Posted by nelo
I've not come across it before so I'll have to look into it.
it's very usefull if you have to do merge and difference etc. operations on collections.
Originally Posted by nelo
It appears to be new in .NET 3.5. It doesn't seem to be supported in the Compact Framework though. At least it's not stated on MSDN ( HashSet of T).
oops, I forgot that this is a mobile device with the compact version of the framework. then you have to use some other collection to store the selected picture boxes but the concept stays the same
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 11th, 2010, 06:31 AM
#14
Re: Feels like too many If's!
I have a slight problem.. (again ).
I implemented an ArrayList to hold the picture boxes.
However.. when the form closes and reopen later on. I try to restore the picture boxes colour via:
Code:
foreach (PictureBox pbox in ptemp)
{
pbox.BackColor = Color.Red;
}
ptemp being the ArrayList retrieved from another class.
I recieve the error message: Object Disposed Exception was unhandled.
Am I right to assume once the form closed, the ArrayList no longer points to existing picture boxes and therefore cannot be used? How can I go about this?
Thanks.
-
February 11th, 2010, 07:04 AM
#15
Re: Feels like too many If's!
Originally Posted by demise
Am I right to assume once the form closed, the ArrayList no longer points to existing picture boxes and therefore cannot be used?
that's right.
Originally Posted by demise
How can I go about this?
you'll have to recognize them by names for example: PictureBox.Name and you'll have to store their names in the array instead of object references.,
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|