Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 17th, 2006, 07:17 AM
Friend of Wrox
 
Join Date: May 2006
Location: Delhi, Delhi, India.
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default OOPs

How to create a class of which only single instance be created?

Bijgupt
__________________
Bijgupt
Reply With Quote
  #2 (permalink)  
Old May 17th, 2006, 07:26 AM
Authorized User
 
Join Date: Apr 2006
Location: Horsens, , Denmark.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

With the following code:

private static SingletonClass instance = null;
private static object lockObj = new object();

private SingletonClass()
{}//constructor

public static SingletonClass Instance
{
    get
    {
        lock(lockObj)
        {
            if(instance == null)
                instance = new SingletonClass();
            return instance;
        }
    }
}

you can only use the one instance.

To use it simply use the property:
SingletonClass.Instance.WhateverMethod();

The first time you use it, the instance is initialized. All following uses will use the same instance.

Reply With Quote
  #3 (permalink)  
Old May 17th, 2006, 07:49 AM
Friend of Wrox
 
Join Date: May 2006
Location: Delhi, Delhi, India.
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you so much. I was searching the solution for long time. Now i got what i wanted. Tahnks again.

Bijgupt
Reply With Quote
  #4 (permalink)  
Old May 17th, 2006, 07:54 AM
Authorized User
 
Join Date: Apr 2006
Location: Horsens, , Denmark.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My pleasure.

Reply With Quote
  #5 (permalink)  
Old May 18th, 2006, 04:27 AM
Friend of Wrox
 
Join Date: May 2006
Location: Delhi, Delhi, India.
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I want to create an object during runtime. The problem is that i want to supply object name at runtime. That is whatever the name user supply, object of that name should be created.How can i do this?

Bijgupt
Reply With Quote
  #6 (permalink)  
Old May 18th, 2006, 05:36 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Please start a new thread for a new question.
Look at the Activator class, you'll need the assembly name and type.

--

Joe (Microsoft MVP - XML)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OOPS tutorail interview questions gsri General .NET 1 September 15th, 2008 10:42 AM
oops with c#.net sureshmm General .NET 8 March 22nd, 2007 10:43 PM
oops with c#.net sureshmm ASP.NET 1.0 and 1.1 Basics 3 March 21st, 2007 11:58 AM
Oops, wrong subject! Aaron Edwards ASP.NET 2.0 Basics 0 September 13th, 2006 07:37 PM
Lost part of a Primary Key - Oops! owain SQL Language 7 June 15th, 2003 01:29 PM



All times are GMT -4. The time now is 06:50 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.