开发者

c# nesting classes and visibility?

开发者 https://www.devze.com 2023-01-27 08:20 出处:网络
I am trying to get the same functionality that a combobox has, like combobox1.Items.Add() // editor.Tags.Tags1()

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();
    }



    publi开发者_高级运维c 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.


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();


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号