Results 1 to 3 of 3

Thread: [RESOLVED] Question about Variant and Byte array (wrt MSComm)

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    8

    Resolved [RESOLVED] Question about Variant and Byte array (wrt MSComm)

    I posted earlier today about a MSComm question - reading byte data in... I couldn't get some of the code to compile when placed inside my subroutine, but I pasted the MSDN code example into it's own subroutine and it DOES compile. I'm just trying to understand how/why. (I will paste the code again below). I'm new to this but looking at the code below it looks like "Dim Arr() as Byte" declares an undimensioned Array. I thought that it would need a "ReDim" statement before use, but there is not one to be found..

    "Buffer" (Variant) is assigned the MSComm1.Input (will return 10 bytes)

    Assuming "Buffer" gets 10 bytes and then "Buffer" is assigned to "Arr" (which never got dimensioned), does "Arr()" now have a dimension - the size of the 10 bytes of data "Buffer" was holding????? Can you index "Arr" to read the bytes, and assume index 0 to 9? Is there a "Sizeof" operator to get the size of Arr in case "Buffer" had (in another operation) received some unknown # of bytes?

    So can anyone explain this code and the way these variables work? Sorry, I'm just not hitting on the explanations I want in the MSDN Library.

    Thanks for any help, Dale

    Straight from the MSDN Library for VB6 proffessional, SP6
    ==============================
    'InputMode Property Example
    'This example reads 10 bytes of binary data from the communications port 'and assigns it to a byte array.

    Private Sub Command1_Click()
    Dim Buffer as Variant
    Dim Arr() as Byte

    ' Set and open port
    MSComm1.CommPort = 1
    MSComm1.PortOpen = True

    ' Set InputMode to read binary data
    MSComm1.InputMode = comInputModeBinary

    ' Wait until 10 bytes are in the input buffer
    Do Until MSComm1.InBufferCount < 10
    DoEvents
    Loop

    ' Store binary data in buffer
    Buffer = MSComm1.Input

    ' Assign to byte array for processing
    Arr = Buffer

    End Sub

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Question about Variant and Byte array (wrt MSComm)

    This is covered in the manual, which people should spend a lot more time reading. See Arrays and Advanced Features of Arrays which are also in the MSDN CDs that came with every legit copy of VB6.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    8

    Re: Question about Variant and Byte array (wrt MSComm)

    Well, I do have a legit copy since 1999 (though I'm only having to learn VB now), and I do read documents, even quoted/pasted from it as you see, but probably haven't gotten to every word yet. I'm more used to the precision and style of documentation of assembly and C. But thanks, I do see where it automatically ReDims, and have now discovered the Ubound function. Good luck getting that stick out. Guess we wouldn't need a forum if we were all such crack programmers. ;-)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width