0

I'm making a Xamarin cross-platform app and i'm trying to pass a custom class parameter to the constructor of a new page class, but the error "Inconsistent accessibility: parameter type is less accesible than method" showed up.

I have a page that shows a ListView with grouped items and I want to push a new page into the navigation stack when any of the items of the ListView is tapped. All of the XAML and C# code in the .cs and .xaml files where I declare the event that pushes the new page is OK and doesn't give any kind of errors. I'm declaring the event right here:

        private async void Exercises_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            var tappedExercise = (Exercise)sender;
            if(tappedExercise != null)
            {
                await Navigation.PushAsync(new ExerciseInfoPage(tappedExercise));
            }
        }

The ExerciseInfoPage that I push and that gives the error is right here:

using PumpFit.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace PumpFit
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ExerciseInfoPage : ContentPage
    {
        public ExerciseInfoPage(Exercise tappedExercise)
        {
            InitializeComponent();
        }
    }
}

The error shows only when I'm trying to pass a custom class like Exercise, when I pass a .NET class or a class that alredy came with the libraries or packages of the system it doesn't give the error. The error: error image

PS: I need to send that Exercise parameter because In the new page I need to show details about the selected item of the ListView

IF SOMEONE COULD HELP I WILL BE VERY GRATEFULL :)

Dacaramo
  • 75
  • 10
  • is `Exercise` a public class? – Jason Jul 24 '20 at 21:00
  • Can you post the `Exercise` class definition please? – War10ck Jul 24 '20 at 21:01
  • Does this answer your question? [Inconsistent accessibility error C#](https://stackoverflow.com/questions/7805610/inconsistent-accessibility-error-c-sharp) – Joe Sewell Jul 24 '20 at 21:05
  • Thanks for the quick response guys it was a pretty dumb thing. Exercise was internal and not public. Thanks for the help! – Dacaramo Jul 24 '20 at 22:18
  • But if someone could explain to me why it wasn't working with internal or default visibility I will appreciate it – Dacaramo Jul 24 '20 at 22:21
  • @Dacaramo Hi , have you solved this ? – Junior Jiang Jul 27 '20 at 03:06
  • @Dacaramo Your `ExerciseInfoPage` is a top-level `public` class and its constructor is also `public`. This means that it some other project / assembly could reference your project / assembly and create an instance of `ExerciseInfoPage`. However, if `Exercise` is not also `public`, there's a contradiction: the constructor is visible to outside code, but the argument necessary to call the constructor is not visible. – Joe Sewell Jul 28 '20 at 13:44
  • @JuniorJiang-MSFT the problem was that Exercise class was Internal and not public, but thanks for your help! :) Anyway it was my fault of not giving enough information, because the code samples that I have provided were not enough. – Dacaramo Jul 31 '20 at 16:03

2 Answers2

0

The reason that is the Constructor of ExerciseInfoPage is public , we need to keep the parameter with it be the same attribute .

For example , if setting the constructor be private , there will be no error :

private ExerciseInfoPage(Exercise tappedExercise)
{
    InitializeComponent();
}

However , navigation method will occurs error :

Navigation.PushAsync(new ExerciseInfoPage(tappedExercise));

Therefore , here we need to use public for Constructor first , then Exercise need to be public .

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30
  • I do not try that but thanks anyway! I have alredy found the error! Also don't know if it is a good practice to have private constructors but maybe it may work :) – Dacaramo Jul 31 '20 at 16:44
0

Thanks for the comments and replies! you make me find that the Exercise class that I'm using in the constructor parameters was internal and not a public class, That issue was causing the error. Make sure to always set the visibility of all of your classes, if you do so that will prevent dumb errors like this one to happen in your code.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dacaramo
  • 75
  • 10