Compare boolean values

Posted on 2007-07-20
Last Modified: 2007-12-19
I want to do a boolean match similiary to how we do a stringmatch or integermatch. i have a stringmatch function which I have pasted below and i like to have another function which compares boolean values

in the below function I will have the string FilterValue = Yes or No and
string VehicleValue will be bool VehicleValue.

Can someone tell me how to do it?

private static bool StringMatch(string FilterValue, string VehicleValue)
                  bool IsMatch = true;

                  if (FilterValue != null && FilterValue.Length > 0)
                        IsMatch = (FilterValue.ToLower() == VehicleValue.ToLower());

                  return IsMatch;
Question by:pratikshahse
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    well,your function looks fine... where is the problem?
    LVL 8

    Expert Comment

           IsMatch = (FilterValue.ToLower().Equals(VehicleValue.ToLower()));
    LVL 7

    Accepted Solution

    You could cut that down to:
    private static bool StringMatch(string filterValue, string vehicleValue)
        if (string.IsNullOrEmpty(filterValue)
            return true;
        return string.Compare(filterValue, vehicleValue, true);

    or even:
    private static bool StringMatch(string filterValue, string vehicleValue)
        return string.IsNullOrEmpty(filterValue) || string.Compare(filterValue, vehicleValue, true);

    Though if you aren't too familiar with boolean logic the first is probably clearer and thus preferable. The string.Compare(string1, string2, true) performs a case insensitive comparison without the overhead of string manipulation with ToLower().

    I'm struggling to understand what your problem is. Do you have one string with "Yes"/"No" and another with "True"/"False"? Do you want the same function but with bools instead of strings?
    LVL 22

    Expert Comment

    pratikshahse - If I understand you correctly, you want a boolen compare similar to the string compare above:

    private static bool BoolMatch(bool filterValue, bool vehicleValue)
       return (filterValue == vehicleValue);


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    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…
    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…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 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

    9 Experts available now in Live!

    Get 1:1 Help Now