Solved

C# ->  is less accessible than field

Posted on 2011-09-13
15
692 Views
Last Modified: 2013-12-17
why am I getting this msg:
'MyNameSpace.MyClassUtil' is less accessible than field 'MyNameSpace.MyForm.Util'

//from the form class
public MyClassUtil Util = new MyClassUtil();

I almost had my whole app written and the err list stated to show this msg.
The util class has a lot of functions, I made a reference to it and then pass out a reference to all my other classes so they don't need to declare an instance of it, just use the existing.



0
Comment
Question by:sidwelle
  • 7
  • 4
  • 3
15 Comments
 
LVL 23

Expert Comment

by:Snarf0001
ID: 36533196
Is the MyClassUtil class definition inside MyForm? If so make sure you declared it

public class MyClassUtil
0
 

Author Comment

by:sidwelle
ID: 36533345
No, I moved it to an external cs.
0
 
LVL 16

Accepted Solution

by:
SriVaddadi earned 250 total points
ID: 36533446
Looks like your field "Util" in the form is public and the class MyClassUtil is not public.

Either mark the MyClassUtil as public or make the Util field private
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:sidwelle
ID: 36533469
Util is a the instance of the class.  I made sure the constructor is public as well.

0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 36533648
what is the access specifier on the Util?  Could you post the code snippet?
0
 

Author Comment

by:sidwelle
ID: 36533743
namespace MyNameSpace
{
    class MyClassUtil
    {
        //public MyClassUtil() { }     }

//...  Stuff
}
0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 36533750
It needs to be a public declaration:

namespace MyNameSpace
{
    public class MyClassUtil
    {
        //public MyClassUtil() { }     }

//...  Stuff
}
0
 

Author Comment

by:sidwelle
ID: 36533787
making the class 'public' seems to clear the errors.

Noticed that the errors didn't go away until the project was rebuilt ?

I will work with it more in the morning, but for now it looks fixed.

Thanks
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 36533793
Good to know that your problem is solved.
Please note that I was the first one to mention to make the class public in my comment #36533446
0
 

Author Comment

by:sidwelle
ID: 36533815
Yes, I see that and I had tried that before, but like I mentioned, the errors did not clear until the project was rebuilt ?  (I guess I needed someone else to suggest it.)

All the other errors cleared out just after the issues were corrected, but the was different ?


0
 
LVL 23

Assisted Solution

by:Snarf0001
Snarf0001 earned 250 total points
ID: 36535383
Some errors won't appear until compile time.  Syntax errors and things like that will be caught during the process, but public / private errors like this wont' show (or change) until you compile.
Incidentally, unless you really have a strong case for the instance, the preferred method would be to make the Utils functions static so that any class can access them without creating an instance, instead of passing the reference around.
Or make the entire class static if you have instance members.

SriVaddadi, just to make the point, I'm pretty sure my first comment in here said to make the class public, but as most people here I'm not too concerned with points, just trying help people who need it.
0
 

Author Comment

by:sidwelle
ID: 36535591
If I followed your advice and made the class static (so that I would not need to pass a reference around) how would I access it from other classes ?

0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 36535622

You should make the function members static as well (you'll get a compiler error again if you don't).  You don't really need to make the full class itself static unless you're using a lot of member variables.  SInce it's a utility class, I'd be inclined to believe you have few or none?

In any case, once the function members are static you just call with ClassName.FunctionName().

ie MyClassUtil.MyFunction();
myVar = MyClassUtil.MyFunction2(blah, blah);
0
 

Author Closing Comment

by:sidwelle
ID: 36543675
SriVaddadi was the first to write "public", but it was the followup by Snarf0001 that helped find the answer.  I didn't know that the changes making the class public would not take effect until after a build.

Making the class and all its methods static was the best solution.
(for a util class)

Thanks for the answers.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question