Solved

Writing Bool Functions

Posted on 2010-09-04
11
626 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

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!

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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