2016-11-17 5 views
0

私はあなたの助けが必要です。私は、コレクション内のクラス値を変更し、 "変数"を変更したい。編集値のコレクション - 変数機能...(?)

cAdmin = xy 
cCheckDate=yz 
cPruefState= abc 
colSomeName.add Classname, key1 

これをコレクション(colSomeName)に保存します。フォーム上で、イベントが発生すると、コレクションのキーが検索され、現在の値が変更されます。私は、(例えば)の値を設定し、クラスで

Sub CheckOut() 
.. 
editInCollection colSomeName, „StackOver“ 
. . . 



Public Function editInCollection(_ 
        ByRef col As Collection, _ 
        ByRef Elem As String) As Boolean 

    On Error GoTo Ende 
    If IsEmpty(col(Elem)) Then 
     MsgBox " This Element: " & col(Elem) & „ not exits!", _ 
       vbCritical = vbOKOnly, "s o r r y " 
    Exit Function 
    End If`enter code here` 
    IsInCollection = (Err.Number = 0) 
     ' 
     col(Elem).cAdmin =Environ("username") 
     col(Elem).cCheckDate = Date 
     col(Elem).cPruefState= True 
     ' 
    Exit Function 

    Ende: 
     IsInCollection = False 
    End Function 

それは完璧に動作します。 しかし、私は異なるクラス/コレクションのためのfunkionを使用したいと思います。 これはpossipleですか? 私idearは

このような
Public Function editInCollection(_ 
       ByRef col As Collection, _ 
       ByRef Elem As String, clsValue as Variant(?), newValue as variant) As Boolean 

Dim as …. 
For each xAll in col(Elem) 
     If xAll = clsValue then 
     clsVlaue = newValue 
     end if 
    exit for 
next 
end function 

ですが、私は動作しません。どのように私はこの

+0

あなたの例(一番上のスニペット)をもう少し具体的に編集できますか? IMOあなたがしようとしていることとその理由を理解することは難しいです。 –

答えて

2

はあなたがプロパティの名前を使用して、オブジェクトのプロパティにアクセスするためにCallByNameを使用することができます実現することができます。

のclsTest:テスト目的のために非常に単純なクラス...

Option Explicit 
Public TestProperty As String 

例コード:

Sub Tester() 

    Dim o As New clsTest, col As New Collection 

    o.TestProperty = "hello" 

    col.Add o, "key1" 

    Debug.Print "Before", o.TestProperty '>>> hello 

    'Update the property named "TestProperty" to "I was updated" 
    Debug.Print "updated?", EditInCollection(col, "key1", _ 
          "TestProperty", "I was updated") '>>> true 

    Debug.Print "After", o.TestProperty '>>> I was updated 

End Sub 



Function EditInCollection(col, key, propName, newValue) 

    Dim o As Object, rv As Boolean 
    On Error Resume Next 
    Set o = col(key) 'is there an object with this key? 
    On Error GoTo 0 

    If Not o Is Nothing Then 
     'Object was found in the collection: update 
     ' the property in "propName" to "newValue" 
     CallByName o, propName, VbLet, newValue 
     rv = True 
    End If 

    EditInCollection = rv 'return success or failure 
End Function 

複数のプロパティを更新する場合は、プロパティ名の配列と新しいv第3および第4のパラメータとして示される。

+0

ハローティム - それはすごくうまくいきます!あなたは私をとても幸せにする - ありがとう! – woerny

関連する問題