how to make cookies into an array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • André

    #1

    how to make cookies into an array?

    Hi,

    I need several cookies depending of an variable (x), so i defined a
    HttpCookie() as an array.
    My problems:
    1)I get the error: Object reference not set to an instance of an object.
    2)My second question is: how to request those cookies, because there is no
    name?

    Thanks
    André


    I did:
    x=5
    Dim cok(x) As New HttpCookie 'can't give a name to the cookie
    .....

    for k=1 to x
    cok(k).Value = "cookie" & k
    Response.Cookie s.Add(cok(k))
    next

    -----------------------------------
    recovering the cookies?
    for i=1 to x
    cok(i) = Request.Cookies ("??")
    next



  • Tim Patrick

    #2
    Re: how to make cookies into an array?

    If you are using VB2005, you can switch to one of the Generic collections
    that includes a key so that you can look up the cookie by name.

    Dim allCookies As Generic.Diction ary(Of String, HttpCookie)

    Add new cookies to the dictionary this way:

    allCookies.Add( cookieName, theCookieItself )

    Access the cookie this way:

    theRetrievedCoo kie = allCookies(cook ieName)

    -----
    Tim Patrick - www.timaki.com
    Start-to-Finish Visual Basic 2005
    Hi,
    >
    I need several cookies depending of an variable (x), so i defined a
    HttpCookie() as an array.
    My problems:
    1)I get the error: Object reference not set to an instance of an
    object.
    2)My second question is: how to request those cookies, because there
    is no
    name?
    Thanks
    André
    I did:
    x=5
    Dim cok(x) As New HttpCookie 'can't give a name to the cookie
    ....
    for k=1 to x
    cok(k).Value = "cookie" & k
    Response.Cookie s.Add(cok(k))
    next
    -----------------------------------
    recovering the cookies?
    for i=1 to x
    cok(i) = Request.Cookies ("??")
    next

    Comment

    • André

      #3
      Re: how to make cookies into an array?

      Hi,

      thanks for replying ...

      I tried this (it must be a loop because i never don't know in advance how
      many cookies i need)
      But i get the error: "value of type string cannot be converted to
      system.web.http cookie"
      How can i give the value of the cookie?
      Thanks

      Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
      Dim i As Integer
      Dim cc(5) As String
      For i = 1 To 5
      allCookies.Add( cc(i), "test" & i)
      Next




      "Tim Patrick" <invalid@invali d.com.invalidsc hreef in bericht
      news:e3b469762e 408c8e1a20e2d96 d8@newsgroups.c omcast.net...
      If you are using VB2005, you can switch to one of the Generic collections
      that includes a key so that you can look up the cookie by name.
      >
      Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
      >
      Add new cookies to the dictionary this way:
      >
      allCookies.Add( cookieName, theCookieItself )
      >
      Access the cookie this way:
      >
      theRetrievedCoo kie = allCookies(cook ieName)
      >
      -----
      Tim Patrick - www.timaki.com
      Start-to-Finish Visual Basic 2005
      >
      >Hi,
      >>
      >I need several cookies depending of an variable (x), so i defined a
      >HttpCookie() as an array.
      >My problems:
      >1)I get the error: Object reference not set to an instance of an
      >object.
      >2)My second question is: how to request those cookies, because there
      >is no
      >name?
      >Thanks
      >André
      >I did:
      >x=5
      >Dim cok(x) As New HttpCookie 'can't give a name to the cookie
      >....
      >for k=1 to x
      >cok(k).Value = "cookie" & k
      >Response.Cooki es.Add(cok(k))
      >next
      >-----------------------------------
      >recovering the cookies?
      >for i=1 to x
      >cok(i) = Request.Cookies ("??")
      >next
      >
      >

      Comment

      • Tim Patrick

        #4
        Re: how to make cookies into an array?

        But you are trying to convert "test1" to a cookie. That is a string, not
        a cookie. You must specifically create an instance of HttpCookie, fill in
        its constructor or fields as needed, and then store it. I haven't used that
        object myself, but the code would be something like this.

        Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
        Dim oneCookie As HttpCookie
        Dim i As Integer

        For counter = 1 To 5
        oneCookie = New HttpCookie
        ' !!! Fill in all of oneCookie's fields here, then...
        allCookies.Add( "test" & counter, oneCookie)
        Next counter

        -----
        Tim Patrick - www.timaki.com
        Start-to-Finish Visual Basic 2005
        Hi,
        >
        thanks for replying ...
        >
        I tried this (it must be a loop because i never don't know in advance
        how
        many cookies i need)
        But i get the error: "value of type string cannot be converted to
        system.web.http cookie"
        How can i give the value of the cookie?
        Thanks
        Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
        Dim i As Integer
        Dim cc(5) As String
        For i = 1 To 5
        allCookies.Add( cc(i), "test" & i)
        Next
        "Tim Patrick" <invalid@invali d.com.invalidsc hreef in bericht
        news:e3b469762e 408c8e1a20e2d96 d8@newsgroups.c omcast.net...
        >
        >If you are using VB2005, you can switch to one of the Generic
        >collections that includes a key so that you can look up the cookie by
        >name.
        >>
        >Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
        >>
        >Add new cookies to the dictionary this way:
        >>
        >allCookies.Add (cookieName, theCookieItself )
        >>
        >Access the cookie this way:
        >>
        >theRetrievedCo okie = allCookies(cook ieName)
        >>
        >-----
        >Tim Patrick - www.timaki.com
        >Start-to-Finish Visual Basic 2005
        >>Hi,
        >>>
        >>I need several cookies depending of an variable (x), so i defined a
        >>HttpCookie( ) as an array.
        >>My problems:
        >>1)I get the error: Object reference not set to an instance of an
        >>object.
        >>2)My second question is: how to request those cookies, because there
        >>is no
        >>name?
        >>Thanks
        >>André
        >>I did:
        >>x=5
        >>Dim cok(x) As New HttpCookie 'can't give a name to the cookie
        >>....
        >>for k=1 to x
        >>cok(k).Valu e = "cookie" & k
        >>Response.Cook ies.Add(cok(k))
        >>next
        >>-----------------------------------
        >>recovering the cookies?
        >>for i=1 to x
        >>cok(i) = Request.Cookies ("??")
        >>next

        Comment

        • André

          #5
          Re: how to make cookies into an array?

          Ok, thanks

          "Tim Patrick" <invalid@invali d.com.invalidsc hreef in bericht
          news:e3b469762e 5c8c8e1b211b607 08@newsgroups.c omcast.net...
          But you are trying to convert "test1" to a cookie. That is a string, not a
          cookie. You must specifically create an instance of HttpCookie, fill in
          its constructor or fields as needed, and then store it. I haven't used
          that object myself, but the code would be something like this.
          >
          Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
          Dim oneCookie As HttpCookie
          Dim i As Integer
          >
          For counter = 1 To 5
          oneCookie = New HttpCookie
          ' !!! Fill in all of oneCookie's fields here, then...
          allCookies.Add( "test" & counter, oneCookie)
          Next counter
          >
          -----
          Tim Patrick - www.timaki.com
          Start-to-Finish Visual Basic 2005
          >
          >Hi,
          >>
          >thanks for replying ...
          >>
          >I tried this (it must be a loop because i never don't know in advance
          >how
          >many cookies i need)
          >But i get the error: "value of type string cannot be converted to
          >system.web.htt pcookie"
          >How can i give the value of the cookie?
          >Thanks
          >Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
          >Dim i As Integer
          >Dim cc(5) As String
          >For i = 1 To 5
          >allCookies.Add (cc(i), "test" & i)
          >Next
          >"Tim Patrick" <invalid@invali d.com.invalidsc hreef in bericht
          >news:e3b469762 e408c8e1a20e2d9 6d8@newsgroups. comcast.net...
          >>
          >>If you are using VB2005, you can switch to one of the Generic
          >>collections that includes a key so that you can look up the cookie by
          >>name.
          >>>
          >>Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
          >>>
          >>Add new cookies to the dictionary this way:
          >>>
          >>allCookies.Ad d(cookieName, theCookieItself )
          >>>
          >>Access the cookie this way:
          >>>
          >>theRetrievedC ookie = allCookies(cook ieName)
          >>>
          >>-----
          >>Tim Patrick - www.timaki.com
          >>Start-to-Finish Visual Basic 2005
          >>>Hi,
          >>>>
          >>>I need several cookies depending of an variable (x), so i defined a
          >>>HttpCookie () as an array.
          >>>My problems:
          >>>1)I get the error: Object reference not set to an instance of an
          >>>object.
          >>>2)My second question is: how to request those cookies, because there
          >>>is no
          >>>name?
          >>>Thanks
          >>>André
          >>>I did:
          >>>x=5
          >>>Dim cok(x) As New HttpCookie 'can't give a name to the cookie
          >>>....
          >>>for k=1 to x
          >>>cok(k).Val ue = "cookie" & k
          >>>Response.Coo kies.Add(cok(k) )
          >>>next
          >>>-----------------------------------
          >>>recovering the cookies?
          >>>for i=1 to x
          >>>cok(i) = Request.Cookies ("??")
          >>>next
          >
          >

          Comment

          • André

            #6
            Re: how to make cookies into an array?

            Tim, don't become angry (i'm learning), but it still doesn't work ...

            If i do what you wrote here, i get an error with the line:
            oneCookie = New HttpCookie

            the error is: "overload resolution failed because no accessible 'new'
            accepts this number of arguments"

            so i tried this (and the error is gone): oneCookie = New HttpCookie("ok" )
            the whole code is:
            Dim allCookies As New Generic.Diction ary(Of String, HttpCookie)
            Dim i As Integer
            Dim oneCookie As HttpCookie
            For i = 1 To 5
            oneCookie = New HttpCookie("ok" )
            oneCookie.Value = i
            allCookies.Add( "test" & i, oneCookie)
            Next

            But now, how can i retrieve the cookies? I did:

            Dim allCookies As New Generic.Diction ary(Of String, HttpCookie)
            Dim i As Integer
            Dim cc(5) As String
            Dim retCookie As HttpCookie
            For i = 1 To 5
            retCookie = allCookies("tes t" & i)
            cc(i) = retCookie.Value
            Response.Write( cc(i) & " ")
            Next

            And the error is now: "The given key was not present in the dictionary"
            Can you help me a last time? Thanks








            "Tim Patrick" <invalid@invali d.com.invalidsc hreef in bericht
            news:e3b469762e 5c8c8e1b211b607 08@newsgroups.c omcast.net...
            But you are trying to convert "test1" to a cookie. That is a string, not a
            cookie. You must specifically create an instance of HttpCookie, fill in
            its constructor or fields as needed, and then store it. I haven't used
            that object myself, but the code would be something like this.
            >
            Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
            Dim oneCookie As HttpCookie
            Dim i As Integer
            >
            For counter = 1 To 5
            oneCookie = New HttpCookie
            ' !!! Fill in all of oneCookie's fields here, then...
            allCookies.Add( "test" & counter, oneCookie)
            Next counter
            >
            -----
            Tim Patrick - www.timaki.com
            Start-to-Finish Visual Basic 2005
            >
            >Hi,
            >>
            >thanks for replying ...
            >>
            >I tried this (it must be a loop because i never don't know in advance
            >how
            >many cookies i need)
            >But i get the error: "value of type string cannot be converted to
            >system.web.htt pcookie"
            >How can i give the value of the cookie?
            >Thanks
            >Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
            >Dim i As Integer
            >Dim cc(5) As String
            >For i = 1 To 5
            >allCookies.Add (cc(i), "test" & i)
            >Next
            >"Tim Patrick" <invalid@invali d.com.invalidsc hreef in bericht
            >news:e3b469762 e408c8e1a20e2d9 6d8@newsgroups. comcast.net...
            >>
            >>If you are using VB2005, you can switch to one of the Generic
            >>collections that includes a key so that you can look up the cookie by
            >>name.
            >>>
            >>Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
            >>>
            >>Add new cookies to the dictionary this way:
            >>>
            >>allCookies.Ad d(cookieName, theCookieItself )
            >>>
            >>Access the cookie this way:
            >>>
            >>theRetrievedC ookie = allCookies(cook ieName)
            >>>
            >>-----
            >>Tim Patrick - www.timaki.com
            >>Start-to-Finish Visual Basic 2005
            >>>Hi,
            >>>>
            >>>I need several cookies depending of an variable (x), so i defined a
            >>>HttpCookie () as an array.
            >>>My problems:
            >>>1)I get the error: Object reference not set to an instance of an
            >>>object.
            >>>2)My second question is: how to request those cookies, because there
            >>>is no
            >>>name?
            >>>Thanks
            >>>André
            >>>I did:
            >>>x=5
            >>>Dim cok(x) As New HttpCookie 'can't give a name to the cookie
            >>>....
            >>>for k=1 to x
            >>>cok(k).Val ue = "cookie" & k
            >>>Response.Coo kies.Add(cok(k) )
            >>>next
            >>>-----------------------------------
            >>>recovering the cookies?
            >>>for i=1 to x
            >>>cok(i) = Request.Cookies ("??")
            >>>next
            >
            >

            Comment

            • Tim Patrick

              #7
              Re: how to make cookies into an array?

              Since I'm not sure of what you are doing, I'm not fully sure of the answer
              to give you. Cookies are normally only used in the context of a web page,
              and ASP.NET exposes two collections of cookies, one through the Request object
              and one through the Response object. I'm not sure why you want to create
              your own disconnected set of cookies.

              I would confirm that you have read up on the HttpCookie object and its related
              HttpCookieColle ction object (instead of Generic.Diction ary). HttpCookie has
              its own name, so it seems a collection-specified name isn't really necessary.

              Maybe if you could explain your purpose, it would make the problem easier
              to diagnose.

              -----
              Tim Patrick - www.timaki.com
              Start-to-Finish Visual Basic 2005
              Tim, don't become angry (i'm learning), but it still doesn't work ...
              >
              If i do what you wrote here, i get an error with the line: oneCookie =
              New HttpCookie
              >
              the error is: "overload resolution failed because no accessible 'new'
              accepts this number of arguments"
              >
              so i tried this (and the error is gone): oneCookie = New
              HttpCookie("ok" )
              the whole code is:
              Dim allCookies As New Generic.Diction ary(Of String, HttpCookie)
              Dim i As Integer
              Dim oneCookie As HttpCookie
              For i = 1 To 5
              oneCookie = New HttpCookie("ok" )
              oneCookie.Value = i
              allCookies.Add( "test" & i, oneCookie)
              Next
              But now, how can i retrieve the cookies? I did:
              >
              Dim allCookies As New Generic.Diction ary(Of String, HttpCookie)
              Dim i As Integer
              Dim cc(5) As String
              Dim retCookie As HttpCookie
              For i = 1 To 5
              retCookie = allCookies("tes t" & i)
              cc(i) = retCookie.Value
              Response.Write( cc(i) & " ")
              Next
              And the error is now: "The given key was not present in the
              dictionary" Can you help me a last time? Thanks
              >
              "Tim Patrick" <invalid@invali d.com.invalidsc hreef in bericht
              news:e3b469762e 5c8c8e1b211b607 08@newsgroups.c omcast.net...
              >
              >But you are trying to convert "test1" to a cookie. That is a string,
              >not a cookie. You must specifically create an instance of HttpCookie,
              >fill in its constructor or fields as needed, and then store it. I
              >haven't used that object myself, but the code would be something like
              >this.
              >>
              >Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
              >Dim oneCookie As HttpCookie
              >Dim i As Integer
              >For counter = 1 To 5
              >oneCookie = New HttpCookie
              >' !!! Fill in all of oneCookie's fields here, then...
              >allCookies.Add ("test" & counter, oneCookie)
              >Next counter
              >-----
              >Tim Patrick - www.timaki.com
              >Start-to-Finish Visual Basic 2005
              >>Hi,
              >>>
              >>thanks for replying ...
              >>>
              >>I tried this (it must be a loop because i never don't know in
              >>advance
              >>how
              >>many cookies i need)
              >>But i get the error: "value of type string cannot be converted to
              >>system.web.ht tpcookie"
              >>How can i give the value of the cookie?
              >>Thanks
              >>Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
              >>Dim i As Integer
              >>Dim cc(5) As String
              >>For i = 1 To 5
              >>allCookies.Ad d(cc(i), "test" & i)
              >>Next
              >>"Tim Patrick" <invalid@invali d.com.invalidsc hreef in bericht
              >>news:e3b46976 2e408c8e1a20e2d 96d8@newsgroups .comcast.net...
              >>>If you are using VB2005, you can switch to one of the Generic
              >>>collection s that includes a key so that you can look up the cookie
              >>>by name.
              >>>>
              >>>Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
              >>>>
              >>>Add new cookies to the dictionary this way:
              >>>>
              >>>allCookies.A dd(cookieName, theCookieItself )
              >>>>
              >>>Access the cookie this way:
              >>>>
              >>>theRetrieved Cookie = allCookies(cook ieName)
              >>>>
              >>>-----
              >>>Tim Patrick - www.timaki.com
              >>>Start-to-Finish Visual Basic 2005
              >>>>Hi,
              >>>>>
              >>>>I need several cookies depending of an variable (x), so i defined
              >>>>a
              >>>>HttpCookie( ) as an array.
              >>>>My problems:
              >>>>1)I get the error: Object reference not set to an instance of an
              >>>>object.
              >>>>2)My second question is: how to request those cookies, because
              >>>>there
              >>>>is no
              >>>>name?
              >>>>Thanks
              >>>>André
              >>>>I did:
              >>>>x=5
              >>>>Dim cok(x) As New HttpCookie 'can't give a name to the
              >>>>cookie
              >>>>....
              >>>>for k=1 to x
              >>>>cok(k).Valu e = "cookie" & k
              >>>>Response.Co okies.Add(cok(k ))
              >>>>next
              >>>>-----------------------------------
              >>>>recoverin g the cookies?
              >>>>for i=1 to x
              >>>>cok(i) = Request.Cookies ("??")
              >>>>next

              Comment

              • André

                #8
                Re: how to make cookies into an array?

                Tim, it's ok, i think i understand it now ...


                "Tim Patrick" <invalid@invali d.com.invalidsc hreef in bericht
                news:e3b469762e ae8c8e1ef3d9c86 7a@newsgroups.c omcast.net...
                Since I'm not sure of what you are doing, I'm not fully sure of the answer
                to give you. Cookies are normally only used in the context of a web page,
                and ASP.NET exposes two collections of cookies, one through the Request
                object and one through the Response object. I'm not sure why you want to
                create your own disconnected set of cookies.
                >
                I would confirm that you have read up on the HttpCookie object and its
                related HttpCookieColle ction object (instead of Generic.Diction ary).
                HttpCookie has its own name, so it seems a collection-specified name isn't
                really necessary.
                >
                Maybe if you could explain your purpose, it would make the problem easier
                to diagnose.
                >
                -----
                Tim Patrick - www.timaki.com
                Start-to-Finish Visual Basic 2005
                >
                >Tim, don't become angry (i'm learning), but it still doesn't work ...
                >>
                >If i do what you wrote here, i get an error with the line: oneCookie =
                >New HttpCookie
                >>
                >the error is: "overload resolution failed because no accessible 'new'
                >accepts this number of arguments"
                >>
                >so i tried this (and the error is gone): oneCookie = New
                >HttpCookie("ok ")
                >the whole code is:
                >Dim allCookies As New Generic.Diction ary(Of String, HttpCookie)
                >Dim i As Integer
                >Dim oneCookie As HttpCookie
                >For i = 1 To 5
                >oneCookie = New HttpCookie("ok" )
                >oneCookie.Valu e = i
                >allCookies.Add ("test" & i, oneCookie)
                >Next
                >But now, how can i retrieve the cookies? I did:
                >>
                >Dim allCookies As New Generic.Diction ary(Of String, HttpCookie)
                >Dim i As Integer
                >Dim cc(5) As String
                >Dim retCookie As HttpCookie
                >For i = 1 To 5
                >retCookie = allCookies("tes t" & i)
                >cc(i) = retCookie.Value
                >Response.Write (cc(i) & " ")
                >Next
                >And the error is now: "The given key was not present in the
                >dictionary" Can you help me a last time? Thanks
                >>
                >"Tim Patrick" <invalid@invali d.com.invalidsc hreef in bericht
                >news:e3b469762 e5c8c8e1b211b60 708@newsgroups. comcast.net...
                >>
                >>But you are trying to convert "test1" to a cookie. That is a string,
                >>not a cookie. You must specifically create an instance of HttpCookie,
                >>fill in its constructor or fields as needed, and then store it. I
                >>haven't used that object myself, but the code would be something like
                >>this.
                >>>
                >>Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
                >>Dim oneCookie As HttpCookie
                >>Dim i As Integer
                >>For counter = 1 To 5
                >>oneCookie = New HttpCookie
                >>' !!! Fill in all of oneCookie's fields here, then...
                >>allCookies.Ad d("test" & counter, oneCookie)
                >>Next counter
                >>-----
                >>Tim Patrick - www.timaki.com
                >>Start-to-Finish Visual Basic 2005
                >>>Hi,
                >>>>
                >>>thanks for replying ...
                >>>>
                >>>I tried this (it must be a loop because i never don't know in
                >>>advance
                >>>how
                >>>many cookies i need)
                >>>But i get the error: "value of type string cannot be converted to
                >>>system.web.h ttpcookie"
                >>>How can i give the value of the cookie?
                >>>Thanks
                >>>Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
                >>>Dim i As Integer
                >>>Dim cc(5) As String
                >>>For i = 1 To 5
                >>>allCookies.A dd(cc(i), "test" & i)
                >>>Next
                >>>"Tim Patrick" <invalid@invali d.com.invalidsc hreef in bericht
                >>>news:e3b4697 62e408c8e1a20e2 d96d8@newsgroup s.comcast.net.. .
                >>>>If you are using VB2005, you can switch to one of the Generic
                >>>>collectio ns that includes a key so that you can look up the cookie
                >>>>by name.
                >>>>>
                >>>>Dim allCookies As Generic.Diction ary(Of String, HttpCookie)
                >>>>>
                >>>>Add new cookies to the dictionary this way:
                >>>>>
                >>>>allCookies. Add(cookieName, theCookieItself )
                >>>>>
                >>>>Access the cookie this way:
                >>>>>
                >>>>theRetrieve dCookie = allCookies(cook ieName)
                >>>>>
                >>>>-----
                >>>>Tim Patrick - www.timaki.com
                >>>>Start-to-Finish Visual Basic 2005
                >>>>>Hi,
                >>>>>>
                >>>>>I need several cookies depending of an variable (x), so i defined
                >>>>>a
                >>>>>HttpCookie () as an array.
                >>>>>My problems:
                >>>>>1)I get the error: Object reference not set to an instance of an
                >>>>>object.
                >>>>>2)My second question is: how to request those cookies, because
                >>>>>there
                >>>>>is no
                >>>>>name?
                >>>>>Thanks
                >>>>>André
                >>>>>I did:
                >>>>>x=5
                >>>>>Dim cok(x) As New HttpCookie 'can't give a name to the
                >>>>>cookie
                >>>>>....
                >>>>>for k=1 to x
                >>>>>cok(k).Val ue = "cookie" & k
                >>>>>Response.C ookies.Add(cok( k))
                >>>>>next
                >>>>>-----------------------------------
                >>>>>recoveri ng the cookies?
                >>>>>for i=1 to x
                >>>>>cok(i) = Request.Cookies ("??")
                >>>>>next
                >
                >

                Comment

                Working...