Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I have a class as in the following:
C#
public class activityDetail
{
  public string a { get; set; }
  public string b { get; set; }
}

then what I want to do is define a singlton class in my Cache.cs as in the following:
C#
public sealed class Cache
{
private static  readonly Cache cacheInstance = new Cache();
.
.
.
.
public activityDetail instanceofActivitydetail { get { return instanceofActivitydetail; } }

Now, in my controller I want to reference this instanceofActivitydetail, if I do it the following way I get an exception (some type of stack overflow):
C#
Cache.CacheInstance.instanceofActivitydetail.a = some value; 
Cache.CacheInstance.instanceofActivitydetail.b = some other value;

// "some value" is just for ease it actually have a designated known value but it is not easy to share it here.
// so when the compiler eaches this line it throws the error mentioned above, I believe my defninition in the Cache class is missing something, or my call shown above from the controller is missing something.
Posted
Updated 6-Aug-12 14:34pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Aug-12 20:04pm    
Not enough information to find the bug -- it is somewhere else. You can do it yourself -- please see my answer.
--SA

This exception is one of the easiest to nail down and fix. Almost always, it is related to unfinished recursion or mutual recursion:
http://en.wikipedia.org/wiki/Recursion[^],
http://en.wikipedia.org/wiki/Mutual_recursion[^].

Use the debugger, put a breakpoint where the exception is thrown. You will observe that the code is repeated many times in execution. To see where a call comes from, use "Call stack" debugger window. When you see what the calling code is repeated, move your breakpoint there, until you see the root of the problem.

—SA
 
Share this answer
 
v2
Comments
Member 8714829 7-Aug-12 12:28pm    
Thanks Sergey for your feedback, it looks like it is having an issue with the Cached value/properties of the class, the problem is somewhere within:

public activityDetail instanceofActivitydetail { get { return instanceofActivitydetail; } }

// so it could how I am handling the Cach or the class it is returning instanceofActivitydetail... (I know it is not clear still but I am still troubleshooting the issue.
Sergey Alexandrovich Kryukov 7-Aug-12 14:56pm    
Just the opposite -- this is 100% clear: you go into the infinite recursion right away after first call to it; this is a call made by a getter to itself. Replace it and be fine. And please accept my answer formally (green button), as the problem is nailed down.
The simplest solution will be:

public activityDetail instanceofActivitydetail { get; } // nothing else

If you still did not get it, read a chapter explaining properties. And still accept the answer -- it will solve the problem, unless you have the same bug in other places...
--SA
Member 8714829 7-Aug-12 16:26pm    
Thanks Sergey! that worked, I only had to add a set property as well and instantiate a class before using it.
Sergey Alexandrovich Kryukov 7-Aug-12 17:05pm    
Great! No doubt you would do it.
Now, please accept the answer formally (green button) -- thanks.
--SA
public activityDetail instanceofActivitydetail { get; set;}

and placed the following in the controller (before using the Cache class/property):

Cache.CacheInstance.instanceofActivitydetail = new activityDetai();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900