vb.net

VB.NET Cookie Mystery


I'm working on a legacy app, and I'm a little stumped by the intention behind this:

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Request.Cookies("userinfo").Add("<key>", "<value>")
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = Request.Cookies("userinfo")("<key>")

    End Sub

I get compilation error in Button1_Click:

'Add' is not a member of 'System.Web.HttpCookie'.

I should add I don't know much VB.


Solution

  • I think that Request.Cookies("userinfo") should be Request.Cookies("userinfo").Values. That will return a NameValueCollection, which does have an Add method that accepts a key and a value as Strings. The other one is OK because the HttpCookie object can be indexed directly by key.