Is it possible to iterate through a type
Hello,
I'm looking to iterate through the elements of a customized data type similar to how the for each control on form1.controls idea works, but instead to look at each element of a type. I want to simply make a for next look to test if any elements of a type are empty, and instead of testing each of the 10+ entries, I was trying to see if it could be simplified.
Re: Is it possible to iterate through a type
Since a Type is something that you can only build at design time I don't understand why you have to iterate through it. Don't you already know its contents? Perhaps I just don't understand the question so could you give an example of what you want to do?
Re: Is it possible to iterate through a type
I believe he wants to iterate through each type of the UDT to see if anything is empty, and I don't know of a way to do that.
Re: Is it possible to iterate through a type
Yes. I know the contents of the UDT. I just want to check and see if any items in it are empty. I need to be able to set an alert for my program if one of the elements is empty, and I don't want to check each one.
Re: Is it possible to iterate through a type
Quote:
Originally Posted by drag0n_45
and I don't want to check each one.
I think you will have to.
Select Case would be the easiest way, but all 10 will need to be checked individually.
Re: Is it possible to iterate through a type
i thought so. There would be noway to do it via a MoveMemory API call or anything? One function I want to retain is the ability to change the type in later versions and have this functions be dynamic.
Re: Is it possible to iterate through a type
Can't you just make a function where you pass the UDT to check if any item in the UDT is empty ? Then just call the function every time you need that check... That's one reason functions exitst....
And no... you can't make it dynamic... The only thing that you can make dynamic is to check if the whole UDT is empty (not just an iteam in the UDT)
1 Attachment(s)
Re: Is it possible to iterate through a type
Are you locked into using a UDT? Often you can do the same thing with a class and avoid some problems. I've attached a collection class example.
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
Re: Is it possible to iterate through a type
Quote:
Originally Posted by CVMichael
Can't you just make a function where you pass the UDT to check if any item in the UDT is empty ? Then just call the function every time you need that check... That's one reason functions exitst....
I could. That's what I'll probably do. I was trying to allow for maximum upgradability if I want to add newer features to my program later on. I was wanting to avoid trying to go back and edit parts of code and possibly miss things, causing errors in the future.
(I'm the guy who uses for each control in form1 all the time..lol)
Quote:
Originally Posted by Doogle
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
Lucky me, the entire type is variable length string...LOL. The CopyMemory idea is similar to what I was thinking - but variable length strings wouldn't work.
Quote:
Originally Posted by MartinLiss
Are you locked into using a UDT? Often you can do the same thing with a class and avoid some problems. I've attached a collection class example.
I couldn't quote follow your example. Are you simply using a Class with public properties that you assign at run time and then to check and see if any are empty just iterate through each object and check the property?