• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

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.
0
gvector1
Asked:
gvector1
1 Solution
 
gregoryyoungCommented:
       const int f = 1;
        const int f1 = f + 1;
        const int f2 = f1 + 1;

works just fine in C# ... Not sure why you are defining them as objects?

So ...

//// Base for MCI Control error codes
const int MCIERR_CONTROL_BASE = (MCIERR_CUSTOM_DRIVER_BASE + 100);

const int MCIERR_SETDRIVERDATA = (MCIERR_CONTROL_BASE + 0);
const int MCIERR_GETDRIVERDATA = (MCIERR_CONTROL_BASE + 1);
const int MCIERR_PSPCTRL_NODRIVER = (MCIERR_CONTROL_BASE + 2);


works

Cheers,

Greg Young
0
 
sabeeshCommented:
You can define like this


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);
0
 
gvector1Author Commented:
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.
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now