0

I have this register that registers all the objects I need:

public static class ObjectRegister
{
    public static List<IObject> RegisteredObjects = new List<IObject>();
    static ObjectRegister()
    {
        RegisteredObjects.Add(new Object1());
        RegisteredObjects.Add(new Object2());
        RegisteredObjects.Add(new Object3());
    }
}

Next I have this function that checks a list and if the items in the list pass the test, it creates an object instance and adds it to the list:

public static List<IObject> Scan(List<parametar> list)
    {
        List<IObject> neededObjects = new List<IObject>();
        foreach (IObject registeredObject in ObjectRegister.RegisteredObjects)
        {
            foreach (parametar param in list)
            {
                if (registeredObject.Test(param)) //returns true or false 
                {
                    neededObjects.Add(registeredObject.CreateInstance(param));
                }
            }
        }
        return connectedObjects;
    }

Here is the CreateInstace method for Object1:

public IObject CreateInstance(parametar param)
    {
        return new Object1(param);
    }

And here is the constructor:

public Object1(parametar newParam)
    {
        this.param = newParam;
    }

It keeps trowing StackOverflow exception on this line:

this.param = newParam;

Tried all the possibilities for creating an instance, default constructor, empty object etc etc, but nothing worked... any ideas?

Thanx

EDIT: Code to the Object1 class:

public class Object1: IObject
{
    public parametar param
    {
        get { return this.param; }
        set { this.param = value; }
    }

    internal Object1() { }

    public Object1(parametar newParam)
    {
        this.param = newParam;
    }        


    public bool test(parametar param)
    {
        // I do the propper checking of the param here, and return the result
    }


    public IObject CreateInstance(parametar param)
    {
        return new Object1(param);
    }
} 
4
  • You need to show the code for your Object1 class. Commented Dec 25, 2011 at 17:35
  • 2
    Do you really have a class called parametar? Commented Dec 25, 2011 at 17:39
  • code public class Object1: IObject { public parametar param { get { return this.param; } set { this.param = value; } } internal Object1() { } public Object1(parametar newParam) { this.param = newParam; } public bool test(parametar param) { // I do the propper checking of the param here, and return the result } public IObject CreateInstance(parametar param) { return new Object1(param); } } code Commented Dec 25, 2011 at 17:42
  • @user1064633: Don't include the code in comments - put it in the question. Ideally, post a short but complete program demonstrating the problem. Commented Dec 25, 2011 at 17:43

1 Answer 1

9

This is your problem, in Object1:

public parametar param { get { return this.param; } set { this.param = value; }

That property calls itself recursively - which is exactly why you're getting a stack overflow. Don't do that. Instead, you probably either want an automatically-implemented property:

public parameter param { get; set; }

or use a private backing field:

private parametar param;
public parametar Param { get { return param; } set { param = value; }

Additionally, I'd strongly recommend that you start following .NET naming conventions, and pay attention to spelling in type and member names.

So you probably want your class to be called Parameter - although personally I'd at least try to make it a little bit more descriptive, e.g. QueryParameter or something similar. Likewise Object1 isn't exactly a semantically-meaningful name - I hope it's not the name in your real code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanx :) that was the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.