1

This may sound like an odd question, and I have a feeling that the short answer is 'No'.

However, is there any way a variable could take more than one value, based on Boolean operators? For example:

//Current implementation
string Variable1 = "A";
string Variable2 = "B";
string Variable3 = "C";

//Sought after implementation
string Variable = "":
Variable = "A" || Variable = "B" || Variable = "C";

This doesn't look like it could be feasible, especially since Boolean operators can't be applied to string types, because, well... They're not Boolean.

  • 5
    I don't understand what conditions determine which value is assigned to the variable. – Jonathon Chase Mar 15 '18 at 21:24
  • 1
    Maybe an Enum? Kind of hard to tell from the XY format – Ňɏssa Pøngjǣrdenlarp Mar 15 '18 at 21:24
  • 1
    Instead of string, you can have an int and set bit flags. – GregorMohorko Mar 15 '18 at 21:24
  • Do you want the string to maintain 3 possible states all the time? or do you mean to set it to A, B, or C and from that point forward it is that value? For the first one, you are better making a single object with the 3 values and then determining which of the 3 is appropriate later, as suggested Enums do this quite well. For the second case, ternary operators do quite well at setting a value based on some condition(s) in a single line. see https://stackoverflow.com/questions/6208067/operators-instead-of-ifelse – user7396598 Mar 15 '18 at 22:58

2 Answers2

5

However, is there any way a variable could take more than one value, based on Boolean operators?

Sure! Let's implement it. We'll use the ImmutableHashSet<T> type from System.Collections.Immutable.

struct MySet<T>
{
    public readonly static MySet<T> Empty = default(MySet<T>);
    private ImmutableHashSet<T> items;
    private MySet(ImmutableHashSet<T> items) => this.items = items;
    public ImmutableHashSet<T> Items => this.items ?? ImmutableHashSet<T>.Empty;
    public MySet<T> Add(T item) => new MySet<T>(this.Items.Add(item));
    public static MySet<T> operator |(T item, MySet<T> items) => items.Add(item);
    public static MySet<T> operator |(MySet<T> items, T item) => items.Add(item);
    public static MySet<T> operator |(MySet<T> x, MySet<T> y) => new MySet<T>(x.Items.Union(y.Items));
    public static MySet<T> operator &(MySet<T> items, T item) => new MySet<T>(items.Items.Contains(item) ? ImmutableHashSet<T>.Empty.Add(item) : ImmutableHashSet<T>.Empty);
    public static MySet<T> operator &(T item, MySet<T> items) => new MySet<T>(items.Items.Contains(item) ? ImmutableHashSet<T>.Empty.Add(item) : ImmutableHashSet<T>.Empty);
    public static MySet<T> operator &(MySet<T> x, MySet<T> y) => new MySet<T>(x.Items.Intersect(y.Items));
}

Now we can create a variable that contains multiple values of any type, and obeys the laws of | and &:

    var items1 = MySet<String>.Empty | "Hello" | "Goodbye" | "Whatever";
    var items2 = MySet<String>.Empty | "Goodbye" | "Hello" | "Blah";
    var items3 = items1 & items2;
    var items4 = items1 | items2;
    Console.WriteLine(String.Join(" ", items3.Items)); // "Hello Goodbye"
    Console.WriteLine(String.Join(" ", items4.Items)); // "Hello Goodbye Whatever Blah"
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
4

Define an enum with the [Flags] attribute.

Reference: What does the [Flags] Enum Attribute mean in C#?

[Flags]
public enum PossibleValues { A = 1, B = 2, C = 4 }

var foo = PossibleValues.A | PossibleValues.B | PossibleValues.C;
Robert Levy
  • 28,747
  • 6
  • 62
  • 94
  • 2
    The `[Flags]` isn't really required... Manual numbering 1,2,4,8, is essential. – H H Mar 15 '18 at 21:38
  • @HenkHolterman added numbers :) Attribute is helpful for tooling to know whats up. – Robert Levy Mar 15 '18 at 21:51
  • 1
    @RobertLevy here is a useful trick, label your enums with `public enum PossibleValues { A = 1<<0, B = 1<<1, C = 1<<2, D = 1<<3, ... }` it gemerates 1, 2, 4, 8,.... but you don't need to remember your higher powers of 2. – Scott Chamberlain Mar 15 '18 at 22:01