?
Solved

C# ->  is less accessible than field

Posted on 2011-09-13
15
Medium Priority
?
703 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 750 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
Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

 

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 750 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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