Solved

C# ->  is less accessible than field

Posted on 2011-09-13
15
684 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 22

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 22

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 22

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 22

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 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