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. How can I use a MemoryStream in a System.Generic.List<>

How can I use a MemoryStream in a System.Generic.List<>

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 3 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.
  • M Offline
    M Offline
    mbudak
    wrote on last edited by
    #1

    I've object and it has a lot of objects :-\ When I access Childobjects everything is ok but not MemoryStreams;

    public class myObject
    {
    public List<ChildObject> subObject = new List<ChildObject> { };

        public string ObjectName { get; set; }
    
        public class ChildObject
        {
            public string ChildObjectName { get; set; }
            public MemoryStream ChildStream { get; set; }
        }
    }
    

    When I use this object I am using following code;

    // This is OK
    MemoryStream myMS = new MemoryStream();
    myMS.Position = 0;
    // End of This is OK
    //
    myObject MyObj = new myObject();
    MyObj.ObjectName = "My First Object";
    MyObj.subObject.Add(new myObject.ChildObject());
    MyObj.subObject[0].ChildObjectName = "My First Sub Object";
    MyObj.subObject[0].ChildStream.Position = 0; // :mad:ERROR LINE

    Thanks for your reply. :rolleyes:

    J 1 Reply Last reply
    0
    • M mbudak

      I've object and it has a lot of objects :-\ When I access Childobjects everything is ok but not MemoryStreams;

      public class myObject
      {
      public List<ChildObject> subObject = new List<ChildObject> { };

          public string ObjectName { get; set; }
      
          public class ChildObject
          {
              public string ChildObjectName { get; set; }
              public MemoryStream ChildStream { get; set; }
          }
      }
      

      When I use this object I am using following code;

      // This is OK
      MemoryStream myMS = new MemoryStream();
      myMS.Position = 0;
      // End of This is OK
      //
      myObject MyObj = new myObject();
      MyObj.ObjectName = "My First Object";
      MyObj.subObject.Add(new myObject.ChildObject());
      MyObj.subObject[0].ChildObjectName = "My First Sub Object";
      MyObj.subObject[0].ChildStream.Position = 0; // :mad:ERROR LINE

      Thanks for your reply. :rolleyes:

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      :rolleyes: Indeed - you've not provided the error message you're getting. Hold on though - I'll use my psychic powers..... "Object reference not set to an instance of an object" ChildStream has not been instantiated.

      MyObj.subObject[0].ChildObjectName = "My First Sub Object";
      MyObj.subObject[0].ChildStream = new MemoryStream() // New Line
      MyObj.subObject[0].ChildStream.Position = 0; // (NoLongerAn) ERROR LINE

      M 1 Reply Last reply
      0
      • J J4amieC

        :rolleyes: Indeed - you've not provided the error message you're getting. Hold on though - I'll use my psychic powers..... "Object reference not set to an instance of an object" ChildStream has not been instantiated.

        MyObj.subObject[0].ChildObjectName = "My First Sub Object";
        MyObj.subObject[0].ChildStream = new MemoryStream() // New Line
        MyObj.subObject[0].ChildStream.Position = 0; // (NoLongerAn) ERROR LINE

        M Offline
        M Offline
        mbudak
        wrote on last edited by
        #3

        Impressive ;) Is it possible to auto create this MemoryStream in my subClass's constructor? When I Create following...

        MyObj.subObject.Add(new myObject.ChildObject());

        I want to use this MemoryStream directly. Thanks

        J 1 Reply Last reply
        0
        • M mbudak

          Impressive ;) Is it possible to auto create this MemoryStream in my subClass's constructor? When I Create following...

          MyObj.subObject.Add(new myObject.ChildObject());

          I want to use this MemoryStream directly. Thanks

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          Sure, just add a constructor to ChildObject and instantiate the memoryStream therein.

          M 1 Reply Last reply
          0
          • J J4amieC

            Sure, just add a constructor to ChildObject and instantiate the memoryStream therein.

            M Offline
            M Offline
            mbudak
            wrote on last edited by
            #5

            Thanks for your help. This is work for me. Final solution as like these;

            public class myObject
            {
                public List<ChildObject> subObject = new List<ChildObject> { };
            
                public string ObjectName { get; set; }
            
                public class ChildObject
                {
                    public string ChildObjectName { get; set; }
                    public MemoryStream ChildStream { get; set; }
            
                    public ChildObject()
                    {
                        ChildStream = new MemoryStream();
                    }
                    ~ChildObject()
                    {
                        ChildStream.Dispose();
                    }
                }
            }
            
            M 1 Reply Last reply
            0
            • M mbudak

              Thanks for your help. This is work for me. Final solution as like these;

              public class myObject
              {
                  public List<ChildObject> subObject = new List<ChildObject> { };
              
                  public string ObjectName { get; set; }
              
                  public class ChildObject
                  {
                      public string ChildObjectName { get; set; }
                      public MemoryStream ChildStream { get; set; }
              
                      public ChildObject()
                      {
                          ChildStream = new MemoryStream();
                      }
                      ~ChildObject()
                      {
                          ChildStream.Dispose();
                      }
                  }
              }
              
              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              You may also want/need to implement IDisposable on your class to have more control over the lifetime of that stream (and it's associated memory). Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              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