I tried to create a class which basically acts like union.
I have a string and an double member in my class but only
one is "active" and I want the use the "implicit" keyword
to get the "active" object. Here's the code:
public class StringDoubleVal
{
string strval;
double dblval;
bool string_active;
public void SetVal(string val)
{
strval = val; string_active = true;
}
public void SetVal(double dblval)
{
dblval = val; string_active = false;
}
public static implicit operator object(StringDoubleVal
val)
{
return val.string_active ? val.strval : val.dblval;
}
}
The problem is that since "object" is a base class of
StringDoubleVal, the compiler won't let me define an
implicit operator for the "object" type since the compiler
can perform such an operation by its self.
The problem is that the semantics in my program of the
value of a StringDoubleVal object is not the object it
contains but the currently active object contained by the
StringDoubleVal object. Why is this just not a warning
instead of an error?