Superfly1611 Posted February 15, 2007 Posted February 15, 2007 Hi Hi, Is it possible to define a class within a class? I have two classes 1: Product 2: ProductImagesCollection I would like to restrict the ProductImagesCollection class so that it can only be used as part of an instantiated Product object and therefore cannot be instantiated in any other way. Is this possible? Many Thanks Quote
NeuralJack Posted February 15, 2007 Posted February 15, 2007 Ya , this is done all the time. Remember, Forms are just classes too. It sounds like all you want to do is Declare a new ProductImagesCollection in your Product Class. This way the ProductImagesCollection is only instantiated when a Product is instantiated. Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Superfly1611 Posted February 15, 2007 Author Posted February 15, 2007 you see I thought this was possible but I've obviously got the sytax wrong because my compiler keeps kicking up a fuss! using System; namespace foo { public class Product { private int _productID; private string _make; private string _model; private ProductImageCollection _images; public Product() { } public int ProductID { get{return _productID;} } public string Make { get {return _make;} set {_make = value;} } public string Model { get {return _model;} set {_model = value;} } public ProductImageCollection Images { get {return _images;} } private class ProductImageCollection { public ProductImageCollection() { } } } } Quote
Superfly1611 Posted February 15, 2007 Author Posted February 15, 2007 ok - worked it out I the class needs to be public - and the constructor needs to be private doh! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.