friggityfraggity

I have run into this multiple times in the life of the app I'm working on. The scenario is simple. I've got a public field in a class that I create an instance of. I cannot access the variable via an instance, but I can if I access it via the base type or if I make it private and create an accessor.

Initially, before adding the accessor, both the field and class are public and not static.

The only pattern I can follow is that it is consistent across classes. Either all the public fields are open in a class or none.

Any ideas on things to look at

I can post some code if it comes down to it.

Thanks!
Greg



Re: Visual C# General Public field not accessible, forced to create an accessor...why?

Mark Dawson

Hi,

can you post a short code example which exhibits the problem.

Thanks

Mark.






Re: Visual C# General Public field not accessible, forced to create an accessor...why?

friggityfraggity

With this code, I can only access the field maxX via the base class i.e. Level.maxX.

namespace Game
{
public class Level
{
public const int maxX = 10;
etc...

However, if I add this accesssor, I can now get to it via an instance, i.e. InstanceofLevel.maxX

namespace Game
{
public class Level
{
private const int _maxX = 10;
public int maxX
{
get
{
return _maxX;
}
}

etc...

Any attempt to access the first snippet via InstanceofLevel.maxX results in

Static member 'Level.maxX' cannot be accessed with an instance reference; qualify it with a type name instead

In trying to clean up the code to post, I took "const" out of the declaration, but then thought, no I'd better leave that in just in case it's significant. Well, I tried the code without it and it worked, so it has something to do with "const" but I don't know why yet. The error does say static, so I'm guessing const is a special case of static.





Re: Visual C# General Public field not accessible, forced to create an accessor...why?

Mark Dawson

Hi,

yes const fields are static variables, so you must access them at the class level. My personal opinion is that even if you are using const or static class fields you should access them through properties and not directly make them public.

Mark.






Re: Visual C# General Public field not accessible, forced to create an accessor...why?

friggityfraggity

It's funny how the mind plays tricks on you. Now that I know what to look for, I can trace every one those cases of this error back to references to constants. I could've sworn I was getting the error with methods too. Oh, well, thanks again!

Before today, I would've thought that you should only need an accessor if you need to do bounds-checking, check or change dependencies, etc. I use a bool as an example so that bounds-checking isn't a factor. I just couldn't see a benefit in wrapping extra code around a get.

Most people agree with what you said regarding always using an accessor. I could only find one source that agrees with my opinion (prior to today) and even it concedes the point.

I'm off to accessor-ize my public fields now. Cheers!




Re: Visual C# General Public field not accessible, forced to create an accessor...why?

Mark Dawson

Hi,

Another thing you should be aware of with const variables are that they are replaced at compile time with the actual value of the constant, so if you have two projects A and B and project B uses a const value from a class in project A, if you compile B, then change the const value in A you will also have to recompile the B project since it is not like accessing a variable, but the original const value was replaced in the calls in B, so compiling just project A will mean B is still using only the old value. Just something to be aware of.

Mark.