Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Generics class

Generics class

Scheduled Pinned Locked Moved C#
data-structureshelptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abedo1982
    wrote on last edited by
    #1

    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,

    N 1 Reply Last reply
    0
    • A abedo1982

      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,

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • N Not Active

        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

        A Offline
        A Offline
        abedo1982
        wrote on last edited by
        #3

        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}

        N 1 Reply Last reply
        0
        • A abedo1982

          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}

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups