Modifying singleton data.

V

VidalSasoon

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];
}
}

}
 
W

William Stacey [MVP]

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();
}
}
}
 
V

VidalSasoon

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 said:
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 said:
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];
}
}

}
 
W

William Stacey [MVP]

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?
 
M

Michael Voss

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
 
V

VidalSasoon

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/
 
W

William Stacey [MVP]

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 said:
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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top