Passing a variable as the first parameter to a NameValueCollection.

garywm

New member
Joined
Jun 26, 2011
Messages
3
Programming Experience
10+
when adding to a NameValueCollection you normally would do this:

nvc.Add("SOME LITERAL STRING", "MY VALUE")


but if you dont know the first parameter until you read it in from a file then you would want this:

nvc.Add(myVar, "MY VALUE")


this does not work! The NameValueCollection parameters SAY they want a string, but they really don't, they want a "Literal" sting.

So, the question is: How do I pass a variable string to the first parameter?
 
What you just said is completely false. Anywhere a String is required, you can use ANY String at all. It can be a literal, a local variable, a field, a property value, the return value of a functionh or the result of any expression at all. As far as the compiler is concerned, a String is a String. Execute the following code and tell us EXACTLY what happens. If it doesn't do what you expect, don't just say that it doesn't work.
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim nvc As New Specialized.NameValueCollection
        Dim theKey = "The Key"
        Dim theValue = "The Value"

        nvc.Add(theKey, theValue)

        MessageBox.Show(nvc(theKey))
        MessageBox.Show(nvc("The Key"))
    End Sub

End Class
Regardless, you probably shouldn't really be using a NameValueCollection in .NET 4.0. Now that we have generics, you should probably be using a Dictionary(Of String, String). The NameValueCollection does support retrieval by key or index though, which can make it advantageous in certain situations.
 
Thanks for the quick reply and for helping me slim down my problem.

I am trying to use a .DrawImg method and when I use theKey with "The Key" as the string it fails to show the picture in my PDF file. But if I use "THEKEY" as the contents of the variable, then it works. Why is it that any variable that contains space(s) wont show in my PDF document that is storing NameValueCollection pairs to use as the pictures to display?

I'll keep debugging, but any clues would be nice.
 
I wrote a quick function to remove all spaces from the variable and now it is working. Again it wasn't working with either the String Literal or the variable if they had spaces. They stored in the NameValueCollection alright, but the PDF writer did not like them with spaces.
 

Latest posts

Back
Top