C# nested class/struct visibility -
i'm trying figure out proper syntax achieve api goal, struggling visibility.
i want able access messenger
instance's member msgr.title.forsuccesses
.
however, not want able instantiate messenger.titles
outside messenger
class.
i'm open making messenger.titles struct.
i'm guessing need sort of factory pattern or something, have no idea how i'd go doing that.
see below:
class program { static void main(string[] args) { var m = new messenger { title = { forerrors = "an unexpected error occurred ..." } }; // should allowed var t = new messenger.titles(); // should not allowed } } public class messenger { // i've tried making private/protected/internal... public class titles { public string forsuccesses { get; set; } public string fornotifications { get; set; } public string forwarnings { get; set; } public string forerrors { get; set; } // i've tried making private/protected/internal well... public titles() {} } public titles title { get; private set; } public messenger() { title = new titles(); } }
you need make titles private , expose interface instead of it.
class program { static void main(string[] args) { var m = new messenger { title = { forerrors = "an unexpected error occurred ..." } }; // allowed var t = new messenger.titles(); // not allowed } } public class messenger { public interface ititles { string forsuccesses { get; set; } string fornotifications { get; set; } string forwarnings { get; set; } string forerrors { get; set; } } private class titles : ititles { public string forsuccesses { get; set; } public string fornotifications { get; set; } public string forwarnings { get; set; } public string forerrors { get; set; } } public ititles title { get; private set; } public messenger() { title = new titles(); } }
Comments
Post a Comment