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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Creating a Class that can be a list?

Creating a Class that can be a list?

Scheduled Pinned Locked Moved C#
questioncomtutorial
9 Posts 6 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.
  • B Offline
    B Offline
    bbranded
    wrote on last edited by
    #1

    Hello, I'm a little lost as to how to write a class that will take in arguments from the .Add function of a List<>. With reference to this thread, I have written the following class.

     public class SnapShot
        {
            private DateTime m\_SnapShotDateTime;
    
            public DateTime SnapShotDateTime
            {
                get
                {
                    return m\_SnapShotDateTime;
                }
                set
                {
                    m\_SnapShotDateTime = value;
                }
    
            }
    
            private long m\_BPSAverage;
    
            public long BPSAverage
            {
                get
                {
                    return m\_BPSAverage;
                }
                set
                {
                    m\_BPSAverage = value;
                }
            }
    
            public SnapShot(DateTime snapshotdatetime, long bpsaverage)
            {
                m\_SnapShotDateTime = snapshotdatetime;
                m\_BPSAverage = bpsaverage;
            }
    
        }
    

    I have created a List and would like to use the .Add function to add "entries" for .SnapShot(DateTime snapshotdatetime, long bpsaverage).

          SnapShot LiveSnapshot;
          List LiveSnapshotList = new List();
    
          //LiveSnapshotList.Add();
    

    How do I properly write the class so that it handles the .Add function of the List? Or should I be calling .Add with some other overload value? Thanks, Matt

    L S realJSOPR 3 Replies Last reply
    0
    • B bbranded

      Hello, I'm a little lost as to how to write a class that will take in arguments from the .Add function of a List<>. With reference to this thread, I have written the following class.

       public class SnapShot
          {
              private DateTime m\_SnapShotDateTime;
      
              public DateTime SnapShotDateTime
              {
                  get
                  {
                      return m\_SnapShotDateTime;
                  }
                  set
                  {
                      m\_SnapShotDateTime = value;
                  }
      
              }
      
              private long m\_BPSAverage;
      
              public long BPSAverage
              {
                  get
                  {
                      return m\_BPSAverage;
                  }
                  set
                  {
                      m\_BPSAverage = value;
                  }
              }
      
              public SnapShot(DateTime snapshotdatetime, long bpsaverage)
              {
                  m\_SnapShotDateTime = snapshotdatetime;
                  m\_BPSAverage = bpsaverage;
              }
      
          }
      

      I have created a List and would like to use the .Add function to add "entries" for .SnapShot(DateTime snapshotdatetime, long bpsaverage).

            SnapShot LiveSnapshot;
            List LiveSnapshotList = new List();
      
            //LiveSnapshotList.Add();
      

      How do I properly write the class so that it handles the .Add function of the List? Or should I be calling .Add with some other overload value? Thanks, Matt

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, 1. you don't need to create a special list class, a simple:

      List<SnapShot> snapShots=new List<SnapShot>();
      SnapShot snap=new SnapShot(...);
      snapShots.Add(snap);
      ...
      List<SnapShot> moreSnapShots=new List<SnapShot>();
      moreSnapShots.Add(...);
      snapShots.Add(moreSnapShots);

      would work just fine. 2. And you could make that slightly more user-friendly with an almost empty class like this:

      class SnapShotList : List<SnapShot> {}

      SnapShotList snapShots=new SnapShotList();
      SnapShot snap=new SnapShot(...);
      snapShots.Add(snap);
      ...
      SnapShotList moreSnapShots=new SnapShotList();
      moreSnapShots.Add(...);
      snapShots.Add(moreSnapShots);

      :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Merry Christmas and a Happy New Year to all.


      B 1 Reply Last reply
      0
      • B bbranded

        Hello, I'm a little lost as to how to write a class that will take in arguments from the .Add function of a List<>. With reference to this thread, I have written the following class.

         public class SnapShot
            {
                private DateTime m\_SnapShotDateTime;
        
                public DateTime SnapShotDateTime
                {
                    get
                    {
                        return m\_SnapShotDateTime;
                    }
                    set
                    {
                        m\_SnapShotDateTime = value;
                    }
        
                }
        
                private long m\_BPSAverage;
        
                public long BPSAverage
                {
                    get
                    {
                        return m\_BPSAverage;
                    }
                    set
                    {
                        m\_BPSAverage = value;
                    }
                }
        
                public SnapShot(DateTime snapshotdatetime, long bpsaverage)
                {
                    m\_SnapShotDateTime = snapshotdatetime;
                    m\_BPSAverage = bpsaverage;
                }
        
            }
        

        I have created a List and would like to use the .Add function to add "entries" for .SnapShot(DateTime snapshotdatetime, long bpsaverage).

              SnapShot LiveSnapshot;
              List LiveSnapshotList = new List();
        
              //LiveSnapshotList.Add();
        

        How do I properly write the class so that it handles the .Add function of the List? Or should I be calling .Add with some other overload value? Thanks, Matt

        S Offline
        S Offline
        Saksida Bojan
        wrote on last edited by
        #3

        bbranded wrote:

        I'm a little lost as to how to write a class that will take in arguments from the .Add function of a List<>.

        If you use List, then do not handle .Add, because it is implemented. As your example you incorrectly defined List.

        List<SnapShot > LiveSnapshot = new List<SnapShot>();

        1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, 1. you don't need to create a special list class, a simple:

          List<SnapShot> snapShots=new List<SnapShot>();
          SnapShot snap=new SnapShot(...);
          snapShots.Add(snap);
          ...
          List<SnapShot> moreSnapShots=new List<SnapShot>();
          moreSnapShots.Add(...);
          snapShots.Add(moreSnapShots);

          would work just fine. 2. And you could make that slightly more user-friendly with an almost empty class like this:

          class SnapShotList : List<SnapShot> {}

          SnapShotList snapShots=new SnapShotList();
          SnapShot snap=new SnapShot(...);
          snapShots.Add(snap);
          ...
          SnapShotList moreSnapShots=new SnapShotList();
          moreSnapShots.Add(...);
          snapShots.Add(moreSnapShots);

          :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Merry Christmas and a Happy New Year to all.


          B Offline
          B Offline
          bbranded
          wrote on last edited by
          #4

          Thanks very much Luc. I will take a look into this. As for snap, this is of the type SnapShot as defined via the SnapShot class's SnapShot function? Thanks, Matt

          L 1 Reply Last reply
          0
          • B bbranded

            Thanks very much Luc. I will take a look into this. As for snap, this is of the type SnapShot as defined via the SnapShot class's SnapShot function? Thanks, Matt

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            bbranded wrote:

            as defined via the SnapShot class's SnapShot function?

            yes. however functions are passé, you should call them methods now; unless they are constructors! :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            Merry Christmas and a Happy New Year to all.


            D L 2 Replies Last reply
            0
            • B bbranded

              Hello, I'm a little lost as to how to write a class that will take in arguments from the .Add function of a List<>. With reference to this thread, I have written the following class.

               public class SnapShot
                  {
                      private DateTime m\_SnapShotDateTime;
              
                      public DateTime SnapShotDateTime
                      {
                          get
                          {
                              return m\_SnapShotDateTime;
                          }
                          set
                          {
                              m\_SnapShotDateTime = value;
                          }
              
                      }
              
                      private long m\_BPSAverage;
              
                      public long BPSAverage
                      {
                          get
                          {
                              return m\_BPSAverage;
                          }
                          set
                          {
                              m\_BPSAverage = value;
                          }
                      }
              
                      public SnapShot(DateTime snapshotdatetime, long bpsaverage)
                      {
                          m\_SnapShotDateTime = snapshotdatetime;
                          m\_BPSAverage = bpsaverage;
                      }
              
                  }
              

              I have created a List and would like to use the .Add function to add "entries" for .SnapShot(DateTime snapshotdatetime, long bpsaverage).

                    SnapShot LiveSnapshot;
                    List LiveSnapshotList = new List();
              
                    //LiveSnapshotList.Add();
              

              How do I properly write the class so that it handles the .Add function of the List? Or should I be calling .Add with some other overload value? Thanks, Matt

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              public class SnapShotList : List {}

              Then you can do this:

              SnapShotList snapshots = new SnapShotList();

              As a result, you get all of the benefits of having a list (including all of the methods therein) in a conveniently named object, and you can add specific custom behavior to your class to handle circumstances ragarding the items in the list. I do it all the time.

              .45 ACP - because shooting twice is just silly
              -----
              "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." - J. Jystad, 2001

              1 Reply Last reply
              0
              • L Luc Pattyn

                bbranded wrote:

                as defined via the SnapShot class's SnapShot function?

                yes. however functions are passé, you should call them methods now; unless they are constructors! :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                Merry Christmas and a Happy New Year to all.


                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                :laugh: 5ed :thumbsup:

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Why are you using VB6? Do you hate yourself? (Christian Graus)

                1 Reply Last reply
                0
                • L Luc Pattyn

                  bbranded wrote:

                  as defined via the SnapShot class's SnapShot function?

                  yes. however functions are passé, you should call them methods now; unless they are constructors! :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  Merry Christmas and a Happy New Year to all.


                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Haha! What do you call a Lambda Then Luc?

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    Haha! What do you call a Lambda Then Luc?

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    I'm afraid I can't answer that on a public forum. :-O

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    Merry Christmas and a Happy New Year to all.


                    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