• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 489
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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