Re: Writing array to disk with Put

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: J French (erewhon_at_nowhere.uk)
Date: 12/20/04


Date: Mon, 20 Dec 2004 08:49:29 +0000 (UTC)

On Sun, 19 Dec 2004 17:51:30 GMT, "mayayana"
<mayaXXyana1a@mindYYspring.com> wrote:

<snip>

> Thank you, Jerry. I find, myself, that between lack
>of coffee in the morning and lack of sleep at night
>it's not easy for me to send a post without mistakes. :)
> (Morning is the worst. My mind is in a semi-hypnogogic
>state until after the second cup of coffee....or maybe I'm
>just making excuses for the noticeably ossifying mental
>process that seems to accompany aging.)

Tell me about it !
I also have problems reading the text in my News Reader
- but changing the Font/Size makes things worse

> What I was actually doing was saving bytes in a
>winsock callback (for a dependency-free file download
>component). I'm dimming a dynamic array with
> Dim A1() as Byte
> and then redimming for the recv call. Then the loaded-up
>A1 goes into a collection, in order to accomodate the
>possibility of large downloads. When I go to write the file I'm
>just writing the saved arrays out, one at a time. All of it was
>working fine for text files, because I was converting the
>arrays to unicode strings before writing, but it was causing
>problems with writing binary files.
>
>I knew that a fixed-size array could be written:
> Dim A1(1024) as Byte......
> Put #FF, , A1()
>But the docs say that you get the array descriptor written
>if you try to use a dynamic array.

Not my VB5 docs
- not if the file is opened in Binary or Random mode

I must confess the docs are not that lucid
- but I've spent a lot of time squinting at that area

> I still don't understand why I can't just write the
>dynamic array in the collection, and yet it works to
>assign the collection Item to a new dynamic array.
> In other words, I can't use Collection.Item(i) but
>I can use Dim A1() as Byte: A1 = Collection.Item(i)
>as Norm Cook showed.

While in the Collection the 'thing' you are trying to write is
perceived by VB as being a Variant containing a Dynamic Byte Array

Get and Put for Variants just don't work the same as for the things
they contain.

> Maybe there's an extra step with the collection?....
>...that the Item pointer does not point directly to the
>Item contents?
>
> I was curious about whether I might just use
>an ANSI string directly, with s = Collection.Item(i) but
>I wondered if the Chr(0)s in that might cause a
>problem. (Since I had to fire up the sample program
>and download something for every test I didn't
>research all the options.)

Yes, in one of the examples I showed using StrConv to convert the Byte
Array (in a Deviant) into a Unicode String, then relying on VB's trick
of converting Unicode to ANSI whenever it writes to disk.
It works - but I find it slightly ugly

Personally I would experiment with a simple Function that takes in a
Variant and returns a pure Byte Array

Function VarArrayToByteArray( V As Variant ) As Byte()
      VarArrayToByteArray = V
End Function

It is really the same as explicitly copying the Array, but much more
neatly packaged.

I can't test this with VB5, and it is possible that the function will
really return a Variant - but it is worth testing.



Relevant Pages

  • Re: ReDim not working as expected. Array expert needed.
    ... MsgBox TypeName) ' Type Variant() ... examples of how array dimensions work in VBA. ... Sub ArrayStudies14() ... Dim MyArray As Variant ' Declare nonarray variant. ...
    (microsoft.public.excel.programming)
  • Re: C++ Conversion functions for pointers
    ... I know I can create an array of Variant pointers and assign to these ... >> But the problem I am having is converting an int pointer to a Variant ...
    (alt.comp.lang.learn.c-cpp)
  • Re: ReDim not working as expected. Array expert needed.
    ... "I've seen people use a variant to contain the values ... array in the first place, the damage is done and it's not likely to get ... I next told you not to redim it. ... Dim MyArray As Variant ' Declare nonarray variant. ...
    (microsoft.public.excel.programming)
  • Re: Need Help With Arrays and Passing Arguments
    ... array definition to illustrate without really thinking about it. ... Dim myArray() As Variant ... > Sub CallTest1() ...
    (microsoft.public.word.vba.general)
  • Re: How do I set up a 6 day work week in Excel?
    ... With your workbook opened and active, press Alt+F11 - this opens VBA Editor. ... Dim arrayH As Variant, arrayW As Variant ... ' or not an array or cell range with numeric values, ... ReDim arrayH) As Variant ...
    (microsoft.public.excel.worksheet.functions)