Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1957902
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T07:56:52+00:00 2026-05-17T07:56:52+00:00

Initially I had the following: [Flags] public enum QueryFlag { None = 0x00, CustomerID

  • 0

Initially I had the following:

[Flags]
public enum QueryFlag
{
    None = 0x00,
    CustomerID = 0x01,
    SalesPerson = 0x02,
    OrderDate = 0x04
}

As check boxes are checked/unchecked, I would add/remove flags from:

QueryFlag qflag;

My idea – when the user clicks the Search button, I would iterate the actual flags set in qflag to modify a .Where clause in my LINQ to Sql. However, Enum.GetValues(qflag.GetType()) returns all the values of the QueryFlag itself. Not helpful.

My solution:

class myForm : Form
{
    List<QueryFlag> qflag = new List<QueryFlag>();

    private void chkOrderDate_CheckedChanged(object sender, EventArgs e)
    {
        if (chkOrderDate.Checked && !qflags.Contains(QueryFlag.OrderDate))
            qflags.Add(QueryFlag.OrderDate);
        else
            qflags.Remove(QueryFlag.OrderDate);
    }

    private void cmdSearch_Click(object sender, EventArgs e)
    {
        if (qflags.Count == 0)
        {
            rtfLog.AppendText("\nNo search criteria selected.");
            return;
        }

        foreach (QueryFlag flag in qflag)
        {
            rtfLog.AppendText(string.Format("\nSearching {0}", flag.ToString()));

            // add switch for flag value
        }
    }
}

public enum QueryFlag
{
    CustomerID,
    SalesPerson,
    OrderDate
}

I have 3 check boxes and this works without any issues to this point. But I am wondering if there is a better way to perform this iteration.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T07:56:52+00:00Added an answer on May 17, 2026 at 7:56 am

    The way you had it originally was correct; you just got messed up by the Enum.GetValues method. This method returns every defined value for a particular enum type, yes. So what you do with this is check each defined value against your particular enum value to see whether the defined value is set within your value.

    That is, you should do this:

    private void chkOrderDate_CheckedChanged(object sender, EventArgs e)
    {
        if (chkOrderDate.Checked)
        {
            qFlag |= QueryFlag.OrderDate;
        }
        else
        {
            qFlag &= (~QueryFlag.OrderDate);
        }
    }
    

    …and likewise for your other CheckBoxes. Then when you want to enumerate what flags you have set:

    static IEnumerable<QueryFlag> GetFlags(QueryFlag flags)
    {
        foreach (QueryFlag flag in Enum.GetValues(typeof(QueryFlag)))
        {
            // Presumably you don't want to include None.
            if (flag == QueryFlag.None)
            {
                continue;
            }
    
            if ((flags & flag) == flag)
            {
                yield return flag;
            }
        }
    }
    

    In fact, you could even abstract the above into a handy extension method for all enum types:

    public static class FlagsHelper
    {
        // This is not exactly perfect, as it allows you to call GetFlags on any
        // struct type, which will throw an exception at runtime if the type isn't
        // an enum.
        public static IEnumerable<TEnum> GetFlags<TEnum>(this TEnum flags)
            where TEnum : struct
        {
            // Unfortunately this boxing/unboxing is the easiest way
            // to do this due to C#'s lack of a where T : enum constraint
            // (there are ways around this, but they involve some
            // frustrating code).
            int flagsValue = (int)(object)flags;
    
            foreach (int flag in Enum.GetValues(typeof(TEnum)))
            {
                if ((flagsValue & flag) == flag)
                {
                    // Once again: an unfortunate boxing/unboxing
                    // due to the lack of a where T : enum constraint.
                    yield return (TEnum)(object)flag;
                }
            }
        }
    }
    

    So your cmdSearch_Click handler could look like this:

    private void cmdSearch_Click(object sender, EventArgs e)
    {
        if (qFlag == QueryFlags.None)
        {
            rtfLog.AppendText("\nNo search criteria selected.");
            return;
        }
    
        foreach (QueryFlag flag in qFlag.GetFlags())
        {
            rtfLog.AppendText(string.Format("\nSearching {0}", flag.ToString()));
    
            // add switch for flag value
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I initially designed my system following the s# architecture example outlined in this codeproject
Java was initially slow before the JIT but today performance is pretty close to
I need to generate buttons initially based on quite a processor and disk intensive
I have a modal popup that initially shows some content but expands a div
Right, initially ran: c:\regsvr32 Amazing.dll then, (accidentally - I might add) I must have
Currently Tomcat's login support redirects users back to where they initially were when the
Announced today at PDC. Initially made up of a Service Bus, the Workflow Service,
I have a function I've written that was initially supposed to take a string
Our team is developing a rather big ASP.NET web project which initially started in
Hi I've got a DIV section that has only its title visible initially. What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.