By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,493 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,493 IT Pros & Developers. It's quick & easy.

How do I Access Elements of a UserType Using a String

P: 2
Is it possible to reference elements of a UserType indirectly with string variables?

EG.
Expand|Select|Wrap|Line Numbers
  1. Public Type myType
  2.     subTypeOne as Integer
  3.     subTypeTwo as Integer
  4.     subTypeThree as Integer
  5. End Type
  6.  
  7. Public Sub mySub
  8.     Dim myUDT as myType, indirectRef as string, myStr as string
  9.  
  10.     myStr="One"
  11.     indirectRef ="subType" & myStr
  12.     myUDT.(indirectRef) = 32
  13. End Sub
Feb 1 '12 #1
Share this Question
Share on Google+
5 Replies


ADezii
Expert 5K+
P: 8,597
To the best of my knowledge, this will never work.
Feb 1 '12 #2

P: 2
how about using collections or dictionaries?
Feb 1 '12 #3

NeoPa
Expert Mod 15k+
P: 31,186
I'm afraid the answer to your question is still No. No collection for the elements of a UserType is presented to the interface. Even the Eval() function was unable to handle the reference.
Feb 1 '12 #4

ADezii
Expert 5K+
P: 8,597
What, exactly, are you trying to accomplish?
Feb 2 '12 #5

ADezii
Expert 5K+
P: 8,597
You, yourself, brings up a very interesting point in using a Collection in order to achieve something parallel as to what you are looking for. It is not the same as a Type...End Type Construct, but it is similar. In a Collection, individual Items can be Set/Retrieved by using its Key Argument, (String) as indicated below:
Expand|Select|Wrap|Line Numbers
  1. Dim col As New Collection
  2. Dim varCol As Variant
  3. Dim intCtr As Integer
  4. Dim strItem As String
  5.  
  6. For intCtr = 1 To 100
  7.   col.Add Item:=intCtr ^ 2, Key:="Item#" & CStr(intCtr)
  8. Next
  9.  
  10. strItem = "Item#" & CStr(75)
  11.  
  12. Debug.Print "The 75th Item in the col Collection is: [" & FormatNumber(col(strItem), 0) & "]"
  13.  
  14. 'Clear the Collection
  15. For intCtr = 1 To 100
  16.   col.Remove 1
  17. Next
OUTPUT of Debug.Print
Expand|Select|Wrap|Line Numbers
  1. The 75th Item in the col Collection is: [5,625]
Feb 2 '12 #6

Post your reply

Sign in to post your reply or Sign up for a free account.