5 Replies - 427 Views - Last Post: 27 June 2012 - 07:10 AM Rate Topic: -----

#1 Andrew74  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 26-June 11

If statement help. Multi conditions

Posted 26 June 2012 - 10:21 PM

I'm trying to sort this list of movie object I have by year, rating, and quality. I have 3 domain Up Down controls for the variables and check boxes to enable that type of sorting. I bunched it all into one if statement and it's not bring up any results to the data grid. I'm sure I messed up the If statement order or I'm completely doing it wrong.

Any help is much appreciated.
Thanks in advanced.

foreach (MovieData m in Movies)
            {
                if ((m.year.ToString() == domainUpDown1.SelectedItem.ToString() & checkBox1.Checked) &&

                    (m.Rating > double.Parse(domainUpDown2.SelectedItem.ToString()) & checkBox2.Checked) &&

                    (GetQualityIndex(m.Quality) == domainUpDown3.SelectedItem.ToString() & checkBox3.Checked) )
                {
                    n = dataGridView1.Rows.Add();
                    dataGridView1.Rows[n].Cells[0].Value = m.Title;
                    dataGridView1.Rows[n].Cells[2].Value = m.Rating;
                    dataGridView1.Rows[n].Cells[3].Value = m.year;
                    dataGridView1.Rows[n].Cells[5].Value = GetFlagFile(m.flag);
                }
            }

This post has been edited by Andrew74: 26 June 2012 - 10:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: If statement help. Multi conditions

#2 plh  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 13
  • Joined: 23-May 11

Re: If statement help. Multi conditions

Posted 26 June 2012 - 11:33 PM

Why don't you comment out some of the if condition and set a breakpoint inside the if statement to see what condition is failing.
Was This Post Helpful? 0
  • +
  • -

#3 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: If statement help. Multi conditions

Posted 27 June 2012 - 12:21 AM

in order to add movie to datagrid all 3 checkboxes must be checked? is this true?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: If statement help. Multi conditions

Posted 27 June 2012 - 06:44 AM

Why are you mixing your & and && ?
& is a logical/binary additive
&& is 'and' as we humans think of it.
Looking at what you are doing I would be using all &&

Are you familiar with breakpoints and debugging and actually checking your values?

I have my doubts about this actually matching:
m.year.ToString() == domainUpDown1.SelectedItem.ToString()
at the very least I always talk my string comparrisons to lowercase to I am certain that don't fail from "June" != "june" etc.

I wonder about this too
m.Rating > double.Parse(domainUpDown2.SelectedItem.ToString()
because to use the greater than you would becomparing numeric values.
but one of these is a domainUpDown? What values does that hold?
How would a rating be greater than a domain name?
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4463
  • View blog
  • Posts: 7,779
  • Joined: 08-June 10

Re: If statement help. Multi conditions

Posted 27 June 2012 - 07:04 AM

Quote

& is a logical/binary additive
&& is 'and' as we humans think of it.


That's true if the values are numeric. If they evaluate to a boolean, & and && behave similarly with one crucial difference: & doesn't use short-circuit evaluation. & will always evaluate the right side of the equation, even if the left is already false.

Also, a DomainUpDown is like a numeric spinner, except for collections of strings or other objects. Domain means something different than domain name.

Now that that little tidbit is out of the way...

For the sake of all that's holy, don't do 1000 things in a single statement. It's impossible to debug, as you are learning. Your code will be easier to read if you just use variables to represent some of that, or better yet, most of that. Then, when it crashes out or you need to put a breakpoint on it, you can find the single exact operation that's failing.

While you're at it, name your controls something more intuitive as well. If I asked you in six months what "checkBox2" represented, you'd have no idea, I promise.
Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: If statement help. Multi conditions

Posted 27 June 2012 - 07:10 AM

Quote

Also, a DomainUpDown is like a numeric spinner, except for collections of strings or other objects. Domain means something different than domain name.

Right. But its a collection of strings so I wonder how the m.rating is going to be greater than the selected string. if (6 > "PG-13") or whatever just won't work.

Curtis is completely right about breaking down complex lines to simple ones to make debugging easier.

Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?
A:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1