11

With this code:

public partial class Form1 : Form
{
    private static readonly int TABCONTROL_BASICINFO = 0;
    private static readonly int TABCONTROL_CONFIDENTIALINFO = 1;
    private static readonly int TABCONTROL_ROLESANDSECURITY = 2;
    private static readonly int TABCONTROL_INACTIVEINFO = 3;
. . .
int ActiveTabPage = tabControlWorker.SelectedIndex;
switch (ActiveTabPage) {
    case TABCONTROL_BASICINFO:
        if (currentNode == "NodeBuckingham") {
        } else if (currentNode == "NodeNamath") {
        } else if (currentNode == "NodeParsons") {
        } else {
        }
    break;

...I have to replace "TABCONTROL_BASICINFO" with "0", or I get, "A constant value is expected"

Heavens to Murgatroyd! Can't it look up and see that TABCONTROL_BASICINFO is 0?

1
  • Frustrating huh! Gotta use an actual const rather than the readonly statics :( Commented Mar 22, 2012 at 23:08

4 Answers 4

21

If you want to make it a constant expression as far as the compiler is concerned, declare it as a const:

// Note that static readonly is implied here
private const int TABCONTROL_BASICINFO = 0;

Or follow .NET naming conventions...

private const int TabControlBasicInfo = 0;

Or use an enum, given that you've basically got a fixed set of values:

private enum TabControlType
{
    // Could ditch the explicit values here if you want
    BasicInfo = 0,
    ConfidentialInfo = 1,
    ...
}

By the way, you can also switch on strings in C#, so this:

 if (currentNode == "NodeBuckingham") {
 } else if (currentNode == "NodeNamath") {
 } else if (currentNode == "NodeParsons") {
 } else {
 }

Can become:

 switch (currentNode) {
     case "NodeBuckingham":
         ...
         break;
     case "NodeNamath":
         ...
         break;
     case "NodeParsons":
         ...
         break;
     default:
         ...
         break;
 }
Sign up to request clarification or add additional context in comments.

Comments

14

A readonly variable is not a constant. The value is not known at compile time, but rather can be initialized either in the declaration (as you have done) or in the class constructor (in this case, the static constructor for your class).

For more see

http://msdn.microsoft.com/en-us/library/acdd6hb7(v=vs.71).aspx

You can change it to:

private const int TABCONTROL_BASICINFO = 0; 

Unless you need to compute something to initialize the variable, declare it as const. It will be slightly more efficient.

1 Comment

This one is the real answer.
8

That's not a constant; it's a read-only variable. If you want it to be a constant then mark it as a constant.

private const int TabControlBasicInfo = 0;

Or even better:

private enum TabControl
{
    BasicInfo,
    ...
}
...
switch(whatever)
{
    case TabControl.BasicInfo:

3 Comments

Why is an enum better in this case? Just because it groups related values?
@EricJ. Looking at how the readonly names are structured, and how he he is trying to use them, enum would appear to be the correct structure.
when I do that, I get, "Cannot implicitly convert type 'UserControlsOnTabPagePOCApp.Form1.TabControl' to 'int'. An explicit conversion exists (are you missing a cast?)"
5

Static readonly isn't a constant variable. It can be initialized. Whereas "const" is constant.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.