-1

I have this code that take a 2d array and turns them into strings to be written as a file, it checks 4 comboBoxes in the attempt to not write the line that has all 4 comboBox values. what is happening though is it is not writing any line with any of the combo box values. please help and thanks

if (grid[i][3] != comboBox4.Text && grid[i][0] != comboBox1.Text && grid[i][1] != comboBox2.Text
            && grid[i][2] != comboBox3.Text)
            {
                string gridstring = String.Join(",", grid[i]);
                MessageBox.Show(gridstring);
                tw.WriteLine(gridstring);
            }

some example pseudo code

line1 = a,b,c,d
line1 = b,c,d,e
line1 = e,f,g,h
line1 = h,I,j,k

if line does not cantain a,b,c,d then write, so that only line 1 would not be written, but currenly it would skip the first 2

animuson
  • 53,861
  • 28
  • 137
  • 147
  • 3
    Have you tried debugging? – eddie_cat Jun 26 '14 at 19:56
  • 2
    Have you tried debugging with the debugger and stepping through to see what values are in your variables? In particular, `grid`. It seems like you're not getting to the MessageBox line at all if it's not popping up, which means the if statement is evaluating to false. – eddie_cat Jun 26 '14 at 19:57
  • 1
    What is the desired behavior? Can you provide an example? – BradleyDotNET Jun 26 '14 at 19:57
  • 2
    If you click on the left "dead space" in Visual Studio, a red circle will appear. That circle is called a breakpoint. When you run your program, the flow will halt at the breakpoint. Then, you can hover over any of the variables (or even the conditions themselves) and see why this is evaluating to false (if it is). – Theodoros Chatzigiannakis Jun 26 '14 at 20:02
  • I appreciate the feedback, but I still don't get it. Show what is in each combo box and in each grid row, and give the expected output. – BradleyDotNET Jun 26 '14 at 20:02
  • It sounds like the 1 based arrays issue that I was asking about in a memory leaking question.... http://stackoverflow.com/questions/24535483/memory-leak-using-pywin32com-for-opc the code in the question is a sync read which is very similar to the write. You can see a python version of doing those functions in opclib on bitbucket. – clutton Jul 09 '14 at 19:05
  • You are not allowed to edit your question to *completely* change it into something else. That's not how our site works. Please do not do this again. – animuson Jul 25 '14 at 20:23
  • i have absolutely no way to start a new thread, nor correct the post to allow me to continue using this site. so in effect I cannot use this site any longer. with that in mind this system is fairly hostile towards new users – user3761370 Aug 11 '14 at 20:18

1 Answers1

2

It's better to check for the possible match, and skip that line then to only include those that are an exception to the rule. This makes the code a little easier to read.

if (grid[i][0] == comboBox1.Text 
 && grid[i][1] == comboBox2.Text
 && grid[i][2] == comboBox3.Text
 && grid[i][3] == comboBox4.Text)
        {
            continue;
        }
else
{
    string gridstring = String.Join(",", grid[i]);
    MessageBox.Show(gridstring);
    tw.WriteLine(gridstring);
}

Without seeing what's in grid and the comboBox# variables it's difficult to say what else could break this.

RemarkLima
  • 11,639
  • 7
  • 37
  • 56
Reactgular
  • 52,335
  • 19
  • 158
  • 208