Question

I am trying to get the same functionality that a combobox has, like combobox1.Items.Add() // editor.Tags.Tags1()

Like this:

class Main()
{
    // The editor is passed into the editor class, that allows the editor class to update the editor.
    MyEditorClass editor = new MyEditorClass(editorBox);
    editor.Tags.InsertTag1();

    // Now i don't want people to do this
    // MyEditorClass.TagClass tags = new MyEditorClass.TagClass();
}

The reason is that the tags class calls the editorBox passed into the MyEditorClass and if you create a tags class without that editor, it won't work.

My MyEditorClass looks like this:

public class MyEditorClass
{
    static RichTextBox editor;
    public TagClass Tags;

    public MyEditorClass(RichTextBox editorBox)
    {
        editor = editorBox;
        Tags = new TagClass();
    }



    public class TagClass
    {
        public void InsertTag1()
        {
            editor.Text += "Test tag 1";
        }
    }
}

I was trying to make the TagClass static but that didn't work. How is the combobox structured? Since you cant use Combobox.Items but if you declare one you can use the Combobox.Items on the one that you declared.

Was it helpful?

Solution

Make your Tags property readonly, then it can be initialized in the constructor only:

public readonly TagClass Tags;

The object reference stored in Tags can't be changed later then and that code:

MyEditorClass editor = new MyEditorClass(editorBox);
editor.Tags = new MyEditorClass.TagClass();

wouldn't compile.

Or, second possibility, even better - expose only public getter for your Tags property and keep the instance privately inside your MyEditorClass class, like in example below.

By the way, it don't have to do anything with nesting classes. It is quite strange to have public class inside a public class.


EDIT: To have a structure similar to ComboBox so that TagClass has access to the editor, you can pass the editor instance to TagClass internal constructor. The constructor is internal to not allow external code to use TagClass directly.

public class MyEditorClass
{
    private readonly RichTextBox editor;
    private readonly TagClass tags;

    public TagClass Tags
    {
        get 
        {
            return tags; 
        } 
    }

    public MyEditorClass(RichTextBox editorBox)
    {
        editor = editorBox;
        tags = new TagClass(editorBox);
    }
}

public class TagClass
{
    private RichTextBox _editor;

    internal TagClass(RichTextBox editor)
    {
        _editor = editor;
    }

    public void InsertTag1()
    {
        _editor.Text += "Test tag 1";
    }
 }

And now this will work:

MyEditorClass editor = new MyEditorClass(editorBox);
editor.Tags.InsertTag1();

OTHER TIPS

In the TagClass class add member of type MyEditorClass and assign it when creating new TagClass instance:

public class TagClass
{
    private MyEditorClass editor = null;

    public TagClass(MyEditorClass parent)
    {
        this.editor = parent;
    }

    public void InsertTag1()
    {
        editor.Text += "Test tag 1";
    }
}

...

Tags = new TagClass(this);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top