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);
}
}
This is your problem, in Object1:
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:
or use a private backing field:
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.QueryParameteror something similar. LikewiseObject1isn’t exactly a semantically-meaningful name – I hope it’s not the name in your real code.