Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Re-declare/re-use local memvar - How-To?

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
568
US
Colleagues,

I have, in some Sub, declared a memvar at the beginning as Char array of 14. Now I'd like to change it to Char array of 16MB (just do not want to multiply them local memvars incessantly).
Here's the code:

Code:
Dim lcBuffer(14) As Char

Using loStreamReader As StreamReader = New StreamReader(tcFileIn)
   loStreamReader.Read(lcBuffer, 0, 14)
End Using

Being "old school", I presume that trying to read more than 14 chars into lcBuffer would result in it accepting only those 14 chars. (Am I right, BTW? [ponder])
Attempt to re-Dim lcBuffer to lcBuffer(2 ^ 14) As Char erred with message "Local variable 'lcBuffer' is already declared in the current block."
Question is: how do I re-declare or, at least, re-assign a local memvar within the same block?
OR
Would "Using - End Using" construct help in this respect?
Please advise.

TIA!

Regards,

Ilya
 
This should work:

ReDim lcBuffer(2^14)

This will clear any data already in lcBuffer. If you already have data in lcBuffer and want to keep it use the Preserve keyword:

ReDim Preserve lcBuffer(2^14)

Also, trying to read more than 14 chars into the buffer (before the ReDim) will result in an index out of bounds exception.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
With VB.NET, unlike classic VB, you do not need the As Char with the ReDim
 
Scheise! (That's the only civil curse that came to my mind - apologies, mea culpa!)
Totally forgot about this ReDim statement! (This is what 14-year gap in working in VB does to you! [blush] )
Thank you, colleague J. E. Benson!

Right, colleague Strong M., VB interpreter told me same thing. :)
Problem is resolved, case is being closed.

Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top