Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to use boolean Function using csharp in windows application.

my code as follows;

C#
private Boolean SaveDetails()
       {
           //validation for textbox not empty

           bool Check = true;
           if (txt_Faccode.Text.Trim().Length == 0)
           {
               Check = false;
               MessageBox.Show("Please enter the Faculty code","Not Enter the details",MessageBoxButtons.OK,MessageBoxIcon.Information);
               return Check;
           }

           if (Txt_Facname.Text.Trim().Length == 0)
           {
               Check = false;
               MessageBox.Show("Please enter the Faculty Name","Not Enter the details",MessageBoxButtons.OK,MessageBoxIcon.Information);
               return Check;
           }

           if (txt_Hrs.Text.Trim().Length == 0)
           {
               Check = false;
               MessageBox.Show("Please enter the allocated hours","Not Enter the details",MessageBoxButtons.OK,MessageBoxIcon.Information);
               return Check;
           }


           //insert code for update to the DataBase
           this.Cursor = Cursors.WaitCursor;
           try
           {
               sql = "insert into Tb_SCH_Faculty_Details  ([Faculty_Code], [Faculty_Name],[Allocated_Hours]) " + " values('" + txt_Faccode.Text + "','" + Txt_Facname.Text + "', " + txt_Hrs.Text + ")";

               int temp = 0;
               if (!int.TryParse(txt_Hrs.Text.Trim(), out temp))
               {
                   Check = false;
                   MessageBox.Show("Enter Numbers only in allocated hours", "Characters Not Allowed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                   return Check;
               }

               GFun.Error = "";
               GFun.InsertAccessData(sql);
               if (GFun.Error.ToString() != "")
               {
                   Check = false;
                   MessageBox.Show(GFun.Error.ToString(), "Error");
                   this.Cursor = Cursors.Arrow;
                   return Check;
               }

               GFun.OleDbCon.Close();
           }
           catch (Exception ex)
           {
               Check = false;
               MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               this.Cursor = Cursors.Arrow;
               return Check;
           }
             this.Cursor = Cursors.Arrow;
       }
       private void Btn_Save_Click(object sender, EventArgs e)
       {
           if (SaveDetails == true)
               MessageBox.Show("Record Inserted Successfully", "Records Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);
           else
               MessageBox.Show("Not Inserted Successfully", "Records Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);
       }


i use the boolean function,when i run error shows as follows in the following line;

FrmFacultyDetails.SaveDetails()': not all code paths return a value
(the above error shows in private Boolean SaveDetails())


Operator '==' cannot be applied to operands of type 'method group' and 'bool'
( the above error shows in if (SaveDetails == true))

please help me.

from my above what is the mistake i made.

please help me.

using csharp.
Posted
Updated 17-Feb-13 17:39pm
v2
Comments
Sergey Alexandrovich Kryukov 17-Feb-13 23:53pm    
Please do us a favor: comment the lines in text where the error message appears, save some time...
—SA
Sergey Alexandrovich Kryukov 18-Feb-13 0:04am    
Forget it, but next time do it: format code with "pre" block, comment the lines which cause error messages, exceptions in question, etc.
I answered your question in full, please see.
But you are supposed to read and understand error messages, which are quite straightforward.
—SA

hi dear,

please add
C#
return check

after your line
C#
this.Cursor = Cursors.Arrow;

in your function.

and in your click event modify statement from
C#
if (SaveDetails == true)

to
C#
if (SaveDetails() == true)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-13 13:45pm    
Aha, save SaveDetails() == true... You probably know something, but still have no clue about programming. I would advise you to avoid giving any answers.
Sorry, my 1 again.
—SA
Mr. Mahesh Patel 18-Feb-13 23:39pm    
Ok dear, I am sorry, I never give you any answer if you dont like
Hi
Here is your code refactored. You did not return the final value as exactly said in the error message.


C#
private bool SaveDetails()
        {
            //validation for textbox not empty
            if (string.IsNullOrEmpty(txt_Faccode.Text))
            {
                MessageBox.Show("Please enter the Faculty code", "Not Enter the details", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (string.IsNullOrEmpty(Txt_Facname.Text))
            {
                MessageBox.Show("Please enter the Faculty Name", "Not Enter the details", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (string.IsNullOrEmpty(txt_Hrs.Text))
            {
                MessageBox.Show("Please enter the allocated hours", "Not Enter the details", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }


            //insert code for update to the DataBase
            this.Cursor = Cursors.WaitCursor;
            try
            {
                sql = "insert into Tb_SCH_Faculty_Details  ([Faculty_Code], [Faculty_Name],[Allocated_Hours]) " + " values('" + txt_Faccode.Text + "','" + Txt_Facname.Text + "', " + txt_Hrs.Text + ")";

                int temp = 0;
                if (!int.TryParse(txt_Hrs.Text.Trim(), out temp))
                {
                    MessageBox.Show("Enter Numbers only in allocated hours", "Characters Not Allowed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }

                GFun.Error = "";
                GFun.InsertAccessData(sql);
                if (!string.IsNullOrEmpty(GFun.Error.ToString()))
                {
                    MessageBox.Show(GFun.Error.ToString(), "Error");
                    this.Cursor = Cursors.Arrow;
                    return false;
                }

                GFun.OleDbCon.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Cursor = Cursors.Arrow;
                return false;
            }

            this.Cursor = Cursors.Arrow;
            return true;
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (SaveDetails())
            {
                MessageBox.Show("Record Inserted Successfully", "Records Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Not Inserted Successfully", "Records Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }



Regards
Jegan
 
Share this answer
 
In the click event handler, add () after SaveDetails in the if statement.
 
Share this answer
 
OK, I see. First, the method should always return some Boolean value. Make sure all your "if" branches return something.
And, in second error, there are two.

First, the "if" blocks like if (something == true) {/* ... */} are not completely incorrect, they are just quite silly. If something can be compered with true it is Boolean, but the condition is already Boolean. This expression is fully equivalent to if (something) {/* ... */}. Comparison is totally redundant, its only purpose it to cause some laugh. :-) (But I cannot laugh at it anymore, after so many similar questions, it's rather boring :-)). I hope this is clear now.

And the real bug now. You are comparing with SaveDetail. There is no such variable or field/property member. This is a name of the function, but not the call, which could be SaveDetail(/* ... */). It just makes no sense, hence the error message.

—SA
 
Share this answer
 
v2

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