Search code examples
c#monoxamarin-studiogtk#access-levels

How can I change access level of fields generated by GUI designer in Xamarin Studio


I'm writing my code in Gtk# using Xamarin Studio. When I create a new window and a TreeView in it, the access level will be private. I want to use it (TreeView) in an other class so I would like to change the access level to internal, but I couldn't find how can I make that. Thanks for any help in advance. Here is the code which I want to change by the GUI designer (not writing inside to the code, because it will be overwritten by the GUI designer...)

    // This file has been generated by the GUI designer. Do not modify.
namespace XX_xxxx
{
    public partial class Settings
    {
        private global::Gtk.VBox vbox1;

        private global::Gtk.ScrolledWindow GtkScrolledWindow;

        private global::Gtk.TreeView settingsTreeView;

        private global::Gtk.HBox hbox1;

        private global::Gtk.ToggleButton saveAndCloseButton;

        private global::Gtk.ToggleButton closeButton;

        protected virtual void Build ()
        {

Here is where I want to use (in an other class where I use an instance of the Settings class): settings.settingsTreeView.Model = settingsListStore;

The error message is:

Error CS0122: `XX_xxxxx.Settings.settingsTreeView' is inaccessible 
    due to its protection level (CS0122) (XX_xxxx_GUI)

Solution

  • Generally the controls are marked as private to stop code in other modules from reaching in and changing attributes of the class directly. The good news is that there is another (IMHO) better way to do what you are trying to do. Since the generated class is marked as partial you can use a non generated partial Settings class with whatever internal methods you add for what your other class needs to interact with the UI.

    This approach is generally considered superior as it will allow you to control how the other class interacts with the Settings class's private members (the controls). So you could add a method like this:

    public partial class Settings
    { 
        internal void SetModel(ModelType model)
        {
            // Check if valid model and throw some type of argument exception if not
            settingsTreeView.Model = model;
        }
    }
    

    and then call it like this:

    settings.SetModel(settingsListStore);