Design a Class
-
Can anyone suggest me how can I design a class of follwing kind. suppose i have 3 classes like Class A, Class B, Class C. All these classes have same method like returning collection of record of their own type. e.g. collection GetAllRecord() { returning collection of A; } Now I want to a common method in different class for calling above method e.g Class D { Getall(A) { A.GetAllRecord(); } } How could i design this method to take agument of different type A B C...?
-
Can anyone suggest me how can I design a class of follwing kind. suppose i have 3 classes like Class A, Class B, Class C. All these classes have same method like returning collection of record of their own type. e.g. collection GetAllRecord() { returning collection of A; } Now I want to a common method in different class for calling above method e.g Class D { Getall(A) { A.GetAllRecord(); } } How could i design this method to take agument of different type A B C...?
Use an interface. Simple example below.
public interface IMyInterface
{
void WriteMe();
}
public class A : IMyInterface
{
public void WriteMe()
{
Console.Write("A");
}
}
public class B : IMyInterface
{
public void WriteMe()
{
Console.Write("B");
}
}
public class C : IMyInterface
{
public void WriteMe()
{
Console.Write("C");
}
}
public class D
{
public void WriteParameter(IMyInterface test)
{
test.WriteMe();
}
}Then you can do this
D d = new D();
d.WriteParameter(new A());
d.WriteParameter(new B());
// ...Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Can anyone suggest me how can I design a class of follwing kind. suppose i have 3 classes like Class A, Class B, Class C. All these classes have same method like returning collection of record of their own type. e.g. collection GetAllRecord() { returning collection of A; } Now I want to a common method in different class for calling above method e.g Class D { Getall(A) { A.GetAllRecord(); } } How could i design this method to take agument of different type A B C...?
If I understand what you want (and I probably don't), what you want to do is called oveloading.
public class A
{
private List<a> list = new List<a>();
public List<a> GetAllRecords() { get { return list; } }
}
public class B
{
private List<b> list = new List<b>();
public List<b> GetAllRecords() { get { return list; } }
}
public class C
{
private List<c> list = new List<c>();
public List<c> GetAllRecords() { get { return list; } }
}public class D
{
private A a = new A();
private B b = new B();
private C c = new C();private List<a> aList; private List<b> bList; private List<c> cList; public D() { } private List<a> GetAllRecords(A a) { aList = a.GetAllRecords; } private List<b> GetAllRecords(B b) { bList = b.GetAllRecords; } private List<c> GetAllRecords(C c) { cList = c.GetAllRecords; }
}
[EDIT] Forgot to convert the pointy brackets...
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001modified on Saturday, October 11, 2008 5:08 AM
-
Use an interface. Simple example below.
public interface IMyInterface
{
void WriteMe();
}
public class A : IMyInterface
{
public void WriteMe()
{
Console.Write("A");
}
}
public class B : IMyInterface
{
public void WriteMe()
{
Console.Write("B");
}
}
public class C : IMyInterface
{
public void WriteMe()
{
Console.Write("C");
}
}
public class D
{
public void WriteParameter(IMyInterface test)
{
test.WriteMe();
}
}Then you can do this
D d = new D();
d.WriteParameter(new A());
d.WriteParameter(new B());
// ...Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)