1

I try to write this class:

public class ModelManager
{
    public OmniacareHomeProductionEntities _db;

    public CategoriaManager categoriaManager 
    { 
        get { return categoriaManager; }
        set 
        {
            if (categoriaManager == null)
            {
            categoriaManager = new CategoriaManagerImpl();
            }
        }
    }

    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ModelManager));
    public ModelManager()
    {
        _db = new OmniacareHomeProductionEntities();
    }
}

CategoriaManager is an Interface and CategoriaManagerImpl is a class that implements CategoriaManager.

I use ModelManager class in this mode:

ModelManager modelManager = new ModelManager();
modelManager.categoriaManager.saveLocalCategory(category, true);

so when I try to run this code, I have a StackOverflowError at this line

get 
{
    return categoriaManager;
}

Where is my error? Can you help me?

L-Four
  • 11,965
  • 8
  • 52
  • 103
bircastri
  • 2,707
  • 7
  • 38
  • 96
  • You are returning the property itself, so it gets stuck in in infinite loop. Use a backing variable to get and set the property. – John Willemse Jul 01 '14 at 10:02
  • possible duplicate of [How Does A Stack Overflow Occur and How Do You Prevent It?](http://stackoverflow.com/questions/26158/how-does-a-stack-overflow-occur-and-how-do-you-prevent-it) – Nick Udell Jul 01 '14 at 10:03
  • For this type of work and if you are using .NET 4.0 or higher, use Lazy or LazyInitializer related API – Sheen Jul 01 '14 at 10:18

2 Answers2

5

Your problem is clearly here

  public CategoriaManager categoriaManager 
    { 
        get 
        {
            return categoriaManager;
        }
        set 
        {
            if (categoriaManager == null)
            {
                categoriaManager = new CategoriaManagerImpl(); //HERE !!!!!!!!!
            }
        }
    }

the name of the member is the same of the property, change it like this, for example:

    public CategoriaManager CatManager //NAME OF HE PROPERTY ISCHANGED !!!!
    { 
        get 
        {
            return categoriaManager;
        }
        set 
        {
            if (categoriaManager == null)
            {
                categoriaManager = new CategoriaManagerImpl();
            }
        }
    }

General guideline:

  • for properties use names starting from upper case
  • for fields use names starting from low case

So your code would lool like this:

ModelManager modelManager = new ModelManager();
modelManager.CatManager.saveLocalCategory(category, true);
Tigran
  • 59,345
  • 8
  • 77
  • 117
  • I try to change my code but I have an error on return categoriaManager; ecc ecc. The error is: The name 'categoriaManager' not exist in the current context – bircastri Jul 01 '14 at 10:13
  • I have add this property and it found private CategoriaManager categoriaManager; – bircastri Jul 01 '14 at 10:17
  • @user2405663: because (if did as the code) now the name of the property is CatManager. Change the code to that name. – Tigran Jul 01 '14 at 10:17
3

You should change it like this:

private CategoriaManager _categoriaManager;

public CategoriaManager CategoriaManager 
{ 
    get { return _categoriaManager; }
    set 
    {
        if (_categoriaManager == null)
        {
            _categoriaManager = new CategoriaManagerImpl();
        }
    }
}
RickL
  • 2,741
  • 3
  • 20
  • 35