tags:

views:

39

answers:

3

Hello! 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

A: 

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.

abhishek
Ok. I had idea. If I create some universal class like: public class UniversalClass : A{ public string param1 { set; get; } public string param2 { set; get; } public static A CreateObject(string p1,string p2) { var obj = new MyClass1(); obj.param1 = p1; obj.param2 = p2; return obj; } Then I'll use this class for GetNodes() function. I'll create object of Universal class. Then I'll convert It to the myclass1 or myclass2 object. Is it good idea?
Leonid
Why do you need the static CreateObject? Do you want to create a SingleTon ? Yes you can probably do, but I would recommend to use an UniversalClass and don't put any inheritance there, rather write your static CreateObject only to create classes based on your concrete types like MyClass1 / MyClass2
abhishek
Also you must note static member is not allowed inside an interface, if you like to have a static method on a type, use Extension method for that.
abhishek
A: 

Your interface declares a CreateObject method, which neither of your classes implement. Your classes implement a static CreateObject, which does not satisify the interface. Interfaces cannot declare static members.

x0n
I know it. But question is "How to create object, that would have type A, and then convert this object into type what I want. In this state I don't know what object i had.
Leonid
A: 

I have bad solution. I'll create class like this:

public class Universal: A
{
 public string param1 { set; get; }
 public string param2 { set; get; }
 public static A CreateObject(string p1,string p2)
 {
  var obj = new MyClass1();
  obj.param1 = p1;
  obj.param2 = p2;
  return obj;
 }

Function GetNodes will have appearance like this:

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(Universal.CreateObject(Param1 ,Param2)); 
                }
             return lst_Objects;
}

This function will return List of objects which have type A(which I easily convert into myclass1 or myclass2 object.

Is it good Idea?

Thank for your attention.

Leonid