gvector1
asked on
Const declarations
I am making some code conversions from VB6 and VB.NET to C# and have run into a little problem. In VB6 and VB.NET, the code had const declarations like follows:
'// Base for MCI Control error codes
Const MCIERR_CONTROL_BASE As Integer = (MCIERR_CUSTOM_DRIVER_BASE + 100)
Const MCIERR_SETDRIVERDATA As Object = (MCIERR_CONTROL_BASE + 0)
Const MCIERR_GETDRIVERDATA As Object = (MCIERR_CONTROL_BASE + 1)
Const MCIERR_PSPCTRL_NODRIVER As Object = (MCIERR_CONTROL_BASE + 2)
When I make the same declarations in C#:
//// Base for MCI Control error codes
const int MCIERR_CONTROL_BASE = (MCIERR_CUSTOM_DRIVER_BASE + 100);
const object MCIERR_SETDRIVERDATA = (MCIERR_CONTROL_BASE + 0);
const object MCIERR_GETDRIVERDATA = (MCIERR_CONTROL_BASE + 1);
const object MCIERR_PSPCTRL_NODRIVER = (MCIERR_CONTROL_BASE + 2);
When I try to compile, I get an error stating "The expression being assigned to must be a constant". How I make the same declarations? I know I can use a string, but I want to make sure that there is no chance of the values getting changed.
'// Base for MCI Control error codes
Const MCIERR_CONTROL_BASE As Integer = (MCIERR_CUSTOM_DRIVER_BASE
Const MCIERR_SETDRIVERDATA As Object = (MCIERR_CONTROL_BASE + 0)
Const MCIERR_GETDRIVERDATA As Object = (MCIERR_CONTROL_BASE + 1)
Const MCIERR_PSPCTRL_NODRIVER As Object = (MCIERR_CONTROL_BASE + 2)
When I make the same declarations in C#:
//// Base for MCI Control error codes
const int MCIERR_CONTROL_BASE = (MCIERR_CUSTOM_DRIVER_BASE
const object MCIERR_SETDRIVERDATA = (MCIERR_CONTROL_BASE + 0);
const object MCIERR_GETDRIVERDATA = (MCIERR_CONTROL_BASE + 1);
const object MCIERR_PSPCTRL_NODRIVER = (MCIERR_CONTROL_BASE + 2);
When I try to compile, I get an error stating "The expression being assigned to must be a constant". How I make the same declarations? I know I can use a string, but I want to make sure that there is no chance of the values getting changed.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sabeesh,
That is what I have defined and am getting the error. As GregoryYoung posted, if I change the object declaration to int, it works fine.
That is what I have defined and am getting the error. As GregoryYoung posted, if I change the object declaration to int, it works fine.
const int MCIERR_CONTROL_BASE = (MCIERR_CUSTOM_DRIVER_BASE
const object MCIERR_SETDRIVERDATA = (MCIERR_CONTROL_BASE + 0);
const object MCIERR_GETDRIVERDATA = (MCIERR_CONTROL_BASE + 1);
const object MCIERR_PSPCTRL_NODRIVER = (MCIERR_CONTROL_BASE + 2);