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.
Make your
Tagspropertyreadonly, then it can be initialized in the constructor only:The object reference stored in
Tagscan’t be changed later then and that code:wouldn’t compile.
Or, second possibility, even better – expose only public getter for your
Tagsproperty and keep the instance privately inside yourMyEditorClassclass, 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
ComboBoxso thatTagClasshas access to the editor, you can pass the editor instance toTagClassinternal constructor. The constructor is internal to not allow external code to useTagClassdirectly.And now this will work: