Click here to Skip to main content
15,901,932 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys,

I've problem with bool function always giving me errors, but syntactic and logic are correct, here's the function and the call.
C#
bool exist=false;
exist = checking(stock_code);

public bool checking(int x)
        {
            string i;
            i = Convert.ToString(x);
            for (int j = 0; j < dataGridView1.Rows.Count; j++)
            {
                if (dataGridView1.Rows[j].Cells[0].Value == i.ToString ())
                {
                    exist = true;
                    return true;
                }
                else
                {
                    exist = false;
                    return false;
                }
                //else
                    //return false;
             }
        }
Posted
Comments
[no name] 9-Sep-12 20:16pm    
And we are supposed to guess what "errors" you are getting? Why are you converting x to a string (i) and then convert i to a string again? It's already a string.
Member 8584763 9-Sep-12 20:28pm    
not all code paths return a value

1 solution

It means exactly what is says. Not all of your code paths return a value.

C#
public bool checking(int x)
        {
            for (int j = 0; j < dataGridView1.Rows.Count; j++)
            {
                if (dataGridView1.Rows[j].Cells[0].Value.ToString == x.ToString())
                {
                    exist = true;
                    return true;
                }
                else
                {
                    exist = false;
                    return false;
                }
             }
             return false;
        }
 
Share this answer
 
Comments
Member 8584763 9-Sep-12 20:58pm    
this function always gives me false, even if the value in the data grid view
[no name] 9-Sep-12 21:05pm    
Your code presupposes that x is only in the first column of your grid.
Member 8584763 9-Sep-12 21:07pm    
that's right, the call is inside try and catch. it's given me error like this
Object reference not set to an instance of an object. in catch
[no name] 9-Sep-12 21:10pm    
And which object is null?
Member 8584763 9-Sep-12 21:17pm    
system.nullreferenceexpception:object reference not set to an instance of an object
it's say in these line: in the call and in the condition of the function
exist = checking(stock_code);
if (dataGridView1.Rows[j].Cells[0].Value.ToString () == x.ToString ())

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900