Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello all, How does one update i

Status
Not open for further replies.

pozitron969

Programmer
May 14, 2002
29
0
0
US
Hello all,

How does one update information in a collection after it has been added to said collection? I have a collection of classes that store media files. I am setting the key for each item to be the filename, and I don't really want to use index #'s. Here is what my app does:
1) The user clicks on an item in the list and it gets the class from the collection based on the key.
2) The user updates the information and I write it to a temporary class variable.
3) The user clicks on another item in the listbox and I am trying to save the temporary class variable before loading the new one to be edited.

Here is the statement that it is erroring at:

[tt]
Public myPlaylist As New Collection
Public myMedia As New clsMediaFile
...
...
Private Sub SaveProp()
If sCurrentKey <> &quot;&quot; Then Set myPlaylist.Item(sCurrentKey) = myMedia
End Sub[/tt]


I get the error Run-time error '438' Object doesn't support this property of method


Thank you
 
You add to a collection like so..

myPlayList.Add Item, Key

myPlayList.Add myMedia, sCurrentKey
 
Yes, I understand that. I would like to know how does one update an element in a collection already populated with data when they only know the key? Is there a way to do this without having to copy each element from the old collection to the new collection?

Example Data:(Element,key)

Andy,me
Mik,brother
Levi,friend

I would like to update &quot;Mik&quot; to be &quot;Mike&quot;, and I know the key which is &quot;brother&quot;.
 
Leave out the Set keyword

Private Sub SaveProp()
If sCurrentKey <> &quot;&quot; Then myPlaylist.Item(sCurrentKey) = myMedia
End Sub
 
Thank you for replying. I tried that and I get the same error. The myPlaylist collection is populated with myMedia classes. Any other possibilities? Here is the code from my form, maybe I am doing something else wrong.


Public myPlaylist As New Collection
Public myMedia As New clsMediaFile

Private sCurrentKey As String

Public Sub SetPlaylist()
Dim media As clsMediaFile
For Each media In myPlaylist
lstMedia.AddItem media.sFile
Next
End Sub

Private Sub chkActive_Click()
If chkActive.Value = 0 Then
myMedia.bActive = False
Else
myMedia.bActive = True
End If
End Sub

Private Sub Form_Load()
If myPlaylist.Count > 0 Then
SetPlaylist
End If
End Sub

Private Sub lstMedia_Click()
SaveProp
sCurrentKey = lstMedia.List(lstMedia.ListIndex)
Set myMedia = myPlaylist(sCurrentKey)
SetProp
End Sub


Private Sub SaveProp()
If sCurrentKey <> &quot;&quot; Then myPlaylist.Item(sCurrentKey) = myMedia
End Sub


Private Sub SetProp()
With myMedia
lblFull.Caption = .sFull
txtComments.Text = .sComment
txtPlays.Text = CStr(.iPlays)
txtVolume.Text = CStr(.iVolume)
If .bActive Then
chkActive.Value = 1
Else
chkActive.Value = 0
End If

For Each entry In .Categories
lstCat.AddItem entry
Next

For Each entry In .Keywords
lstKey.AddItem entry
Next
End With
End Sub

Private Sub txtComments_Change()
myMedia.sComment = txtComments.Text
End Sub

Private Sub txtPlays_Change()
myMedia.iPlays = CInt(txtPlays.Text)
End Sub

Private Sub txtVolume_Change()
myMedia.iVolume = CInt(txtVolume.Text)
End Sub
 
Private Sub SaveProp()
If sCurrentKey <> &quot;&quot; Then myPlaylist.Item(sCurrentKey) = myMedia.??? myMedia.sFull .sComment .iVolume
End Sub
 
Looks like your trying to save several values from within the myMedia .cls into one value in the collection
 
Yes, the myMedia class has both properties and procedures. I was hoping to be able to update each of the properties in the temporary variable before writing it back to the collection. Possible? Hope so...
 
Sorry, I just reread your previous post and noticed that you said that it seems that I was trying to write all of the properties of the myMedia into only one of the properties of the myPlaylist element. I am basically trying to do the opposite of this procedure (with the exclusion of the call to the SaveProp) I am able to read the whole class from one element of the collection, but not write the updated information in its place...


Private Sub lstMedia_Click()
SaveProp
sCurrentKey = lstMedia.List(lstMedia.ListIndex)
Set myMedia = myPlaylist(sCurrentKey)
SetProp
End Sub
 
Then delete the element after reading it and replace it with the new data
 
Ok, that will work and I may end up doing that. One last nit picky detail though. If I delete the element, won't the original position of the element be lost. If I delete the element and add it again the new element will be appended to the bottom of the collection instead of the original position. On a side question: is it possible to use an array instead of a collection? If so will I be able to reference it directly in the array and get around the problem I am having with the collection? Thank you again.
 
If your using the collection key to identify the item then why does position bother you?

You could use an array also, I guess its up to how you want to code your app.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top