By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,977 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,977 IT Pros & Developers. It's quick & easy.

Const Initializer within class

P: n/a
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?


Mar 28 '06 #1
Share this Question
Share on Google+
6 Replies


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

Adam.
=====
Mar 28 '06 #2

P: n/a
Hi,

It does compile & run fine here ( 1.1 )

in what line you are getting the error?

"WLT" <WL*@nospam.com> wrote in message
news:og********************************@4ax.com...
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?


Mar 28 '06 #3

P: n/a
WLT <WL*@nospam.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 29 '06 #4

P: n/a
You did not use reflection for accessing the variable, did you?
"WLT" <WL*@nospam.com> schrieb im Newsbeitrag
news:og********************************@4ax.com...
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?


Mar 29 '06 #5

P: n/a
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?
On Wed, 29 Mar 2006 11:46:15 +0200, "cody" <de********@gmx.de> wrote:
You did not use reflection for accessing the variable, did you?
"WLT" <WL*@nospam.com> schrieb im Newsbeitrag
news:og********************************@4ax.com.. .
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?



Mar 30 '06 #6

P: n/a
WLT <WL*@nospam.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 30 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.