Solved

Dumb question about array allocation

Posted on 2006-10-19
2
193 Views
Last Modified: 2010-04-23

Dumb question.  So the following code (lifted from msdn):

Dim binWriter As New BinaryWriter(New MemoryStream())
Dim binReader As New BinaryReader(binWriter.BaseStream)
Dim verifyArray() As Byte = binReader.ReadBytes(dataArray.Length)

will allocate the verifyArray to the correct size for me?  And as long as verifyArray is in scope, the data will still be there?

And if I called verifyArray = binReader.ReadBytes again, it would replace the old array with the new data, and the old array would eventually be garbage collected?

If so, it seems like a strange departure (from the pre-.NET world) for a called function to allocate the array for you, right?  What's the rhyme/reason for that?
0
Comment
Question by:riceman0
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17771631
That's correct.

I don't see this as any different from VB6.

Take the Split() function for example:

    Dim data As String
    Dim myArray() As String
   
    data = "1,2,3,4"
    myArray = Split(data, ",")
    Debug.Print LBound(myArray) & " --> " & UBound(myArray)

It automatically allocated the array to the right size for us...
0
 

Author Comment

by:riceman0
ID: 17771756

Hmm, that's true.  Thanks.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question