MemoryStream.Write() Offset cannot be zero

  • Thread starter Thread starter Gravity
  • Start date Start date
G

Gravity

Hi,

I am using the MemoryStream class with the member function, Write( ). I
found that the 2 parameter, which supposed to control the offset of the
destination cannot be zero. As long as it is non zero, like 1, 2 or 3, it
will be catch.

Please advise.
 
Gravity said:
I am using the MemoryStream class with the member function, Write( ). I
found that the 2 parameter, which supposed to control the offset of the
destination cannot be zero. As long as it is non zero, like 1, 2 or 3, it
will be catch.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Hi,

I am using the MemoryStream class with the member function, Write( ).
I found that the 2 parameter, which supposed to control the offset of
the destination cannot be zero. As long as it is non zero, like 1, 2
or 3, it will be catch.

I'm not sure if this will solve your problem or not since you contradict
yourself (can it be zero or not?), but consider this...

The 'offset' isn't the offset of the destination, its the offset of the
source. If you want to control the position of the destination, use the
MemoryStream objects .Seek(...) method or .Position works as well. You
might need to use the .SetLength(long) function of the MemoryStream first
to make sure you don't seek where there is no data.
 
I see... I think I might have some missunderstanding here. I have always
compare to C/C++ -> memcpy( )

If I am writting 5 parameter of int32 which each consist of 4 bytes into it.

Does it mean the code should look like;

MemoryStream.Write(paramA, 0, 4);
MemoryStream.Write(paramB, 0, 4);

MemoryStream.Write(paramC, 0, 4);

MemoryStream.Write(paramD, 0, 4);

MemoryStream.Write(paramE, 0, 4);

where the write operation will be performed will automatically increased to
the next location?
 
Gravity said:
I see... I think I might have some missunderstanding here. I have always
compare to C/C++ -> memcpy( )

If I am writting 5 parameter of int32 which each consist of 4 bytes into it.

Does it mean the code should look like;

MemoryStream.Write(paramA, 0, 4);
MemoryStream.Write(paramB, 0, 4);

MemoryStream.Write(paramC, 0, 4);

MemoryStream.Write(paramD, 0, 4);

MemoryStream.Write(paramE, 0, 4);

where the write operation will be performed will automatically increased to
the next location?

Absolutely. It's a *stream*, not an array. Imagine you were writing to
a file - it's just like that, except it all stays in memory.
 
Thanks. Its been a great help.


Jon Skeet said:
Absolutely. It's a *stream*, not an array. Imagine you were writing to
a file - it's just like that, except it all stays in memory.
 
Back
Top