1

Please check following code tell me what the difference between Convert.ToInt32() and int(), Why Convert.ToInt32 is showing error?

Here UserType is a enum

// Showing error constant initializer must be compile time constant
const int case1 = Convert.ToInt32(UserType.Admin); 

const int case2 = int(UserType.Admin);
Oded
  • 489,969
  • 99
  • 883
  • 1,009
Venom
  • 1,076
  • 1
  • 11
  • 23
  • did you mean int.Parse(UserType.Admin)? – Ebad Masood Jun 28 '12 at 12:23
  • Difference is in the result of operation. (int)(some value) performs casting and may end with unexpected result. While, Convert.ToInt32 is dedicated to convert other compatible values to integer of 32 bit. - http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx – Yusubov Jun 28 '12 at 12:28
  • @ElYusubov: that's not the problem. – user7116 Jun 28 '12 at 12:30

4 Answers4

3

You're assigning a return value from a method to a constant, which is not allowed. In .Net, the value of a constant is required to be known at compile time. This is not possible if it is being assigned a value from a method call at runtime.

For the general case, you could alter this slightly and have code which is logically equivalent:

static readonly int Case1 = Convert.ToInt32(UserType.Admin);

However, a simple cast to an int is allowable with enumerations, so your second example would probably be Ok (albeit not idiomatic).

user7116
  • 63,008
  • 17
  • 141
  • 172
  • As an alternative, you can instead declare it as `readonly` which will let you use the `Convert.ToInt32` method (if you really need it). Slightly different, but it may be acceptable depending on your design. – Chris Sinclair Jun 28 '12 at 12:24
  • I think a `static readonly` would be more appropriate in this case. `const` should be used with values which are unlikely to ever change (i.e. `Math.PI`). If you have a constant that may change use a `readonly` (see this [answer](http://stackoverflow.com/a/56024/63011)). – Paolo Moretti Jun 28 '12 at 12:39
  • @PaoloMoretti: agreed, I just have no idea what the OP is attempting to do. – user7116 Jun 28 '12 at 13:51
1

Convert.ToInt32 is a method that needs to be called.
It's not a compile-time constant, it needs to be called at runtime.

(int)UserType.Admin is constant at compile time, because UserType.Admin is a constant and (int) is just a cast to int. The compiler can evaluate this without problems. No methods to be called.

I would rather suggest not converting enums to integers though, as the original meaning associated with the enum type gets lost.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
1

You are getting this error because:

A constant expression is an expression that can be fully evaluated at compile time.

const - MSDN

And your Convert.ToInt32() is not a compile time constant expression, its a method.

For 2nd part:

const int case2 = int(UserType.Admin);

I believe you meant

const int case2 = (int)UserType.Admin;
Habib
  • 219,104
  • 29
  • 407
  • 436
-1

(int) will only convert types that can be represented as an integer (ie double, long, float, etc) although some data loss may occur.

Int32.Parse will only convert strings to integers. You can't cast (ie (int)mystring) strings to integers.

JohnnBlade
  • 4,261
  • 1
  • 21
  • 22