-1

Well I have created a program that takes some files (Mp3) and change their tags recently I wanted to add some new Subs (like: Take the songs name and make every letter in it upercase). The problem is that i use a list with its items to be keyvaluepairs

Public MP3List As New List(Of KeyValuePair(Of String, String))

When i tried to edit the key or value of any Item in that list i get an error (That this is READONLY) Example:

    For Each Song In MP3List
        Song.Key = "Something"
    Next

I add items like this :

Private Function OpenAFile()
    Dim MP3List1 = MP3List
    Dim oFileDialog As New OpenFileDialog
    oFileDialog.Title = "Επέλεξε ένα MP3 Άρχειο"
    oFileDialog.Filter = "MP3 Files|*.mp3|All Files|*.*"
    oFileDialog.Multiselect = True
    Dim Path As String = ""
    Dim Name As String = ""
    Dim NewPair As New KeyValuePair(Of String, String)
    If oFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
        For Each sPath In oFileDialog.FileNames
            Path = New String(sPath)
            Name = New String(Strings.Split(Path, "\").ToList(Strings.Split(Path, "\").ToList.Count - 1))
            NewPair = New KeyValuePair(Of String, String)(Name, Path)
            If Not MP3List1.Contains(NewPair) Then MP3List1.Add(NewPair)
        Next
    End If

    Return MP3List1

End Function

So the idea is this: Each time i press A button to add a song it will run the function OpenAFile() and it was working fine then . Now that i want to change a key or value i get this error

The error

Thanks for the Help and sorry for bad english

sheach
  • 65
  • 11
  • The key in a KVP is meant to identify a specific entry and not supposed to change (and if that loop did execute all your items would have the same key and your code could not tell one from the other). You could fix the key before you add the item. Changing the key wont update MP3 tag though. – Ňɏssa Pøngjǣrdenlarp May 05 '15 at 18:08
  • maybe don't populate the key until you know what it is...or remove it and add a new one with the right information...or keep the key the same but have the value be a custom object that stores everything you might need to know... – Jeremy May 05 '15 at 18:08
  • Well I want to be able to change the key whenever i want cuz thats what i want to do. (I know what my key is but i want to be able to change it) – sheach May 05 '15 at 18:10
  • @Plutonix I know that if my loop works it wont be any useful but for now the problem is the readonly part . I want to be able to add my key and then in runtime change it whenever i want. (I know it wont change the mp3 tag) – sheach May 05 '15 at 18:12
  • If you want to change the key whenever you want you should then talk to Microsoft as keys in key-value pair are read-only by design (and logic). I suggest you to remove the key you want to change and add a new one with the same value. – Josh Part May 05 '15 at 18:13
  • That i didnt know @JoshPart so you tell me that this is how it works and that my code has nothing to do with it? If so ok thanks – sheach May 05 '15 at 18:14
  • 1
    You could also use a `Dictionary(Of String, String)` instead of a `List(Of KeyValuePair(Of String, String))` – Josh Part May 05 '15 at 18:17

1 Answers1

0

The Keys in a KeyValuePair are readonly because they are often used as the key in a hash table. Changing the key would cause issues where you would lose your item in the hash.

If you want to do something like this, you could always create your own data type that stores a key and value. An overly simplified example would be as follows.

Public Structure PathNamePair
    Public Property Path As String
    Public Property Name As String

    Public Sub New(path As String, name As String)
        Me.Path = path
        Me.Name = name
    End Sub
End Structure

I will note that in order to get better performance with your Contains method, you should also implement IEquatable(Of T), but that's probably beyond the scope of this question. I will also note that it is not best practice to have a ValueType (Structure) that is mutable.

Scott
  • 370
  • 2
  • 7