Solved

Writing Bool Functions

Posted on 2010-09-04
11
625 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
Independent Software Vendors: 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!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 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