Generics class
-
Hi All I'm Create a Generics class and i need to write a function return lis of object stored on it but i have a problem as following: I want to compare if the Generics type is the same of the created class and if its the same i want to use a same method and if not the same type of the created class return the object value . example: class myList { Array MyArray; public myList() { _MyArray=new Array(); } public void add(T value) { _MyArray.add(value); } public object[] List { //here is the problem //Array temp=new Array(); //if T is myList<> //{ // foreach(T item in _MaArray) // { // temp.addRange(item .list);//use list method // } //} //else //{ // foreach(T item in _MaArray) // { // temp.addRange((object)item); // } //} } } { myList AList=new myList(); myList BList=new myList(); BList.add(1); BList.add(2); BList.add(3); AList.add(BList); //BList.list === {1,2,3} BList=new myList(); BList.add(4); BList.add(5); BList.add(6); AList.add(BList); //BList.list === {4,5,6} //AList.list === {1,2,3,4,5,6} Note:I know there is an exist class List but This is just an example i need to know how can i do the list function that's it thank you,
-
Hi All I'm Create a Generics class and i need to write a function return lis of object stored on it but i have a problem as following: I want to compare if the Generics type is the same of the created class and if its the same i want to use a same method and if not the same type of the created class return the object value . example: class myList { Array MyArray; public myList() { _MyArray=new Array(); } public void add(T value) { _MyArray.add(value); } public object[] List { //here is the problem //Array temp=new Array(); //if T is myList<> //{ // foreach(T item in _MaArray) // { // temp.addRange(item .list);//use list method // } //} //else //{ // foreach(T item in _MaArray) // { // temp.addRange((object)item); // } //} } } { myList AList=new myList(); myList BList=new myList(); BList.add(1); BList.add(2); BList.add(3); AList.add(BList); //BList.list === {1,2,3} BList=new myList(); BList.add(4); BList.add(5); BList.add(6); AList.add(BList); //BList.list === {4,5,6} //AList.list === {1,2,3,4,5,6} Note:I know there is an exist class List but This is just an example i need to know how can i do the list function that's it thank you,
It doesn't appear you have an understanding of what a generic class is. Try reading though these articles. http://www.codeproject.com/csharp/generics.asp[^] http://www.codeproject.com/csharp/treecollection2.asp[^]
only two letters away from being an asset
-
It doesn't appear you have an understanding of what a generic class is. Try reading though these articles. http://www.codeproject.com/csharp/generics.asp[^] http://www.codeproject.com/csharp/treecollection2.asp[^]
only two letters away from being an asset
thank you Mark this is the correct code but I need just how can i call a function in this example list for the generics T(((T)item) .list) or use it like the class( myList) plz reply me as soon as possible //-------------------------- myList ///.list =={1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90} [0]myList>///.list =={1,2,3,4,5,6,7,8,9} [0]=myList;///.list =={1,2,3} [0]=1; [1]=2; [2]=3; [1]=myList;///.list =={4,5,6} [0]=4; [1]=5; [2]=6; [2]=myList;///.list =={7,8,9} [0]=7; [1]=8; [2]=9; [1]myList>///.list =={10,20,30,40,50,60,70,80,90} [0]=myList;///.list =={10,20,30} [0]=10; [1]=20; [2]=30; [1]=myList;///.list =={40,50,60} [0]=40; [1]=50; [2]=60; [2]=myList;///.list =={40,50,60} [0]=70; [1]=80; [2]=90; //--------------------------- thank you in advance, example: class myList { Array MyArray; public myList() { _MyArray=new Array(); } public void add(T value) { _MyArray.add(value); } public object[] List { //here is the problem //Array temp=new Array(); //if T is myList<> //{ // foreach(T item in _MaArray) // { // temp.addRange(((T)item) .list);//use list method // } //} //else //{ // foreach(T item in _MaArray) // { // temp.addRange((object)item); // } //} } } { myList> AList=new myList>(); myList BList=new myList(); BList.add(1); BList.add(2); BList.add(3); AList.add(BList); //BList.list === {1,2,3} BList=new myList(); BList.add(4); BList.add(5); BList.add(6); AList.add(BList); //BList.list === {4,5,6} //AList.list === {1,2,3,4,5,6}
-
thank you Mark this is the correct code but I need just how can i call a function in this example list for the generics T(((T)item) .list) or use it like the class( myList) plz reply me as soon as possible //-------------------------- myList ///.list =={1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90} [0]myList>///.list =={1,2,3,4,5,6,7,8,9} [0]=myList;///.list =={1,2,3} [0]=1; [1]=2; [2]=3; [1]=myList;///.list =={4,5,6} [0]=4; [1]=5; [2]=6; [2]=myList;///.list =={7,8,9} [0]=7; [1]=8; [2]=9; [1]myList>///.list =={10,20,30,40,50,60,70,80,90} [0]=myList;///.list =={10,20,30} [0]=10; [1]=20; [2]=30; [1]=myList;///.list =={40,50,60} [0]=40; [1]=50; [2]=60; [2]=myList;///.list =={40,50,60} [0]=70; [1]=80; [2]=90; //--------------------------- thank you in advance, example: class myList { Array MyArray; public myList() { _MyArray=new Array(); } public void add(T value) { _MyArray.add(value); } public object[] List { //here is the problem //Array temp=new Array(); //if T is myList<> //{ // foreach(T item in _MaArray) // { // temp.addRange(((T)item) .list);//use list method // } //} //else //{ // foreach(T item in _MaArray) // { // temp.addRange((object)item); // } //} } } { myList> AList=new myList>(); myList BList=new myList(); BList.add(1); BList.add(2); BList.add(3); AList.add(BList); //BList.list === {1,2,3} BList=new myList(); BList.add(4); BList.add(5); BList.add(6); AList.add(BList); //BList.list === {4,5,6} //AList.list === {1,2,3,4,5,6}
No, a generic class would be something like this*
public class MyList<T> : IEnumerable { private List<T> m_InternalList; }
As in the example, now you have a Generic class that can support a collection of objects in a type-safe manner and gives the ability to iterate through that collection. *disclaimer: Not 100% complete and acccurate code, just psuedo code so don't complain;P -- modified at 18:03 Sunday 27th May, 2007
only two letters away from being an asset