How can I use a MemoryStream in a System.Generic.List<>
-
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 LINEThanks for your reply. :rolleyes:
-
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 LINEThanks for your reply. :rolleyes:
: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 -
: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 -
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
-
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(); } } }
-
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(); } } }
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: