Anton Shepelev
2016-05-17 20:23:51 UTC
Hello, all
I have to maintain a program which can be compiled
to use different (and not fully compatible) versions
of a 3rd-party API. I am using conditional compila-
tion and a symbol that holds the version of the API,
e.g.:
int msSqlVerInt = GetMsSqlVersion();
Api.SqlVersion msSqlVer;
switch ( msSqlVerInt )
{ case 10: msSqlVer = Api.SqlVersion.MsSql2008; break;
#if api_v8
case 11: msSqlVer = Api.SqlVersion.MsSql2012; break;
#endif
#if api_v9
case 11: msSqlVer = Api.SqlVersion.MsSql2012; break;
case 12: msSqlVer = Api.SqlVersion.MsSql2014; break;
#endif
default throw new Exception
( "MsSql version " + msSqlVerInt.ToString() " not supported." );
}
The code above takes care of the fact that newer
versions of the API support more versions of MS SQL
Server. The enumeration element Api.SqlVersion.
MsSql2014, for example, is not defined in versions
earler than the 9th. As more API versions are re-
leased, the code will become increasingly redundant,
each #ifdef clause repeating the contents of the
previous one and adding but a single new element.
Is there a way to express it more elegantly, e.g.:
case 10: msSqlVer = Api.SqlVersion.MsSql2008; break;
#if apiver >= 8
case 11: msSqlVer = Api.SqlVersion.MsSql2012; break;
#endif
#if apiver >= 9
case 12: msSqlVer = Api.SqlVersion.MsSql2014; break;
#endif
And since it turned out that I must handle minor
versions as well (e.g 8.1, 9.2) my current approach
is barely tolarable.
I have to maintain a program which can be compiled
to use different (and not fully compatible) versions
of a 3rd-party API. I am using conditional compila-
tion and a symbol that holds the version of the API,
e.g.:
int msSqlVerInt = GetMsSqlVersion();
Api.SqlVersion msSqlVer;
switch ( msSqlVerInt )
{ case 10: msSqlVer = Api.SqlVersion.MsSql2008; break;
#if api_v8
case 11: msSqlVer = Api.SqlVersion.MsSql2012; break;
#endif
#if api_v9
case 11: msSqlVer = Api.SqlVersion.MsSql2012; break;
case 12: msSqlVer = Api.SqlVersion.MsSql2014; break;
#endif
default throw new Exception
( "MsSql version " + msSqlVerInt.ToString() " not supported." );
}
The code above takes care of the fact that newer
versions of the API support more versions of MS SQL
Server. The enumeration element Api.SqlVersion.
MsSql2014, for example, is not defined in versions
earler than the 9th. As more API versions are re-
leased, the code will become increasingly redundant,
each #ifdef clause repeating the contents of the
previous one and adding but a single new element.
Is there a way to express it more elegantly, e.g.:
case 10: msSqlVer = Api.SqlVersion.MsSql2008; break;
#if apiver >= 8
case 11: msSqlVer = Api.SqlVersion.MsSql2012; break;
#endif
#if apiver >= 9
case 12: msSqlVer = Api.SqlVersion.MsSql2014; break;
#endif
And since it turned out that I must handle minor
versions as well (e.g 8.1, 9.2) my current approach
is barely tolarable.
--
() ascii ribbon campaign - against html e-mail
/\ http://preview.tinyurl.com/qcy6mjc [archived]
() ascii ribbon campaign - against html e-mail
/\ http://preview.tinyurl.com/qcy6mjc [archived]