I have question connected with interfaces and abstract classes.
I’ll give to you simple example, that could explain what I want to do. So, Lets start.
public interface A
{
string param1 { set; get;}
string param1 { set; get;}
A CreateObject(string p1,string p2);
}
public class MyClass1 : A
{
public string param1 { set; get; }
public string param2 { set; get; }
public A CreateObject(string p1,string p2)
{
var obj = new MyClass1();
obj.param1 = p1;
obj.param2 = p2;
return obj;
}
}
public class MyClass2 : A
{
public string param1 { set; get; }
public string param2 { set; get; }
public A CreateObject(string p1,string p2)
{
var obj = new MyClass2();
obj.param1 = p1;
obj.param2 = p2;
return obj;
}
}
// I have little problem with this function
public List<A> GetNodes(int count)
{
var lst_Objects = new List<a>();
for(int i=0; i<count; i++)
{
string Param1 = GetParam1();
string Param2 = GetParam2();
lst_Objects.Add(new A.CreateObject(Param1,Param2); // but it defenitly doesn't work(wrong way)
}
return lst_Objects;
}
I have problems with GetNodes function.
Tip:
MyClass1 and MyClass2 is Entity objects, and because of this reason I can not create abstract class, and use some generic to resolve this problem.
I will grateful for your ideas
You didn’t mention A.CreateObject as static while other concrete class MyClass1 / MyClass2 are left as static. Use
lst_Objects.Add(MyClass1.CreateObject(Param1,Param2);
or
lst_Objects.Add(MyClass2.CreateObject(Param1,Param2);
instead.
Also to mention, you need to make sure you define A.CreateObject in both of the classes, otherwise you need to make both of them abstract which is not what you want. Rather remove the nonstatic method CreateObject from interface A.