Return value based on multiple bool conditions

Related searches

I have following case:

enum values{
go = 1,
stand = 2,
jump = 3,
run = 4,
go_stand=5,
go_jump=6,
go_run=7,
go_stand_jump=8,
… and so on 
Go_stand_jum_prun=17}

int select value(bool go, bool stand, bool jump, bool run)
{
}

Based on combination of bool values passed to the method I need to return appropriate value, meaning if go=true and the rest false 1 will returned if all parameters passed in will be true 17 will be returns, the same applies for all combinations in between. The Only idea I have it’s a lot of if and else if statements to evaluate possible combinations, which is sort of ugly. The question is there more elegant solution.

Thank you all!!!

Apply FlagsAttribute to your enumeration:

[Flags]
public enum Activity
{
    None = 0,
    Go = 1,
    Stand = 2,
    Jump = 4,
    Run = 8,
}

And make use of the conditional operator to construct your enumeration:

public static Activity CreateActivity(bool go, bool stand, bool jump, bool run)
{
    return
        (go    ? Activity.Go    : 0) |
        (stand ? Activity.Stand : 0) |
        (jump  ? Activity.Jump  : 0) |
        (run   ? Activity.Run   : 0);
}

Complete example:

using System;

public class Program
{
    public static void Main()
    {
        Activity goJump = Activity.Go | Activity.Jump;

        Activity activity =
            CreateActivity(
                go:    true,
                stand: false,
                jump:  true,
                run:   false);

        Console.WriteLine(activity == goJump); // prints "True"
    }

    public static Activity CreateActivity(bool go, bool stand, bool jump, bool run)
    {
        return
            (go    ? Activity.Go    : 0) |
            (stand ? Activity.Stand : 0) |
            (jump  ? Activity.Jump  : 0) |
            (run   ? Activity.Run   : 0);
    }
}

[Flags]
public enum Activity
{
    None = 0,
    Go = 1,
    Stand = 2,
    Jump = 4,
    Run = 8,
}

Best way to check multiple boolean conditions in C# if statements , It is better to capture the intent of the operation instead of explicitly check the boolean values. For example: public void Check() { if� In this article we will discuss different ways to select rows in DataFrame based on condition on single or multiple columns. Following Items will be discussed, Select Rows based on value in column; Select Rows based on any of the multiple values in column; Select Rows based on any of the multiple conditions on column; First let’s create a

To achieve this you have to modify your enum

enum values {
    none = 0,
    go =  ( 1 << 0 ), // 1
    stand = ( 1 << 1 ), // 2
    jump =  ( 1 << 2 ), // 4,
    run = ( 1 << 3 ), // 8
    go_jump = go | jump
}

Then in your method:

values GetValues(bool go, bool stand, bool jump, bool run)
{
    values result = values.none;
    if( go )
        result |= values.go;

    if( stand )
        result |= values.stand;

    // and so on...

    return result;
}

Avoid duplicate conditional checks in multiple boolean conditions , Based on the return value of this method and several other conditions a test is required to be performed. boolean isIdChanged=true; boolean isAjaxRequest= true� A Boolean value, which is sometimes called a logical value, is one of several types of data used in spreadsheets in applications like Excel and Google Sheets.Named after the 19th-century mathematician George Boole, Boolean values are part of a branch of algebra known as Boolean algebra or Boolean logic.

You can use a flags enum to allow combining the values into a single enum value.

Indicates that an enumeration can be treated as a bit field; that is, a set of flags.

Here's an example: (Dot Net Fiddle)

using System;

[Flags]
public enum Value
{
    None = 0,
    Go = 1,
    Stand = 2,
    Jump = 4,
    Run = 8
}

public class Program
{
    public static void Main()
    {   
        var value1 = CreateValue(true, false, true, false);
        var value2 = CreateValue(false, true, true, true);

        Console.WriteLine(value1); //Go, Jump
        Console.WriteLine(value2); //Stand, Jump, Run
    }

    public static Value CreateValue(bool go, bool stand, bool jump, bool run)
    {
        var value = Value.None;

        if(go)
            value |= Value.Go;

        if(stand)
            value |= Value.Stand;

        if(jump)
            value |= Value.Jump;

        if(run)
            value |= Value.Run;

        return value;
    }
}

Complex if statements with C#'s logical operators � Kodify, Python's if statements test multiple conditions with and and or. Those logical operators combine several conditions into a single True or False value. The or operator returns True when its left, right, or both conditions are True . The only time Each gets a True or False based on what the customer ordered. Sometimes, you may need to extract matched values from multiple columns based on the criteria. Here is the solution for you. Vlookup to return multiple values in one cell Normally, when applying the VLOOKUP function, if there are multiple values that match the criteria, you can only get the result of the first one.

You can use bit-wise math. For instance, here is your class:

class MyClass
{
    [Flags]
    public enum Values
    {
        Go = 1,
        Stand = 2,
        Jump = 4,
        Run = 8
    }

    public Values Select(bool go, bool stand, bool jump, bool run)
    {
        Values ret = 0;
        if (go) ret = (ret | Values.Go);
        if (stand) ret = (ret | Values.Stand);
        if (jump) ret = (ret | Values.Jump);
        if (run) ret = (ret | Values.Run);
        return ret;
    }
}

And here is how you'd use it:

var mc = new MyClass();
MyClass.Values y = mc.Select(true, false, true, true);
bool isStand = (y & MyClass.Values.Stand) == MyClass.Values.Stand; // false
bool isJump = (y & MyClass.Values.Jump) == MyClass.Values.Jump; // true

Python if statements with multiple conditions (and + or) � Kodify, Evaluates multiple conditions and returns a value that corresponds to the first true This can be a boolean, a number, an array, or a reference to any of those. The lookup values are found in row 5,6,9 and 10 but only the corresponding values from row 5 and 6 are returned, that is how the VLOOKUP function is supposed to work. If you need to extract multiple values based on a condition read this: 5 easy ways to VLOOKUP and return multiple values. Array formula in cell D14:D15:

this pseudo code should work if you arrange your values that are not shown "correctly" if they are not and start with 0, instead of 1. .

int select value(bool go, bool stand, bool jump, bool run)
 {
  return (int) runx2^3 + (int)jumpx2^2 + (int) standx2^1 + (int) go;
 }

IFS function - Docs Editors Help, Return multiple matching values based on one or multiple criteria with array formulas. For example, I want to extract all names whose age is 28 and come from United States, please apply the following formula: 1. Copy or enter the below formula into a blank cell where you want to locate the result:

Excel Formula Training. Formulas are the key to getting things done in Excel. In this accelerated training, you'll learn how to use formulas to manipulate text, work with dates and times, lookup values with VLOOKUP and INDEX & MATCH, count and sum with criteria, dynamically rank values, and create dynamic ranges.

The article shows a few different ways to get the max value in Excel based on one or several conditions that you specify. In our previous tutorial, we looked at the common uses of the MAX function which is designed to return the largest number in a dataset.

Multiple IF functions can be nested together to allow for multiple criteria. The Excel IF function statement allows you to make a logical comparison between a value and what you expect by testing for a condition and returning a result if True or False.

Comments
  • What Language should this be? Not C# for sure...
  • Can you please detail the question? what value should you return ? The funciton you wrote is a Void.
  • how enum is related to boolean values? Can you please show how do you use booleans?
  • I need to return appropriate value what is appropriate value? also your method signature is wrong.
  • Have you considered that using a [Flags] based enum might simpify things?
  • You have to admit it was easier in 1978, but have an upvote all the same.
  • You should use the Flags attribute. msdn.microsoft.com/en-us/library/…
  • If you're working with enums as bit fields in .net then it's incorrect without the flags attribute. If you check my answer you'll see that you don't have to define every combination of enum values manually, and yet the output still shows separate fields Console.WriteLine(value2); //Stand, Jump, Run. The fact that your enum has to declare every possible combination makes it more prone to errors (objectively) and less readable (subjectively).
  • @AndyJ I have to admit my solution is "Anty-Idot" solution because you have to get some knowledge about bitwise operators and you have to specify every possible combination which you cannot using FlagsAttribute. For some examples it's okay to use FlagsAttribute but i doubt that you can stand | jump...
  • I love the use of the shift operators to demonstrate what is really going on rather than just using 1, 2, 4, 8.
  • you missed the go_stand_jump and other flags
  • @Toshi -- No. Re-read the answer. And read the linked flags MSDN.
  • @Toshi The Flags attribute handles that. If you run the code (follow the DotNetFiddle link) then you'll see the console outputs the contents of the values. value2 is "Stand, Jump, Run".
  • I've got to ask why 2, 4, 8 and 64 rather than the more traditional 1,2,4,8?