2

For an assignment I have to write a Tribool class in C# using a struct. There are only three possible tribools, True, False, and Unknown, and I have these declared as static readonly. Like this:

public static readonly Tribool True, False, Unknown;

I need my default constructor to provide a False Tribool, but I'm not sure how to go about this. I've tried Tribool() { this = False; } and Tribool() { False; } but I keep getting a "Structs cannot contain explicit parameterless constructors" error.

The assignment specified that the default constructor for Tribool should provide a False Tribool. Otherwise, a user should not be able to create any other Tribools. I don't really know what to do at this point. Any advice would be greatly appreciated. Thanks.

1
  • For myself i would always make the states in this order Unknown, False, True. So a not initialized Tribool has as default the state unknown. Commented Feb 2, 2010 at 7:27

3 Answers 3

10

Just to add a bit to Jason's answer: design your struct carefully so that the default value is meaningful. As an example, consider the Nullable<T> struct. The desired behaviour is that when you say

Nullable<int> x = new Nullable<int>(); // default constructor

that the resulting value is logically null. How do we do that? The struct is defined something like this:

struct Nullable<T> 
{
    private readonly T value;
    private readonly bool hasValue;
    public Nullable(T value)
    { 
        this.value = value;
        this.hasValue = true;
    }
    ...

So when the default constructor runs, hasValue is automatically set to false and value is set to the default value of T. A nullable with hasValue set to false is treated as null, which is the desired behaviour. That's why the bool is hasValue and not isNull.

Sign up to request clarification or add additional context in comments.

5 Comments

When I run the Nullable<int> i = new Nullable<int>(); Console.WriteLine( i.Value); code, I got exception on the second line. You mentioned in the answer that value is set to default value of T. Based on that i.Value should be 0. But it threw an exception {"Nullable object must have a value."}. Why? Also,i was null. Why? P.S. I'm aware of Nullable type. Just curious to know how this behaviour was achieved?
The field is zero. The property access or throws.
I'm not sure what do you mean by property access or throws
@gmailuser - he meant "The property accessor throws", e.g. public T Value {get { if (!hasValue) throw... } }
WOW.. SO has new feature Autocorrect :) But, Anyway Thanks. Your time is always appreciated. For me, sometimes it is hard to understand high profile programmers' explanation.
6

As the error is telling you, you absolutely can not have a parameterless instance constructor for a struct. See §11.3.8 of the C# 3.0 language specification:

Unlike a class, a struct is not permitted to declare a parameterless instance constructor.

The language provides one for you known as the default constructor. This constructor returns the value of the struct where are fields have been set to their default value.

Now, what you could do is have the default value of the struct represent the False value. I'll leave the details to you.

10 Comments

Thanks much for the offer. Just wondering, if I didn't try to overwrite the default constructor, what would the default value be? I think I might not be completely understanding the assignment. Someone mentioned I should have a system for representing True/False/Unknown inside the Tribool class itself instead of using the public static readonly constants as values, and I shouldn't need the constructor since it's not doing anything meaningful...I'm not too sure what that means, and how I would define True/False/Unknown tribools. If you could explain it to me I'd really appreciate it. Thanks!
Yes, the key is find to a way to represent the TriBool. This is completely up to you and the "right way" is very much dependent on what else you have to do with TriBool. But some possibilities: 1. use int internally to represent the TriBool with "false" corresponding to 0, "true" corresponding to 1 and "unknown" corresponding to -1. Or, use two bool internally, one called, say value and the other called, say unknown with value representing "true" or "false" depending on if it is true or false but only if unknown is false, otherwise the instance represents "unknown."
Lastly, you should think about those representations, or whatever you come up with, in the context of the penultimate sentence of my answer.
Thanks so much for the help! I think I have an idea what to do now. One last question--If I were to define, say, the static Tribool True using int, I know to set its value to 1. But where would I declare that? I've tried public static readonly Tribool True {value = 1;} but it seems that I'm doing it wrong again. Could you give me a hint what I'm doing wrong? Thanks again.
What you're doing wrong is that you're trying to use object initializer syntax to initialize value but you probably haven't declared value as a property. That's fine. So you need a different approach. A couple options come to mind. 1. You could define a private constructor TriBool(int value) that sets the value of this.value and then say public static readonly TriBool True = new TriBool(1); 2. You could define a static constructor along the lines of static TriBool() { True.value = 1; } 3. Go ahead and define value as a property. Frankly, I favor option 1.
|
0

Little late to the game but, is there any reason your not using an enum?

after all if you just need a Trinary value then the struct is massive overkill

public enum Tribool{
    Unknown =0,
    True = -1,
    False = 1
}

1 Comment

You cannot overload operators on an enum. If you used a struct, then you could overload operator ~, operator &, operator |, and operator ^ with the appropriate behavior for three-way logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.