1

This is my code , i am using 2 dimension array .

top = 0;
if(rowcol[i-1][j]){ top = rowcol[i-1][j] }

But it shows this error :

Cannot implicitly convert type 'string' to 'bool'

Full code :

 string[] fileData = File.ReadAllLines(@"C:\Users\apr13mpsip\Desktop\OneOrganizer\OneOrganizer\WordPuzzle\testing.txt");

        string[] lineValues;

        int row = 0;
        int col;

        string[][] rowcol = new string[fileData.Length][];

        foreach (string line in fileData)
        {
            lineValues = line.Split(new string[] { "," }, StringSplitOptions.None);


            rowcol[row] = new string[lineValues.Length];

            col = 0;

            foreach (string value in lineValues)
            {
                rowcol[row][col] = value;
                col++;
            }
            row++;

        }


        for (int i = 0; i < rowcol.GetLength(0) ; i++)
        {
            for (int j = 0; j < rowcol[i].GetLength(0) ; j++)
            {


                int iadd =  i+1 <rowcol.GetLength(0) ? i + 1: 0;
                int iminus =   i-1> 0  ? i - 1 : 0;
                int jadd =  j+1 <rowcol.GetLength(0) ? j + 1 : 0;
                int jminus = j-1 > 0 ? j - 1 : 0;
                var self = rowcol[i][j]; // current value

                top = 0;
if(rowcol[iminus][j]){ top = rowcol[iminus][j] } // ERROR HERE
}
}

i am actually reading from a textfile and creating something out of it by creating rows and cols 10x10 in a jagged array. so i could find their value by rowcol[][] .

2 Answers 2

3

because

if(rowcol[i-1][j])

will return string as per your definition

string[][] rowcol

and if statement needs bool. you need to check it against something like

if(rowcol[i-1][j] == "stringtotest")
Sign up to request clarification or add additional context in comments.

Comments

2

Only boolean expression can be used as if condition, for example:

if (!string.IsNullOrEmpty(rowcol[i-1][j]))

2 Comments

how do i return a null if it is empty
@user2376998, not sure if I understood you question correctly. string.IsNullOrEmpty returns true if string in test is either null or empty, and false otherwise. So if you use the condition above as is, top will be assigned only when rowcol[i-1][j] is neither null nor empty. Is this what you are looking for?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.