|
You need to pre-define the type for the generic List class to the type of the object that will be populating it i.e. iPerson so assuming classes Person, Contract_Person and Vendor_Person are all derived from iPerson your code needs to change to the following:
public static List<iPerson> TGeneratePersonList(IDataReader oDR, ePersonType PersonType)
{
...
iPerson oP = null;
List<iPerson> oPList = new List<iPerson>();
...
}
and your property to:
public List<iPerson> Personnel_List
{
...
}
and the field _Personnel_List to:
private List<iPerson> _Personnel_List;
Incidentally, the TGeneratePersonList() is not a generic method - it is an ordinary method that returns a generic List of type iPerson.
This is a generic method:
public void GenericMethod<T>() // Where type T is defined when the method is called
{
// Do something with T in here
}
example calls
GenericMethod<int>();
GenericMethod<string>();
GenericMethod<MyClass>();
|
|
|
|
|
|
Maybe I am missing the point somewhere but I cannot see why you need to use generics to do this.
In my previous example by placing all the initialisation properties into iPerson for example would enable you to initialise all classes derived from iPerson i.e.
// Interface that defines properties and methods to be called during initilaisation
public interface I
{
string Prop1 { get; set; }
int Prop2 { get; set; }
void Initialise(params object[] arrParams);
}
// Classes to be initialised
class A: I
{
private string _prop1 = null;
private int _prop2;
// Interface implementations
string I.Prop1
{
get { return _prop1; }
set { _prop1 = value; }
}
int I.Prop2
{
get { return _prop2; }
set { _prop2 = value; }
}
void I.Initialise(params object[] arrParams)
{
// Do some initialisation on params
Initialise(arrParams);
}
// Option for in class initialisation - could use virtual.
protected abstract void Initialise(params object[] arrParams);
}
class B: A
{
protected override void Initialise(params object[] arrParams)
{
}
}
class C: B
{
private string Prop3 = null;
protected override void Initialise(params object[] arrParams)
{
}
}
Now using interface I to initialise instances of A, B or C as follows:
//Create list
List<I> items = new List<I>();
// This could equally be:
// ArrayList items = new ArrayList();
....
if (ClassType == eType.A)
{
items.Add(new A());
}
else if (ClassType == eType.B)
{
items.Add(new B());
}
else if (ClassType == eType.C)
{
items.Add(new C());
}
// Initialisation can be performed on object instances of type A, B and C this way using interface I no casting required.
foreach (I item in items)
{
// Initialise object properties
item.Prop1 = source.P1;
item.Prop2 = source.P2;
//Or even call an initialise method on the object itself.
item.Initialise(source.P1, source.P2, "P3");
}
This example relies on a single interface structure defined at compile time, this is also a requirement that applies to generics - if what you are looking for is to try and initialise objects by examining their structures at run-time then this falls into the realms of Reflection - and only do this if for some reason you really need to.
If you are requiring to initialise properties that apply to the derived classes (i.e class C)
you could used the Initialise() method shown but you would have to either apply strict parameter ordering or employ a mechanism for identifying which parameter goes to which property.
|
|
|
|
|
Sorry man, you went out to left field and away from the topic at hand. Thanks for your help anyway.
|
|
|
|
|
Ok, I apologize again. Just having a frustrating day. I think I follow what you were trying to explain. I've also watched the webcast from Juval Lowy. What I was trying to explain in my previous post is that I got my function (not a method) to return a generic list of an interface that all my classes implement. What I don't quite understand is how I use this list in the referencing call if I'm expecting a List of of a specific type that implements that interface. Basically it looks to me like I have to iterate throuh the list and cast each object to my desired type in order to get the intelisense etc. Are you saying that I should have my function call look like this based on my last example? If so this won't compile because I get the following error on each result.Add()
Error 1 The best overloaded method match for 'System.Collections.Generic.List<t>.Add(T)' has some invalid arguments C:\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 95 17 ConsoleApplication1
Error 2 Argument '1': cannot convert from 'ConsoleApplication1.A' to 'T' C:\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 95 28 ConsoleApplication1
public static List<t> GetList<t>() where T : A,iClass
{
List<t> result = new List<t>();
iClass oC = null;
if (typeof(T) == typeof(A))
{
oC = new A();
((A)oC).Full_Name = "A_Tom";
result.Add((A)oC);
oC = new A();
((A)oC).Full_Name = "A_Bill";
result.Add((A)oC);
oC = new A();
((A)oC).Full_Name = "A_Jack";
result.Add((A)oC);
}
else if (typeof(T) == typeof(B))
{
oC = new B();
((B)oC).Full_Name = "b_Tom";
result.Add((B)oC);
oC = new B();
((B)oC).Full_Name = "B_Bill";
result.Add((B)oC);
oC = new B();
((B)oC).Full_Name = "B_Jack";
result.Add((B)oC);
}
return result;
}
|
|
|
|
|
I got it , I got it, I got it!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
f
public static List<t> GetList<t>() where T :B, iClass, new()
{
T obj = new T();
List<t> result = new List<t>();
if (typeof(T) == typeof(A))
{
((A)obj).PhoneA = "444-4444";
result.Add(obj);
}
else if (typeof(T) == typeof(B))
{
((B)obj).SSN = "123-12-1111";
result.Add(obj);
}
return result;
}
|
|
|
|
|
You are still having to cast classes A and B though????
|
|
|
|
|
Here's my thing that I don't understand about what you presented earlier, you say to use the Initialise method of the Interface on each individual Derrived class to initilise that instance. My problem with this sample is that you're using an array of type object[] which in itself will do implicit cast on each item thus degrading performance. Secondly you're example of
//Or even call an initialise method on the object itself.
item.Initialise(source.P1, source.P2, "P3");
}
tells me that you will need to write an Initialisation override on each generation of the object, in this case to be able to handle "P3". I guess if it's a virtual you could call base.Initialise and pass it the same params. WOuld that be way to go?
|
|
|
|
|
I got it, I got it, I got it.........Again! I found an elegant solution I think. A light bulb is starting shine!!!! Tell what you think about this solution. THis will allow me to use a static function. WOuld it be better to use an instance virtual function and have each derrived instance call the base.function instead?:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
B objB = new B();
List<iclass> oBList = new List<iclass>();
List oList = A.GetList();
}
}
public interface iClass
{
string Full_Name
{
get;
set;
}
string Short_Name
{
get;
set;
}
}
public class A : iClass
{
public A()
{
}
private string _PhoneA = "";
private string _Full_Name = "";
private string _Short_Name = "";
public string PhoneA
{
get { return _PhoneA; }
set { _PhoneA = value; }
}
public static List<t> GetList<t>() where T : iClass, new()
{
T obj = new T();
List<t> result = new List<t>();
obj.Full_Name = "Susan";
obj.Short_Name = "Su";
if (typeof(T) == typeof(A))
{
(obj as A).PhoneA = "444-4444";
result.Add(obj);
}
else if (typeof(T) == typeof(B))
{
(obj as B).SSN = "123-12-1111";
result.Add(obj);
}
return result;
}
public string Full_Name
{
get { return _Full_Name; }
set { _Full_Name = value; }
}
public string Short_Name
{
get { return _Short_Name; }
set { _Short_Name = value; }
}
}
public class B : A, iClass
{
private string _SSN = "";
public string SSN
{
get { return _SSN; }
set { _SSN = value; }
}
}
}
|
|
|
|
|
|
|
I am glad to have helped although you did most of it yourself.
Andy
|
|
|
|
|
I have following error in window service same code is working in window Application
"QueryInterface for interface SQLXMLBULKLOADLib.ISQLXMLBulkLoad failed."
Dim objbl As SQLXMLBulkLoad3
objbl = New SQLXMLBulkLoad3
objbl.ConnectionString = "Provider=SQLOLEDB.1;Data Source=pcDB;Initial Catalog=webdb;User ID=sa;pwd=sa"
objbl.ErrorLogFile = "c:\error.log"
objbl.BulkLoad = True
objbl.KeepIdentity = False
objbl.CheckConstraints = False
objbl.Transaction = False
objbl.Execute("c:\Items.XSD", "C:\Items.xml")
Thanks
Hardik Panchal
|
|
|
|
|
Don't cross post in multiple forums. It's considered very rude to "spam" the forums and makes it quite difficult for multiple people to collaborate on an answer.
|
|
|
|
|
hi all,
installation failed when i tried to install .net 2005 on windows 2003 server with sp1
Any one please help me
thanks and regards
sumanth
this is sumanth, completed m.c.a from india.
|
|
|
|
|
Would you care to tell us what the error message is?? Or should we just guess at the 1,000 or so possible causes for failure...
|
|
|
|
|
Dave Kreskowiak wrote: Or should we just guess at the 1,000 or so possible causes for failure...
I suspect that it's a PEBKAC failure.
|
|
|
|
|
Pete O`Hanlon wrote: I suspect that it's a PEBKAC failure.
I had to Google that one! I normally call it a short between the keyboard and ground.
|
|
|
|
|
Dave Kreskowiak wrote: I normally call it a short between the keyboard and ground
I like that.
|
|
|
|
|
I normally use EBKAC or an ID10T error, but that one only really works when you say it (ID ten T).
|
|
|
|
|
Hi,
I am having the handle to a combo box's dropdown list control which i have grabed by subclassing and overriding the "WNDProc" to receive window message. is there any way by which i can know what is the real object behind this handle.So if it is list control can i assign it to a .Net list control and start using that list control in .Net. i tried control.FromHandle.. Listbox.Fromhandle and and some methods inside the marshal class in .Net
so can you please help me getting the real control behind this handle
|
|
|
|
|
It doesn't work that way. In most cases, you cannot simply slap a .NET controls over a window's handle and treat that handle like a control. Not every control has an unmanaged control sitting behind it.
|
|
|
|
|
First of all Thanks very much for your reply..
and about the scenario.....
Hmmm .. that i know.. in my scenario i am having the handle to the dropdown area (control) of a combobox.
so i want to know whether we can assign/create a list control from this handle. so that i can treat it is a list control and add a control in it or do whatever accoding to the project requirement
|
|
|
|
|
I am having a hard time understanding how default system fonts are obtained in .NET. Specifically, I need to internationalize an app, and I need to be able to specify the font for various controls. What I want is something like the the old GDI call GetStockObject to return GUI font, system font, fixed width font etc. Can someone point me in the right direction?
|
|
|
|
|
Hi
I have some processing in a thread and want to fire some events into the GUI-thread. Kind of like BackgroundWorker.ReportProgress except that I will not be using the BackgroundWorker, but my own thread .
Anyone got any idea how to add some execution to the GUI-thread's event queue?
|
|
|
|