Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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.

Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...