[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 631
  • Last Modified:

Writing Bool Functions

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
GRChandrashekar
Asked:
GRChandrashekar
  • 4
  • 4
  • 2
  • +1
1 Solution
 
Dale BurrellCommented:
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
 
GRChandrashekarAuthor Commented:
You are right
0
 
Dale BurrellCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GRChandrashekarAuthor Commented:
But in my case when am returing true or false how do i adpot these examples am not clear
0
 
Dale BurrellCommented:
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
 
Gururaj BadamCommented:
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
 
GRChandrashekarAuthor Commented:
OK going by dale's concept, now what chagnes I need to make in function

private static bool Isduplicaterecordupdate(string description, Int32 accountnatureid)
0
 
Dale BurrellCommented:
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
 
GRChandrashekarAuthor Commented:
Error Here. Says Parameter result must be assigned upon exit

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



            }

0
 
Gururaj BadamCommented:
you need to assign value to Result parameter before return false; statement
0
 
yamir2kCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now