Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# operator ! cannot be applied

i wonder why ! cannot be applied

this is my code :

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

                     var top = !((rowcol[i-1][j])) ? rowcol[i-1][j] : '';
                    var bottom = !(rowcol[i+1][j]) ? rowcol[i+1][j] : '';
                    var left = !(rowcol[i][j-1]) ? rowcol[i][j-1] : '';
                    var right = !(rowcol[i][j+1]) ? rowcol[i][j+1] : '';


                }
            }

i have a jagged array that , i am reading the values from a textfile . i am having error with operator ! cannot be applied to string , but i and j is int , , yes rowcol is reading a string from a textfile .

please tell me if u need the full code . Help is appreciated thanks

like image 574
user2376998 Avatar asked Dec 04 '25 03:12

user2376998


2 Answers

The issue is that rowcol[i-1][j] is a string, and ! cannot be applied to a string. The same applies for each of your four lines.

Edit: If your goal is to check that the string is not null or empty, try instead:

var top = !(String.isNullOrEmpty(rowcol[i - 1][j])) ? rowcol[i - 1][j] : '';

and so on, or, if you know that the string will be null and not empty,

var top = (rowcol[i - 1][j]) != null) ? rowcol[i - 1][j] : '';
like image 113
qaphla Avatar answered Dec 06 '25 16:12

qaphla


Try:

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

         var top = !(rowcol[i-1][j]=="") ? rowcol[i-1][j] : '';
        var bottom = !(rowcol[i+1][j]=="") ? rowcol[i+1][j] : '';
        var left = !(rowcol[i][j-1]=="") ? rowcol[i][j-1] : '';
        var right = !(rowcol[i][j+1]=="") ? rowcol[i][j+1] : '';
    }
}

Or,

for (int i = 0; i < rowcol.GetLength(0); i++)
{
    for (int j = 0; j < rowcol[i].GetLength(0); j++)
    {
        var top = rowcol[i-1][j]!="" ? rowcol[i-1][j] : '';
        var bottom = rowcol[i+1][j]!="" ? rowcol[i+1][j] : '';
        var left = rowcol[i][j-1]!="" ? rowcol[i][j-1] : '';
        var right = rowcol[i][j+1]!="" ? rowcol[i][j+1] : '';
    }
}
like image 30
afzalulh Avatar answered Dec 06 '25 18:12

afzalulh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!