Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
how to use boolean Function using csharp in windows application.
 
my code as follows;
 
 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 17-Feb-13 18:37pm
Edited 17-Feb-13 18:39pm
v2
Comments
Sergey Alexandrovich Kryukov at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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. Smile | :) (But I cannot laugh at it anymore, after so many similar questions, it's rather boring Smile | :) ). 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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

In the click event handler, add () after SaveDetails in the if statement.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi
 
Here is your code refactored. You did not return the final value as exactly said in the error message.
 

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

hi dear,
 
please add
return check
after your line
this.Cursor = Cursors.Arrow;
in your function.
 
and in your click event modify statement from
 if (SaveDetails == true)
to
 if (SaveDetails() == true)
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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 at 18-Feb-13 23:39pm
   
Ok dear, I am sorry, I never give you any answer if you dont like

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,580
1 OriginalGriff 8,370
2 Peter Leow 4,859
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,301


Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100