C# -> is less accessible than field

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.



sidwelleAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
SriVaddadiConnect With a Mentor Commented:
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
 
Snarf0001Commented:
Is the MyClassUtil class definition inside MyForm? If so make sure you declared it

public class MyClassUtil
0
 
sidwelleAuthor Commented:
No, I moved it to an external cs.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
sidwelleAuthor Commented:
Util is a the instance of the class.  I made sure the constructor is public as well.

0
 
SriVaddadiCommented:
what is the access specifier on the Util?  Could you post the code snippet?
0
 
sidwelleAuthor Commented:
namespace MyNameSpace
{
    class MyClassUtil
    {
        //public MyClassUtil() { }     }

//...  Stuff
}
0
 
Snarf0001Commented:
It needs to be a public declaration:

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

//...  Stuff
}
0
 
sidwelleAuthor Commented:
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
 
SriVaddadiCommented:
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
 
sidwelleAuthor Commented:
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
 
Snarf0001Connect With a Mentor Commented:
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
 
sidwelleAuthor Commented:
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
 
Snarf0001Commented:

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
 
sidwelleAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.