Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Restricting class creating only 3 objects

Restricting class creating only 3 objects

Scheduled Pinned Locked Moved C#
csharphelp
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 3519179
    wrote on last edited by
    #1

    i am not able to achieve this task in C#, i m new to this , please help

    J 1 Reply Last reply
    0
    • U User 3519179

      i am not able to achieve this task in C#, i m new to this , please help

      J Offline
      J Offline
      Jens Meyer
      wrote on last edited by
      #2

      Use a Singleton-like approach. Make the constructor private and add a static method. In this method return a new instance of the class when a static counter is less than 3 (starting with 0). Then increment the counter. If the counter is >2 do what ever you like to do if the third instance is delivered. Small example: public class MyRestrictedClass { private static int instanceCounter = 0; private MyRestrictedClass() { //cool ctor stuff } public MyRestrictedClass GetNextInstance() { if(instanceCounter<=2) { instanceCounter++; return new MyRestrictedClass(); } throw new Exception("Maximum number of instances exceeded."); } } Best regards Jens

      When in trouble, when in doubt, run in circles, scream and shout

      modified on Thursday, May 27, 2010 3:18 AM

      U 1 Reply Last reply
      0
      • J Jens Meyer

        Use a Singleton-like approach. Make the constructor private and add a static method. In this method return a new instance of the class when a static counter is less than 3 (starting with 0). Then increment the counter. If the counter is >2 do what ever you like to do if the third instance is delivered. Small example: public class MyRestrictedClass { private static int instanceCounter = 0; private MyRestrictedClass() { //cool ctor stuff } public MyRestrictedClass GetNextInstance() { if(instanceCounter<=2) { instanceCounter++; return new MyRestrictedClass(); } throw new Exception("Maximum number of instances exceeded."); } } Best regards Jens

        When in trouble, when in doubt, run in circles, scream and shout

        modified on Thursday, May 27, 2010 3:18 AM

        U Offline
        U Offline
        User 3519179
        wrote on last edited by
        #3

        thank u Jens,, i m facing another problem in Socket programing in Vb.Net, i had developed a Server which recieve data from clients and forward to others, it was a multi threading application but now it stuck up , it was developed using a VB Winsock wrapper, now i want to develop it using .Net sockets, can u help me in this

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups