Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

Is it possible to use variable to specify key in user defined types?

Is it possible to use a variable to specify a .Key for a user defined type?

For instance, if i define this:
Private Type myType
   lName as String * 20
   fName as String * 20
   pAge  as string * 3
End Type
Dim myRecord as myType

I know I can access the keys like this:
With myRecord
  .lName = "Last Name"
  .fName = "First Name"
  .pAge  = 23
End With

But is it possible to specify the keys ( .lName, .fName, .pAge ) via variables?
Like:
Dim lKey as String, fKey as String, pKey as String
lKey = ".lName"
fKey = ".fName"
pKey = ".pAge"

With myRecord
   lKey = "Last Name"
   fKey = "First Name"
   pKey = 23
End With
or maybe something like
myRecord(lKey) = "Last Name"

Or is there maybe some other way to specify the keys via referencing an index or something?
0
salinasj
Asked:
salinasj
1 Solution
 
supunrCommented:
Not that I know of.

With myRecord
  lKey = "Last Name"
  fKey = "First Name"
  pKey = 23
End With

this will assign individual values to those variables.

myRecord(lKey) = "Last Name"

this is not a valid as well. I tried it VB.  I am interested in the outcome of this qustion as well.
0
 
CimperialiCommented:
No. but you can do the following:

Private Type myType
  lName as String * 20
  fName as String * 20
  pAge  as string * 3
End Type

Dim myRecord() as myType

'...
redim myRecord(50)
'....
myRecord(0).lName="Last Name"

or
myRecord(AVariableThatHoldIndex).lName="Last Name"
0
 
RubynCommented:

Private Type MyType
    Key As String
    FName As String
    LName As String
    Age As Long
End Type

'Holds UDTS
Private m_MyTypes() As MyType

'Returns the count of UDT created
Private Property Get MyTypeCount() As Long
On Error Resume Next
    MyTypeCount = UBound(m_MyTypes) - LBound(m_MyTypes) + 1&
End Property

'Returns the index of UDT (Search UDTs with key)
Private Property Get MyTypeGetIndex(Key As String) As Long
Dim Index As Long
    For Index = 0 To MyTypeCount - 1&
        If m_MyTypes(Index).Key = Key Then MyTypeGetIndex = Index: Exit Property
    Next
    Let MyTypeGetIndex = -1
End Property

'Reads UDT
Private Property Get MyTypes(Key As String) As MyType
Dim Index As Long
    Let Index = MyTypeGetIndex(Key)
    If Not Index = -1 Then
        MyTypes = m_MyTypes(Index)
    Else
        Call Err.Raise(Number:=1, Description:="Key Not Found")
    End If
End Property

'Writes UDT
Private Property Let MyTypes(Key As String, MyType As MyType)
Dim Index As Long
    Let Index = MyTypeGetIndex(Key)
    If Index = -1 Then
        Let Index = MyTypeCount
        ReDim Preserve m_MyTypes(Index)
        Let MyType.Key = Key
    End If
    Let m_MyTypes(Index) = MyType
End Property

Private Sub Form_Load()
Dim MyType As MyType
    Let MyType.FName = "First"
    Let MyType.LName = "Last"
    Let MyType.Age = 10
'Save UDT with key
    Let MyTypes("Any Key") = MyType
'Load UDT with key
    Let MyType = MyTypes("Any Key")
'Access UDT variables directly also
    Call MsgBox(MyTypes("Any Key").FName)
End Sub

Without using classes and collection object. we can read and write with key string.
I think this will be usefull for all. :)
0
 
salinasjAuthor Commented:
SWEET!

Thanks!
0
 
JNSTAUBCommented:
create a collection:
Private mCol As Collection


Public Function Add(iname As String, fname As String, page as string,Optional sKey As String) As mytype
    'créer un nouvel objet
    Dim objNewMember As mytype
   
   
    objNewMember.iname = NOM
   
      ......
   
    If Len(sKey) = 0 Then
        mCol.Add objNewMember
    Else
        mCol.Add objNewMember, sKey
    End If


    'renvoyer l'objet créé
     Add = objNewMember
   

End Function

i didn't test it, i use object in my collection
regards
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now