473,480 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Modifying singleton data.

I have a singleton class that I want to only contain a hashtable. I want to
be able to modify this hashtable at will.
The problem I am having is each time I try to update the data using the
"SetState" method, "_Instance" keeps
on getting reset. I have a hard time grasping the idea how a singleton just
holds the data... anyway, here's
my test code for console if anyone can help.

V.

using System;
using System.Collections;

namespace test
{

class Test
{

[STAThread]
static void Main(string[] args)
{
Console.WriteLine("test...");

ObjectState.SetState(3, "fork");

Console.WriteLine(ObjectState.Instance().GetState( 3));
ObjectState.SetState(4, "spoon");

Console.WriteLine(ObjectState.Instance().GetState( 4));
Console.WriteLine(ObjectState.Instance().GetState( 3));
}
}

//Singleton....
public class ObjectState
{
private static ObjectState _Instance = null;
private Hashtable htObjects = new Hashtable();

private ObjectState(int nId, String sState)
{
htObjects.Add(nId, sState);
}

public static ObjectState SetState(int nId, String sState)
{
//if(_Instance == null)
//{
_Instance = new ObjectState(nId, sState); //<--- CONFUSION
//}

return _Instance;
}

public static ObjectState Instance()
{
return _Instance;
}

public String GetState(int nId)
{
return (String)htObjects[nId];
}
}

}
Nov 16 '05 #1
6 1892
Is this what you need? If not, would need more description of what your
trying to do. Note also that this is not thread safe. So if you need to
share this object, you need to sync.

using System;
using System.Collections;

namespace test
{
class Test
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Test...\n");
ObjectState obj = ObjectState.GetObjectState();
Console.WriteLine("Add '3, fork.'");
obj.SetState(3, "fork");
Console.WriteLine("{0} {1}", 3, obj.GetState(3));

// Add 4.
Console.WriteLine();
Console.WriteLine("Add '4, spoon.'");
obj.SetState(4, "spoon");
Console.WriteLine("{0} {1}", 4, obj.GetState(4));
Console.WriteLine("{0} {1}", 3, obj.GetState(3));
Console.ReadLine(); // Stop console from closing until Enter.
}
}

//Singleton....
public sealed class ObjectState
{
private static ObjectState instance = null;
private readonly Hashtable htObjects;

static ObjectState()
{
instance = new ObjectState();
}

private ObjectState()
{
htObjects = new Hashtable();
}

public static ObjectState GetObjectState()
{
return ObjectState.instance;
}

public void SetState(int nId, string sState)
{
htObjects.Add(nId, sState);
}

public string GetState(int nId)
{
return (string)htObjects[nId];
}

public void Clear()
{
htObjects.Clear();
}
}
}

--
William Stacey, MVP

"VidalSasoon" <th******************@pooper.com> wrote in message
news:eb********************@news20.bellglobal.com. ..
I have a singleton class that I want to only contain a hashtable. I want to be able to modify this hashtable at will.
The problem I am having is each time I try to update the data using the
"SetState" method, "_Instance" keeps
on getting reset. I have a hard time grasping the idea how a singleton just holds the data... anyway, here's
my test code for console if anyone can help.

V.

using System;
using System.Collections;

namespace test
{

class Test
{

[STAThread]
static void Main(string[] args)
{
Console.WriteLine("test...");

ObjectState.SetState(3, "fork");

Console.WriteLine(ObjectState.Instance().GetState( 3));
ObjectState.SetState(4, "spoon");

Console.WriteLine(ObjectState.Instance().GetState( 4));
Console.WriteLine(ObjectState.Instance().GetState( 3));
}
}

//Singleton....
public class ObjectState
{
private static ObjectState _Instance = null;
private Hashtable htObjects = new Hashtable();

private ObjectState(int nId, String sState)
{
htObjects.Add(nId, sState);
}

public static ObjectState SetState(int nId, String sState)
{
//if(_Instance == null)
//{
_Instance = new ObjectState(nId, sState); //<--- CONFUSION
//}

return _Instance;
}

public static ObjectState Instance()
{
return _Instance;
}

public String GetState(int nId)
{
return (String)htObjects[nId];
}
}

}


Nov 16 '05 #2
I'm rather new to C# but you do not seem to have implemented ObjectState as
a singleton.
I must use static style calls while keeping data in memory thoughout my app.
To clarify, using only static style calls to the singleton, how would I
modify a hashtable?

something like:

Singleton.SetHashTable("key1", "value1"); // either create new key entry or
modify existing one
.....
String sTemp = Singleton.Instance().GetValue("key1");

does this make sense?

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:uP*************@TK2MSFTNGP11.phx.gbl...
Is this what you need? If not, would need more description of what your
trying to do. Note also that this is not thread safe. So if you need to
share this object, you need to sync.

using System;
using System.Collections;

namespace test
{
class Test
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Test...\n");
ObjectState obj = ObjectState.GetObjectState();
Console.WriteLine("Add '3, fork.'");
obj.SetState(3, "fork");
Console.WriteLine("{0} {1}", 3, obj.GetState(3));

// Add 4.
Console.WriteLine();
Console.WriteLine("Add '4, spoon.'");
obj.SetState(4, "spoon");
Console.WriteLine("{0} {1}", 4, obj.GetState(4));
Console.WriteLine("{0} {1}", 3, obj.GetState(3));
Console.ReadLine(); // Stop console from closing until Enter.
}
}

//Singleton....
public sealed class ObjectState
{
private static ObjectState instance = null;
private readonly Hashtable htObjects;

static ObjectState()
{
instance = new ObjectState();
}

private ObjectState()
{
htObjects = new Hashtable();
}

public static ObjectState GetObjectState()
{
return ObjectState.instance;
}

public void SetState(int nId, string sState)
{
htObjects.Add(nId, sState);
}

public string GetState(int nId)
{
return (string)htObjects[nId];
}

public void Clear()
{
htObjects.Clear();
}
}
}

--
William Stacey, MVP

"VidalSasoon" <th******************@pooper.com> wrote in message
news:eb********************@news20.bellglobal.com. ..
I have a singleton class that I want to only contain a hashtable. I want

to
be able to modify this hashtable at will.
The problem I am having is each time I try to update the data using the
"SetState" method, "_Instance" keeps
on getting reset. I have a hard time grasping the idea how a singleton

just
holds the data... anyway, here's
my test code for console if anyone can help.

V.

using System;
using System.Collections;

namespace test
{

class Test
{

[STAThread]
static void Main(string[] args)
{
Console.WriteLine("test...");

ObjectState.SetState(3, "fork");

Console.WriteLine(ObjectState.Instance().GetState( 3));
ObjectState.SetState(4, "spoon");

Console.WriteLine(ObjectState.Instance().GetState( 4));
Console.WriteLine(ObjectState.Instance().GetState( 3));
}
}

//Singleton....
public class ObjectState
{
private static ObjectState _Instance = null;
private Hashtable htObjects = new Hashtable();

private ObjectState(int nId, String sState)
{
htObjects.Add(nId, sState);
}

public static ObjectState SetState(int nId, String sState)
{
//if(_Instance == null)
//{
_Instance = new ObjectState(nId, sState); //<--- CONFUSION
//}

return _Instance;
}

public static ObjectState Instance()
{
return _Instance;
}

public String GetState(int nId)
{
return (String)htObjects[nId];
}
}

}

Nov 16 '05 #3
Singleton has nothing to do with static or instance methods on your object.
A singleton can never have more than one instance at any one time. The
methods you define on the singleton does not matter in respect to that.
I must use static style calls while keeping data in memory thoughout my app.

Why must you use static style calls? As shown, you can keep data in memory
throughout the app as long as you keep a ref to the singleton somewhere
(i.e. form var, etc.)
To clarify, using only static style calls to the singleton, how would I
modify a hashtable?


Create the hashtable as a static var in static constructor. Then use static
methods as needed if you have some need to only use static. Is this a class
project?

--
William Stacey, MVP

Nov 16 '05 #4

The intent of a Singleton is to have a single _instance_ of a class that you
can use throughout your app.

VidalSasoon wrote:

[...snip...]
The problem I am having is each time I try to update the data using the
"SetState" method, "_Instance" keeps
on getting reset.
You created a static "SetState" method, that creates a new instance of
ObjectState each time it's called:
public static ObjectState SetState(int nId, String sState)
{
//if(_Instance == null)
//{
_Instance = new ObjectState(nId, sState); //<--- CONFUSION
//}

return _Instance;
}


This should be an instance method, using a static method to access the
single instance of ObjectState, like

public static ObjectState Instance()
{
if (_Instance == null)
_Instance = new ObjectState(initialId, initialState);

return _Instance;
}

public ObjectState SetState(int nId, String sState)
{
// whatever
}

This method should work on an instance of ObjectState and therefore it must
not be static...

You'd call it like

ObjectState.Instance.SetState(3, "fork");

HTH
Nov 16 '05 #5
I'm working on a 3D program with DirectX. I was at the point of adding
collision detection. What I need is to know where all the game objects are
in space at any given time.
I was thinking of using a hashtable to store the objectID and it's
corresonding matrix. So for every frame I would update each object in the
hashtable.
With this info I would be able to cycle through the hashtable and see which
object was closest and I would then be able to handle the collisions...

I need a global variable that I could access anywhere. newsgroups pointed to
singletons... a simpler alternative would be great but i'm not sure it
exists.

I've been a programmer for a few years but C# and OO are still quite new to
me.

http://kjo.myip.org/

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:ub****************@TK2MSFTNGP15.phx.gbl...
Singleton has nothing to do with static or instance methods on your
object.
A singleton can never have more than one instance at any one time. The
methods you define on the singleton does not matter in respect to that.
I must use static style calls while keeping data in memory thoughout my

app.

Why must you use static style calls? As shown, you can keep data in
memory
throughout the app as long as you keep a ref to the singleton somewhere
(i.e. form var, etc.)
To clarify, using only static style calls to the singleton, how would I
modify a hashtable?


Create the hashtable as a static var in static constructor. Then use
static
methods as needed if you have some need to only use static. Is this a
class
project?

--
William Stacey, MVP

Nov 16 '05 #6
You can do all of that with the code I showed you. From anywhere in the
program you can get the single instance by calling GetObjectState (using
your method name.) Then as you have the ref, just call methods on it as
normal. Do you not see this? Maybe you could ask another question in a
different way. However, this code will work (naturally we still need to
address error checking, threading issues, etc.)

--
William Stacey, MVP

"VidalSasoon" <th******************@pooper.com> wrote in message
news:nL********************@news20.bellglobal.com. ..
I'm working on a 3D program with DirectX. I was at the point of adding
collision detection. What I need is to know where all the game objects are
in space at any given time.
I was thinking of using a hashtable to store the objectID and it's
corresonding matrix. So for every frame I would update each object in the
hashtable.
With this info I would be able to cycle through the hashtable and see which object was closest and I would then be able to handle the collisions...

I need a global variable that I could access anywhere. newsgroups pointed to singletons... a simpler alternative would be great but i'm not sure it
exists.

I've been a programmer for a few years but C# and OO are still quite new to me.

http://kjo.myip.org/

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:ub****************@TK2MSFTNGP15.phx.gbl...
Singleton has nothing to do with static or instance methods on your
object.
A singleton can never have more than one instance at any one time. The
methods you define on the singleton does not matter in respect to that.
I must use static style calls while keeping data in memory thoughout my

app.

Why must you use static style calls? As shown, you can keep data in
memory
throughout the app as long as you keep a ref to the singleton somewhere
(i.e. form var, etc.)
To clarify, using only static style calls to the singleton, how would I
modify a hashtable?


Create the hashtable as a static var in static constructor. Then use
static
methods as needed if you have some need to only use static. Is this a
class
project?

--
William Stacey, MVP



Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2461
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
2
1674
by: davidw | last post by:
As I asked in last post, I want to put some logic in a object and all my webcontrol instance will access that object, the object is responsed to retrieve data from database if the data has not been...
15
63675
by: DBA | last post by:
Hi All, What is the diff. between a singleton class and a static class in C#?
5
3959
by: Bob | last post by:
Does anyone know of any adverse affects of using singleton data access objects in an ASP.NET Web Service? I've been forced to implement it this way but it just doesn't seem right to me. I haven't...
12
2432
by: solex | last post by:
Hello, I am trying to model a session object that is essentially a collection of different items (connection string, user name, maps etc.) I would like this session object to be available to...
12
8913
by: Preets | last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
13
2098
by: Robin Becker | last post by:
When young I was warned repeatedly by more knowledgeable folk that self modifying code was dangerous. Is the following idiom dangerous or unpythonic? def func(a): global func, data data =...
2
530
by: ben chang | last post by:
i'm hacking at some code originally written for VC++, trying to port to linux GCC 4. the linker returns multiple-definition errors with a singleton object, which i guess is not a problem in visual...
8
1511
by: tech | last post by:
Is it ok to allow another object to own a singleton object, or is this definitely a NO NO. I have a utility class that i want to provide access to a whole group of subobjects so i can make this...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.