Class within a Class

Superfly1611

Regular
Joined
Feb 17, 2004
Messages
66
Location
UK
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
 
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.
 
you see I thought this was possible but I've obviously got the sytax wrong because my compiler keeps kicking up a fuss!
Code:
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()
            {
            }
        }
    }
}
 
Back
Top