Solved

Writing Bool Functions

Posted on 2010-09-04
11
618 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
Comment Utility
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
Comment Utility
You are right
0
 
LVL 21

Expert Comment

by:Dale Burrell
Comment Utility
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
 

Author Comment

by:GRChandrashekar
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
you need to assign value to Result parameter before return false; statement
0
 
LVL 1

Accepted Solution

by:
yamir2k earned 500 total points
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now