Solved

C# ->  is less accessible than field

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

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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 …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now