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

  • SEARCH
  • Home
  • 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 6736627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:09:09+00:00 2026-05-26T11:09:09+00:00

Here is what i am trying to do. I have two bit fields in

  • 0

Here is what i am trying to do.

I have two bit fields in my database:

Archived(bit)
Deleted(bit)

What i would like to do is calculate a int depending on their vaules.

As an example, this would be in my model:

class Person {

Int32 Status {get;set;}

}

If Archived = true set the status to 1,

If Deleted = true set the status to 2,

If both are false, set the status to 0.

I want to do this with out using If statements, maybe there is some sort of boolean arithmetic that i can use?

  • 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-26T11:09:10+00:00Added an answer on May 26, 2026 at 11:09 am

    Why don’t use an enum?

    [Flags]
    public enum PersonState
    {
        None = 0,
        Archived = 1,
        Deleted = 2,
        Both = Archived | Deleted
    }
    
    class Person
    {
        private PersonState status;
    
        public PersonState Status
        {
            get { return this.status; }
            set { this.status = value; }
        }
    
        public bool IsArchived
        {
            get
            {
                return (this.status & PersonState.Archived) != 0;
            }
            set
            {
                if (value)
                    this.status |= PersonState.Archived;
                else
                    this.status &= ~PersonState.Archived;
            }
        }
    
        public bool IsDeleted
        {
            get
            {
                return (this.status & PersonState.Deleted) != 0;
            }
            set
            {
                if (value)
                    this.status |= PersonState.Deleted;
                else
                    this.status &= ~PersonState.Deleted;
            }
        }
    }
    

    You can directly cast an enum value to integer.

    int x = (int)person.Status;
    

    And you can do the contrary, if you have an int.

    person.Status = (PersonState)integerValue;
    

    Also if you don’t provide the possibility that Deleted and Archived can coexists together this is actually a possibility with booleans.
    The number of possible values encoded by n boolean values is 2^n, so, since you have 2 booleans, you have 4 possible values, 00, 01, 10 and 11.

    The problem is in the problem itself: it is wrong to encode that information with booleans. It should be an enum with only 3 possible values also in the DB.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here's what I'm trying to do : On a page, I would have a
Here's what i'm trying to do. I have a project in /var/www/project. I'd like
I'm trying to produce a page that would function a bit like a digital
Here's what I'm trying to do : I have an entity Task with a
Here is a problem I am trying to solve: I have an irregular shape.
Here is what I am trying to do. I have a block of text
Here's what I'm trying to do. I have an ASP.NET MVC web application, where
I have an interesting problem here I've been trying to solve for the last
Here's what I'm trying to do, and failing... I have a File model which
Here is my custom class that I have that represents a triangle. I'm trying

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.