Reputation: 115
How do I create a replica of the bool syntax and without using true
and false
I would use Enabled
and Disabled
? I would like it to be used like so...
sBool E = Enabled;
sBool f = Disabled;
if (e || f == Disabled)
{
Do something...
}
Upvotes: 0
Views: 2728
Reputation: 109852
I'd just use a bool, but if you really want to encapsulate the logic in a separate class with the most readable syntax possible you could do something like this:
public sealed class Status: IEquatable<Status>
{
public Status(bool isEnabled)
{
_isEnabled = isEnabled;
}
public bool IsEnabled
{
get { return _isEnabled; }
}
public bool IsDisabled
{
get { return !_isEnabled; }
}
public bool Equals(Status other)
{
return other != null && this.IsEnabled == other.IsEnabled;
}
public static implicit operator bool(Status status)
{
return status.IsEnabled;
}
public static Status Enabled
{
get { return _enabled; }
}
public static Status Disabled
{
get { return _disabled; }
}
private readonly bool _isEnabled;
private static readonly Status _enabled = new Status(true);
private static readonly Status _disabled = new Status(false);
}
Then for your sample code, do this kind of thing:
Status e = Status.Enabled;
Status f = Status.Disabled;
if (e || f.IsDisabled)
{
// ...
}
// Alternatively:
if ( e.Equals(Status.Enabled) || f.Equals(Status.Disabled) )
{
// ...
}
Upvotes: 0
Reputation: 8993
You could use an enum, like so:
enum Status
{
Enabled,
Disabled
}
var e = Status.Enabled;
if (e == Status.Disabled)
{
// Do something
}
I'm not sure what your use case is but in terms of code readability/maintainability I'd say using an enum is the most simple solution and easiest for other developers to understand.
Upvotes: 1
Reputation: 186843
If sBool plays a significant role in your project, you may opt to implement a corrsponding full scale struct (not enum):
public struct sBool {
private Boolean m_Value;
public static readonly sBool Enabled = new sBool(true);
public static readonly sBool Disabled = new sBool(false);
...
private sBool(Boolean value) {
m_Value = value;
}
...
public override bool Equals(object obj) {
if (!(obj is sBool))
return false;
sBool other = (sBool) obj;
return other.m_Value == m_Value;
}
public override int GetHashCode() {
return m_Value ? 1 : 0;
}
...
public Boolean ToBoolean() {
return m_Value;
}
public static implicit operator Boolean(sBool value) {
return value.m_Value;
}
}
....
sBool e = sBool.Enabled;
sBool f = sBool.Disabled;
if (e || f == sBool.Disabled) {
...
}
Upvotes: 1
Reputation: 3476
It's a little cheat, but you can declare on two variables like this:
Boolean Enabled = true;
Boolean Disabled = false;
Now you can write in your code:
Boolean sBool = Enabled;
The disadvantage: Enabled and Disabled haven't special color..
Upvotes: 2
Reputation: 709
There isn't really a good way to do this. You can exploit the fact that enums are really just ints with fancy names and use bitwise operators to emulate the logical operators.
So:
enum Status { Disabled = 0, Enabled = 1 }
Status a = Status.Disabled;
Status b = Status.Enabled;
if( (a | b) == Status.Enabled){
//Code
}
Upvotes: 0
Reputation: 2579
Just make an enum as such
public enum sBool
{
Enabled,
Disabled
}
Then you declare your code will look like this:
sBool E = sBool.Enabled;
sBool f = sBool.Disabled;
if (E == sBool.Disabled || F == sBool.Disabled)
{
//Do something...
}
EDIT: fixed the if
syntax
Upvotes: 2