✅ Inconsistent accessibility: property type is less accessible than property.
Hey!
I'm making a UWP app, but from my understanding this is universal to C#.
I have 2 different UWP Pages, that are both a
public sealed partial class
.
In one of them, namely MainPage, I have a list (ObservableCollection to be precise, but I've tried changing to a normal List and it gives the same error)
This is said list: static ObservableCollection<CustomObject> MyList { get; } = new ObservableCollection<CustomObject>();
I want to access this in the second Page, let's just call it Page2. I try to call MainPage.MyList, but can't because of the lists protection level.
So I change the list to be public
, and now I have an inconsistent accessibility.
I'm having a hard time understanding what the error means exactly, can someone clear things up for me? How am I supposed to access the property from Page2?24 Replies
That error indicates that you're exposing something that is less accessibile than the type throwing it.
For example:
Here,
Bar
gets that error because Foo
is internal
but Bar
is public
while attempting to derive from Foo
.
The options to resolve this are:
1. Change the accessibility level of Bar
to internal
.
2. Change the accessibility level of Foo
to public
.
A snippet to help visualize why this is important:
I think the issue here is a bit deeper because it depends on the scope of the page as well
ideally u want to either pass the data as a parameter to the next page or have an object in common that can hold that data and will have a longer licecycle
im not as knowledge on the flow of UWP, but how are u obtaining the data you're adding to your ObservableCollection?
yup, turns out it was because my CustomObject class wasn't public :D
thanks for the help guys!
making a static object across pages from page a is not ideal thou. keep that in mind.
right
tbh it might not need to be static anymore because I changed the code up
this issue was (hopefully) the last hurdle in that change xD
how do u fill the observable thou? would u mind sharing some informaiton
I read information from a CSV file (though not because I want to)
it's basically like this:
I see so you're trying to avoid having to do that again on the next page
are u using MVVM?
assuming a
<ListView>
in UWP counts, then yesMVVM is a pattern when u have like say a file called Page.xaml and another called PageViewModel.cs
that feeds information to it in very dumb down terms
so I assume you're writing the code on the xaml.cs
yuup
so one way u could do it
and on the other end u would do
:o
not ideal but since you're doing code behind...
I was going to show u the other alternative but I dont think you're using Dependency Injection right?, so it might go over your head with complexity
main problem I'm immediately running into trying to do that is that I actually have 3 different lists (I only mentioned one because the number was irrelevant to my problem)
they have different custom objects (though they all inherit from the same abstract base class)
but I'm not entirely sure how I'd pass all 3 lists as a single parameter in C#
I'm not using it but I'm down to learn about it
oh wait reading up on dependency injection, I do think I've used this before (at the very least in other languages), at least if it's just about passing through dependencies as a parameter in a constructor, I feel like that just happens naturally in OOP
not sure how I'd do that for UWP pages though, since they're not really constructed as far as I know
that's the concept I was referring to an actually library like Microsoft.DependencyInjection
at least not manually constructed
which is a bit more complex that jus the concept of inverting the control
oh yeah no in that case, not using that
gotta quickly throw laundry into the washing machine so I'll brb
well u would have something like a FileService for example, it would be acessible by both pages i.e.:
and FileService would be something like say
This is just hypothetical code for the sake of showing an example...
So when u move to Page2 for example u could just call _fileService.LastQuery to get the files without having to go thru the proccess of requesting the user to lookup everything again.
and what actually fills the the Page1 constructor and the FileService is the DependencyInjeciton u would setup etc
etc this is just a broad example to give u an idea.
that's why I said the page parameter is fine for the way your doing it
yeah that makes sense, thanks for all that info!
https://github.com/jenius-apps/ambie/tree/main
if u have time this app is done in UWP and uses mvvm and di its a very nice project to look into to
Use the /close command to mark a forum thread as answered
thanks, will take a peek for sure!