0

Operator '!=' cannot be applied to operands of type 'string[]' and 'string' (Yes I know similar questions have been asked before, I looked at one but the context/ elements being used were of different type so I'd appreciate help with my case :) )

I have a file and I want to stop reading as soon as I hit a line which starts with "0" in the file. My while loop is having issues with the inequality operator.

private void button1_Click(object sender, EventArgs e)
{
    // Reading/Inputing column values

    OpenFileDialog ofd = new OpenFileDialog();
    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {

        string[] lines = File.ReadAllLines(ofd.FileName).Skip(8).ToArray();
        textBox1.Lines = lines;

        while (lines != "0") // PROBLEM Happens Here
        {

            int[] pos = new int[3] { 0, 6, 18 }; //setlen&pos to read specific colmn vals
            int[] len = new int[3] { 6, 12, 28 }; // only doing 3 columns right now


            foreach (string line in textBox1.Lines)
            {

                for (int j = 0; j < 3; j++) // 3 columns
                {
                    val[j] = line.Substring(pos[j], len[j]).Trim(); // each column value in row add to array
                    list.Add(val[j]); // column values stored in list

                }

            }

        }
    }
user2788405
  • 195
  • 2
  • 5
  • 16

2 Answers2

5

You get the error because lines is a string[] not a single string. But you want to stop on the first line that starts with "0" anyway. So you could add the check in the foreach:

foreach (string line in lines)
{
    if(l.StartsWith("0")) break;
    // ...

However, i would use this method instead to get only the relevant lines:

var lines = File.ReadLines(ofd.FileName).Skip(8).TakeWhile(l => !l.StartsWith("0"));

the difference is that ReadLines does not need to process the whole file.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Hey tim, great post. I just have a question regarding the "l => !l.StartsWith". This syntax seems very interesting, especially the l=>. Anyways, thanks a ton! – user2788405 Oct 14 '13 at 20:12
  • That's called lamda-expression and is usually used with [Linq](http://msdn.microsoft.com/en-us/library/vstudio/bb397926.aspx), you can read about [here](http://msdn.microsoft.com/en-us/library/vstudio/bb397687.aspx). Apart from that, what _is_ your question? Consider to accept an answer. – Tim Schmelter Oct 14 '13 at 20:21
  • Hey tim your method worked, thanks! Another question. Is there any way to have more than one statement in "Skip"? Can I also write .Skip(8 && l.StartsWith("0"); – user2788405 Oct 17 '13 at 19:54
  • @user2788405: You would chain methods, e.g. `Where(l => l.StartsWith("0")).Skip(8)...` . But what do you want to skip? 8 lines that start with zero, 8 lines that don't start with zero or the first 8 lines? You can chain LINQ methods as you want and mostly the order does not even matter. Have a look at my own question according to the order of LINQ methods at a time i didn't understand LINQ either ;) http://stackoverflow.com/questions/10110013/order-of-linq-extension-methods-does-not-affect-performance – Tim Schmelter Oct 17 '13 at 20:35
2

string[] lines is an array and you are checking it with a string.

It should be something like:

//to check if an element is an empty string
lines[index] != "" 

//to check if the array is null
lines != null 

//to check if the array has any elements
lines.Count() != 0 
lines.Length != 0
Kaf
  • 33,101
  • 7
  • 58
  • 78