Generics / Property Question

D

Doug Handler

Hi (and thanks in advance)

I have a class called Person that i want to expose a public property called
Numbers. I have a class called PhoneNumbers that is a generic collection of
Phone. All i want to do is be able to access the Numbers property from
within Person. In other words, an individual (Person) can have multiple
PhoneNumbers. I'm getting the following error:
Error 1 Inconsistent accessibility: property type 'PhoneNumbers' is less
accessible than property 'Person.Numbers'

Here's the code:
in Person
.....
PhoneNumbers _phoneNumbers = new PhoneNumbers();

public PhoneNumbers Numbers
{
get {return this._phoneNumbers; }
set {this._phoneNumbers = value; }
}

in PhoneNumbers
....
List<Phone> _phoneList = new List<Phone>();

pubilc List<Phone> Phone
{
get { return this._phoneList; }
set { this._phoneList = value; }
}
 
N

Nicholas Paldino [.NET/C# MVP]

Doug,

Do you have the PhoneNumbers class defined in the same class? If so,
then that type needs to be public, since you are going to expose a property
of that type. It is most likely defined as not public, hence the error.

Hope this helps.
 
J

Jon Skeet [C# MVP]

Doug Handler said:
I have a class called Person that i want to expose a public property called
Numbers. I have a class called PhoneNumbers that is a generic collection of
Phone. All i want to do is be able to access the Numbers property from
within Person. In other words, an individual (Person) can have multiple
PhoneNumbers. I'm getting the following error:
Error 1 Inconsistent accessibility: property type 'PhoneNumbers' is less
accessible than property 'Person.Numbers'

Right. My guess is that your PhoneNumbers class is internal. Your
Numbers property is public, meaning that callers outside the assembly
can use it, even though they don't know anything about the PhoneNumbers
type.

Either make PhoneNumbers public, or make the Numbers property internal.
 
D

Doug Handler

Nicholas,

I believe I discovered the problem - conflict of public access. Thanks for
getting back to me so quickly.

dh
Nicholas Paldino said:
Doug,

Do you have the PhoneNumbers class defined in the same class? If so,
then that type needs to be public, since you are going to expose a
property of that type. It is most likely defined as not public, hence the
error.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Doug Handler said:
Hi (and thanks in advance)

I have a class called Person that i want to expose a public property
called Numbers. I have a class called PhoneNumbers that is a generic
collection of Phone. All i want to do is be able to access the Numbers
property from within Person. In other words, an individual (Person) can
have multiple PhoneNumbers. I'm getting the following error:
Error 1 Inconsistent accessibility: property type 'PhoneNumbers' is less
accessible than property 'Person.Numbers'

Here's the code:
in Person
....
PhoneNumbers _phoneNumbers = new PhoneNumbers();

public PhoneNumbers Numbers
{
get {return this._phoneNumbers; }
set {this._phoneNumbers = value; }
}

in PhoneNumbers
...
List<Phone> _phoneList = new List<Phone>();

pubilc List<Phone> Phone
{
get { return this._phoneList; }
set { this._phoneList = value; }
}
 
D

Doug Handler

Jon,

You were correct - thanks. I got all confused w/ the generic properties
within properties.

dh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top