Results 1 to 10 of 10

Thread: Is it possible to iterate through a type

Threaded View

  1. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Is it possible to iterate through a type

    If you arrange your UDT such that like types are together you could use CopyMemory and a loop to check the values. (Althoough I think you end up typing more characters than you would if you'd performed 10 case statements). It only works for types that are fixed length excepting Strings. (It just doesn't work for string variables)
    eg
    Code:
    Private Type TEST_TYPE
        aLong1 As Long
        aLong2 As Long
        aLong3 As Long
        anInteger1 As Integer
        anInteger2 As Integer
        anInteger3 As Integer
    End Type
    
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
                    (Destination As Any, _
                     Source As Any, _
                     ByVal Length As Long)
    
    Private udtReal As TEST_TYPE
    Private lngUDTLongs(2) As Long
    Private intUDTIntegers(2) As Integer
    
    Private Sub Command1_Click()
    Dim intI As Integer
    '
    ' Set-up some values in the udt
    '
    udtReal.anInteger1 = 10
    udtReal.anInteger2 = 12
    udtReal.aLong2 = 123456
    '
    ' Copy the Integer values to the integer array
    ' and display their values
    '
    CopyMemory intUDTIntegers(0), udtReal.anInteger1, 6
    For intI = 0 To 2
        Debug.Print intUDTIntegers(intI)
    Next intI
    '
    ' Do the same for the Longs
    '
    CopyMemory lngUDTLongs(0), udtReal.aLong1, 12
    For intI = 0 To 2
        Debug.Print lngUDTLongs(intI)
    Next intI
    End Sub
    Last edited by Doogle; Mar 11th, 2008 at 01:25 AM.

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