Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hey all,

I am trying to get a listbox which the options are the physical drive model names. I have managed to get the physical drive information into a ArrayList and am trying to now get the listbox to display the model names. I am trying to do this with this code.

C#
foreach (HardDrive d in Reporting.hdCollection)
           {
               DriveBox.Items.Add(d.model);
           }


This d.model is giving me the error that it is inaccessible due to its protection level however it is declared in the reporing.cs in a public way

C#
class Reporting
{
public static ArrayList hdCollection = new ArrayList();


I am not sure why this is happening, but would really appreciate any help anyone could give. Because I am really out of ideas.
Posted
Updated 4-Jul-21 12:42pm
Comments
Ben Marks 7-Feb-12 14:12pm    
is the class is simply declared as
class Reporting

then isnt it public by default? I changed it to public anyway and it made no difference.

The reason I have used an arraylist instead of a straight list is because other information is being held such as serial numbers and other information.
Sergey Alexandrovich Kryukov 7-Feb-12 17:12pm    
If it says "inaccessible due to its protection level", it is inaccessible, period. You don't provide a relevant code sample.

Make sure you understand access modifiers. "In a public" way is not informative. And it does not have to be public, "internal" is sufficient in many cases.

--SA
Ben Marks 8-Feb-12 3:01am    
I accept your point SA, however if you see the for each I provided it is able to see Reporting.hdcollection, which is the arraylist that contains the model field.

It is only this field that is inaccessible, does the fact that it can access Reporting.hdcollection mean that it is public?

Check that model of your HardDrive class is public/internal or whatever requered.

And I think it's better to use List<HardDrive> instead of ArrayList
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 7-Feb-12 17:10pm    
Of course. Both item are are valid, up to just one detail.

Most likely, it does not have to be public, should be internal; one should not provide more access than it is really required.

I voted 4.
--SA
TimGameDev 7-Feb-12 19:59pm    
Correct, I have simplified the answer too much here. Thanks Sergey.

Timur
Though it is irrelevant to the case at hand, for the benefit of the next person who arrives at this article through a search engine, if the default constructor of your base class is marked as private, derived classes will incur a CS0122 diagnostic.

Instead, you must promote the private method to protected.

The protected method remains inaccessible to consumers of the derived class unless said class overrides it with a new constructor.
 
Share this answer
 

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