Const Initializer within class

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

Adam Benson

Well, I don't have an explanation, but an observation :
Maybe if you initialise your string in your ctor it would be OK ?

Adam.
=====
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

It does compile & run fine here ( 1.1 )

in what line you are getting the error?
 
J

Jon Skeet [C# MVP]

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...
 
W

WLT

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

Jon Skeet [C# MVP]

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top