From times to times I stumble over the following situation:
I have got a class with a property that's only used if another property has a particular value, for instance:
public enum enum_ConnectionType
{
Local,
Server
}
public class Session
{
private enum_ConnectionType _connectionType;
private string _serverName; // It only makes sense if _connectionType == enum_ConnectionType.Server
public Session(enum_ConnectionType connectionType, string serverName)
{
_connectionType = connectionType;
_serverName = serverName;
}
}
There's something that does not feel right to me. What is your common approach on this kind of situations?
EDIT
I think both Jeff and Leonid answers are valid depending on the situation.
_serverName = serverName;
to be_serverName = _connectionType == enum_ConnectionType.Server ? serverName : null;
. So an instance ofSession
is internally consistent. \$\endgroup\$