Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# ->  is less accessible than field

Posted on 2011-09-13
15
Medium Priority
?
710 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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