Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to declare a Byte array like this

Dim buffer() As Byte


But I don't know the size of the array SO how can I set it to take the max?
and I Need to set something since it give me it need to be assigned later on.
Posted
Comments
Mehdi Gholam 18-Nov-11 3:09am    
Setting byte arrays to max will consume memory, just reallocate the buffer when you know the size.
Sergey Alexandrovich Kryukov 18-Nov-11 3:22am    
You are right, but then it's much better to not use array at all, please see my solution.
--SA

As Mehdi says, never assign a buffer to the maximum size!
In .NET the maximum size of any single object is 2Gb, so the Max size of a bytes array is 2147483648 bytes. If you keep on allocating this much memory without actually needing it, you will slow your computer to a crawl very, very quickly...
Initially, define it as
VB
Dim buffer() As Byte = nothing
And then re-assign it when you know how many bytes you need.
 
Share this answer
 
Comments
Mehdi Gholam 18-Nov-11 3:18am    
5'ed
Sergey Alexandrovich Kryukov 18-Nov-11 3:24am    
Basically correct, but using generic list is better, see my solution.
4'ed.
--SA
JBoada 14-May-12 16:13pm    
Hi everyone,

I'm having an issue using an array. I require to load a large file into an array for a third-platform, it requires that way, later serialize it to an Base64 string.

For large files, .Net shows "System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown." due to it can't create the required array.

So far, I haven't be able to load large files. Then I would need to deserialize Base64 strings (this is the response from the third-system for a request) to bytes, I assume that it is liked load a large file in an array.

so, is it possible to create large array like "dim mybytes(max.integer-1) as byte"?


Best Regards,

Juan Boada
OriginalGriff 15-May-12 3:23am    
You need to raise this as a separate question - it's rude to tack it on to somebody elses.
You also need to give us better information - how big are the files, what code is giving the exception, and so forth.

It looks like you don't know the array size at the moment of its creation. That said, you should not use array. Instead, use the generic class System.Collections.Generic.List, see http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^]. Yes, you can use array, too, but reallocation is not a very good thing. Due to performance and other reason, using array and not list cannot be justified.

—SA
 
Share this answer
 
Comments
Mehdi Gholam 18-Nov-11 3:31am    
I would agree for any array of other than byte ( int, object, etc.) but for bytes especially for things like packets and files, List of bytes is not very good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900