ReDim Preserve data(totalBytesRead + dataBlock) to C#
-
Use the
Array.Resize
method. Note that neitherReDim Preserve
norArray.Resize
actually does resize the array. A new array is allocated, and the data is copied from the original array. If possible, you should allocate a sufficiently large array from the stast.Despite everything, the person most likely to be fooling you next is yourself.
-
Use the
Array.Resize
method. Note that neitherReDim Preserve
norArray.Resize
actually does resize the array. A new array is allocated, and the data is copied from the original array. If possible, you should allocate a sufficiently large array from the stast.Despite everything, the person most likely to be fooling you next is yourself.
hello i used from Array.Resize,but i could not arrange its arguments please help me that how can i arrange its arguments? thanks public byte[] ExtractBytesFromStream(Stream stream, int dataBlock) { int totalBytesRead = 0; byte[] data; try { while (true) { Array.Reverse(data, totalBytesRead, dataBlock);? Array.Resize(data, totalBytesRead + dataBlock);? int bytesRead = stream.Read(data, totalBytesRead, dataBlock); if (bytesRead == 0) { break; // TODO: might not be correct. Was : Exit While } totalBytesRead += bytesRead; } Array.Reverse(data, -1, totalBytesRead); return data; } catch(Exception ex) { MessageBox.Show(ex.Message); return null; } }
-
hello i used from Array.Resize,but i could not arrange its arguments please help me that how can i arrange its arguments? thanks public byte[] ExtractBytesFromStream(Stream stream, int dataBlock) { int totalBytesRead = 0; byte[] data; try { while (true) { Array.Reverse(data, totalBytesRead, dataBlock);? Array.Resize(data, totalBytesRead + dataBlock);? int bytesRead = stream.Read(data, totalBytesRead, dataBlock); if (bytesRead == 0) { break; // TODO: might not be correct. Was : Exit While } totalBytesRead += bytesRead; } Array.Reverse(data, -1, totalBytesRead); return data; } catch(Exception ex) { MessageBox.Show(ex.Message); return null; } }
First of all, you don't even have an array at all. You have to create an array before you can resize it. Why are you reversing the data in the array? If there is a reason to revere any data in the array, you can't reverse data that isn't even in the array yet, and there is no reason to reverse a bunch of zeroes. Also, you can't use -1 as starting index when reversing a part of an array. You have to resize the array to the size of the actual data read before you return the array. Now you are returning an array that is larger than the actual data read, and you don't return how much of the array that contains valid data.
Despite everything, the person most likely to be fooling you next is yourself.