Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Inconsistent accessibility: property type

Tags:

c#

What's wrong with

public partial class MainWindow : Window
{
    public ObservableCollection<TabViewModel> Tabs { get; set; }
    public ICollectionView TabsViewSource { get; set; }
    public int CurrentIndex { get { return TabsViewSource.CurrentPosition; } }

I get

Inconsistent accessibility: property type 'System.Collections.ObjectModel.ObservableCollection' is less accessible than property 'TabsRendering.MainWindow.Tabs'

when i change the code to

public partial class MainWindow : Window
{
    ObservableCollection<TabViewModel> Tabs { get; set; }
    public ICollectionView TabsViewSource { get; set; }
    public int CurrentIndex { get { return TabsViewSource.CurrentPosition; } }

It works. Whats wrong with the public on the ObservableCollection

like image 958
Jiew Meng Avatar asked Oct 21 '10 23:10

Jiew Meng


2 Answers

MakeTabViewModela public type too.

Obviously, it doesn't make sense for a public property on a public containing-type to be of a type that is not public. How could the property present itself to external assemblies?

Your second sample works because, as a general rule, providing no accessibility modifiers means that the least applicable modifier is chosen as the default - in this case: private. Clearly, there are no consistency issues with declaring a private property of an internal (?) type.

like image 84
Ani Avatar answered Nov 06 '22 07:11

Ani


What's the accessibility on TabViewModel? I'm guessing it's not public.

like image 4
Phil Avatar answered Nov 06 '22 09:11

Phil