Const Initializer within class

  • Thread starter Thread starter WLT
  • Start date Start date
W

WLT

I'm keeping both a variable, and its default value in a class:

class X {
public const string DefaultString = "Default Value";
...
public string VarString = DefaultString;
}

I'm probably not up on subtleties of how 'const' is treated in C#,
cause the code compiles fine, but generates a runtime exception.

System.FieldAccessException: (from docs)
A FieldAccessException exception is thrown when there is an invalid
attempt to access a private or protected field inside a class

If the access level of a field in a class library has changed,
recompile any assemblies that reference that library.
This exception is usually thrown when the access level (Public,
Private, etc) of a field in a class library is changed, and one or
more assemblies referencing the library have not been recompiled.

Of course the code was recompiled...clean build.

Changing 'const' to 'static' seems to fix it, but I'd just like to
understand:

Why did the code compile and run for quite a while, then just start to
show this problem?

Also why would this be a runtime error rather than compile-time?
 
Well, I don't have an explanation, but an observation :
Maybe if you initialise your string in your ctor it would be OK ?

Adam.
=====
 
Hi,

It does compile & run fine here ( 1.1 )

in what line you are getting the error?
 
WLT said:
I'm keeping both a variable, and its default value in a class:

class X {
public const string DefaultString = "Default Value";
...
public string VarString = DefaultString;
}

I'm probably not up on subtleties of how 'const' is treated in C#,
cause the code compiles fine, but generates a runtime exception.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
Why did the code compile and run for quite a while, then just start to
show this problem?

Also why would this be a runtime error rather than compile-time?

It sounds like you may have made the constant non-public and tried to
use it from another assembly...
 
Psychic code debugger, eh? There is one module that iterates over
members of the class using reflection. I believe it only retrieves
variable names (have to recheck that). I'm still not sure why the
code seemed to run without problems, then started generating
exceptions.

The error message really threw me, but I should know to expect that.
Impressive that you guessed this Since you seem to have some insight,
can you explain the cause?
 
WLT said:
Psychic code debugger, eh? There is one module that iterates over
members of the class using reflection. I believe it only retrieves
variable names (have to recheck that). I'm still not sure why the
code seemed to run without problems, then started generating
exceptions.

The error message really threw me, but I should know to expect that.
Impressive that you guessed this Since you seem to have some insight,
can you explain the cause?

I'm not sure that's actually the issue. For instance:

using System;
using System.Reflection;

public class Test
{
public const int Foo = 10;

static void Main()
{
FieldInfo fi = typeof(Test).GetField ("Foo");
Console.WriteLine (fi.GetValue(null));
}
}

works fine.

What does the exception stack trace point you towards?
 
Back
Top