Solved

Writing Bool Functions

Posted on 2010-09-04
11
620 Views
Last Modified: 2012-05-10
Hi I have a bool function to return true or false which is as stated below

private static bool Isduplicaterecordupdate(string description, Int32 accountnatureid)
        {
            try
            {
                var accountnature = new AccountnatureEntity();
                filteraccountnature.Clear();
                var filterdescription =
                    new FieldCompareValuePredicate(AccountnatureFields.Description,
                                                   null,
                                                   ComparisonOperator.Equal,
                                                   ChangeCase.ToUpperCase(description));
                var filterflag = new FieldCompareValuePredicate(AccountnatureFields.Flag, null,
                                                                ComparisonOperator.Equal,
                                                                StandardFlag.recordvalidflag);
                var filteraccountnatureid =
                    new FieldCompareValuePredicate(AccountnatureFields.AccountnatureId, null,
                                                   ComparisonOperator.NotEqual, accountnatureid);
                filterdescription.CaseSensitiveCollation = true;
                filteraccountnature.Add(filterdescription);
                filteraccountnature.Add(filterflag);
                filteraccountnature.Add(filteraccountnatureid);
                adapteraccountnature.FetchEntityUsingUniqueConstraint(accountnature,
                                                                      filteraccountnature);
                return (accountnature.Fields.State) == EntityState.Fetched;
            }
            catch (Exception ex)
            {
                GlobalErrorHandler.LogMessage(ex.Message + ex.StackTrace);
                return true;
            }
            finally
            {
                adapteraccountnature.CloseConnection();
            }
        }

Now, this works fine there is no issues as far as function is concerned.
Problem comes when there is an error ! It return true; Well, I can change this to false Also. But in either case my BI logic may get messed up because I am performing actions based on true or false.

What is the way out to overcome this ?
0
Comment
Question by:GRChandrashekar
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 33602309
Just to understand the situation better, are you saying that ideally you need to know when calling the function whether the result is true, false or error? i.e. a tri-state result?
0
 

Author Comment

by:GRChandrashekar
ID: 33602310
You are right
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 33602336
OK - 2 options really, you could use a nullable return value (http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx) or you can have the return value of the function be whether it executed without error and use and out parameter to contain the result (http://msdn.microsoft.com/en-us/library/t3c3bfhx(VS.80).aspx).
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:GRChandrashekar
ID: 33602351
But in my case when am returing true or false how do i adpot these examples am not clear
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 33602364
Well you have to change the logic where ever this function is called from - there is no other way.

So if you use a nullable type you can return null in the case of an error, and at the point it is called you check whether the result is null before you use it.

Same if you use an out parameter, you check whether there was an error before using the result.


// Using Nullable type
bool ? Result = null;

Result = Isduplicaterecordupdate();

if (Result != null)
{
  // Use result
}
else
{
  // Handle error
}

// Using out parameter
bool Result = false;
bool HasError = false;

HasError = Isduplicaterecordupdate(out Result);

if (!HasError)
{
  // Use result
}
else
{
  // Handle error
}

Open in new window

0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33602930
First of returning true in case of error I believe is wrong. Because the function failed to perform it's intended duty.

Changing it to return false is the correct. Now having changed it to return false has to be adapted everywhere wherever in your Business Logic it's being used. Even if you change it to return nullable boolean you will have to do same changes. Rather I suggest you to make it return false, and make necessary judgment change where its being used.
0
 

Author Comment

by:GRChandrashekar
ID: 33603098
OK going by dale's concept, now what chagnes I need to make in function

private static bool Isduplicaterecordupdate(string description, Int32 accountnatureid)
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 33605735
It depends which of the 2 solutions I proposed you are thinking of using... if the first then change the following:

private static bool? Isduplicaterecordupdate(string description, Int32 accountnatureid)

...

catch (Exception ex)
{
    GlobalErrorHandler.LogMessage(ex.Message + ex.StackTrace);
    return null;
}

if the second


private static bool? Isduplicaterecordupdate(string description, Int32 accountnatureid, out bool Result)

...


    Result = (accountnature.Fields.State) == EntityState.Fetched;
    return true;
}
catch (Exception ex)
{
    GlobalErrorHandler.LogMessage(ex.Message + ex.StackTrace);
    return false;
}
0
 

Author Comment

by:GRChandrashekar
ID: 33616094
Error Here. Says Parameter result must be assigned upon exit

 catch (Exception ex)
            {
                GlobalErrorHandler.LogMessage(ex.Message + ex.StackTrace);
                return false;



            }

0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33616108
you need to assign value to Result parameter before return false; statement
0
 
LVL 1

Accepted Solution

by:
yamir2k earned 500 total points
ID: 33617424
You could implement your method like this (see the code). In this case you don't need to use nullable types.

Btw, don't catch exceptions of Exception class in your code. It's too general, and if you get an unexpected error in your application, your logic may be broken. Better define your own exception class inheriting from Exception class, catch it in your method and throw it to the caller.
//Your method
private static bool Isduplicaterecordupdate(string description, Int32 accountnatureid)
{
  try
  {
    ...
  }
  catch(Exception ex)
  {
    GlobalErrorHandler.LogMessage(ex.Message + ex.StackTrace);
    throw ex; //Throw the exception to the calling method
  }
  finally
  {
    ...
  }
}

//In the calling method

try
{
  bool result = Isduplicaterecordupdate(...)
//Handling your result
  ...
}
catch(Exception ex)
{
//Handling error
  ...
}

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question