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?
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?