Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
hi all. I have two different classes in my solution that I integrate them in one class because I need two classes fields in some query. now the error is:
"Inconsistent accessibility: field type 'Salary.employmentInfo' is less accessible than field 'Salary.PersonelInfoAndEmploymentInfo._empinfo"

C#
 public class PersonelInfoAndEmploymentInfo
    {
        public PersonelInfo _pinfo;
        public employmentInfo _empinfo;
    }
public class PersonelInfo
{
//some fields
}
class employmentInfo  //[edit] removed public per OP's comment -- Matt T Heffron[/edit]
{
//some fields
}
Posted
Updated 27-Jan-14 9:28am
v2
Comments
Kornfeld Eliyahu Peter 27-Jan-14 14:59pm    
This code is just fine. Are you sure you posted your exact code?
mit62 27-Jan-14 15:06pm    
oh,sorry public had been removed from "employmentInfo" class. thanks
Sergey Alexandrovich Kryukov 27-Jan-14 15:27pm    
I'm not sure you need public; are you sure you want to access the field from other assembly? in not, it should be internal. You should better give only sufficient access, no more.
Besides, non-private fields can usually be considered as bad style; so better use properties.
I answered your question, please see Solution 1.
—SA
Kornfeld Eliyahu Peter 27-Jan-14 15:39pm    
Now the error is clear - you can't expose a private type via a public class member.
Try to explain what do you want to achieve (why employmentInfo is private) so someone should advise you better...
bowlturner 27-Jan-14 15:05pm    
If you're still having that problem, you'll need to post more code, there is nothing inherently wrong with what is posted.

mit62 asked:
I have another problem with this class:
C#
PersonelInfoAndEmploymentInfo person = new PersonelInfoAndEmploymentInfo();
person._empinfo.jobLocation = "asdasd";

now error is: Object reference not set to an instance of an object"
This is also easy to explain:
This indicated that you are trying to dereference object which is currently null. You constructed the object person, but from this message, it is apparent that person._empinfo remains null. Of course, person._empinfo.jobLocation does not have to be initialized, you would do it on next step, by assigning that string to it. None of the objects of reference type are constructed by themselves; ultimately, the lifetime starts from something like _empinfo = new ....

Now, this is one of the very easiest cases to detect and fix. You should be able to easily resolve all such problems on your own.

It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 
The message is self-explaining. You did not show the relevant piece of code, but it's easy to speculate. Supposed that you have a public field (considered to be a bad style, better make is a property). What does it mean? It means it would be accessible from any code, including the code from some other assembly.

Good. Now, imagine that, for example, you actually want to access this field from other assembly, from the class which is not derived from yours. Is it possible if the field is public? Not always. Only if the type is public, too, otherwise how would you use the field without its type? Say, if the type is private, you don't have access to it from outside your class. It could be internal, then you could not accessed from the other assembly. It could be protected or internal protected, then you would need to have a derived class outside the declaring assembly. Do you see the inconsistency. If you make some member to be accessible in some scope, all types involved in this declaration should have compatible access level with this scope.

—SA
 
Share this answer
 
v2
Comments
mit62 28-Jan-14 7:04am    
I have another problem with this class:
PersonelInfoAndEmploymentInfo person = new PersonelInfoAndEmploymentInfo();
person._empinfo.jobLocation = "asdasd";

now error is: Object reference not set to an instance of an object"
Sergey Alexandrovich Kryukov 28-Jan-14 10:21am    
Next time, better create a new question; it would not be a re-post, because it is very different from your original post.
If you want, you could leave me a comment about your now post.

Not to worry. This is a very simple thing. I provided a complete answer, please see Solution 2.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900