This documentation is archived and is not being maintained.

HttpCookieCollection.GetKey Method

Returns the key (name) of the cookie at the specified numerical index.

[Visual Basic]
Public Function GetKey( _
   ByVal index As Integer _
) As String
[C#]
public string GetKey(
 int index
);
[C++]
public: String* GetKey(
 int index
);
[JScript]
public function GetKey(
   index : int
) : String;

Parameters

index
The index of the key to retrieve from the collection.

Return Value

The name of the cookie specified by index.

Example

The following example returns each cookie from the cookie collection, checks whether it is named "LastVisit", and, If "LastVisit" is found, updates its value to the current date and time.

[Visual Basic] 
Dim loop1 As Integer
 Dim MyCookie As HttpCookie
 Dim MyCookieCollection As HttpCookieCollection = Request.Cookies
 
 For loop1 = 0 To MyCookieCollection.Count - 1
    If MyCookieCollection.GetKey(loop1) = "LastVisit" Then
       MyCookieCollection(loop1).Value = DateTime.Now().ToString()
       MyCookieCollection.Set(MyCookieCollection(loop1))
       Exit For
    End If
 Next loop1
    

[C#] 
int loop1;

 HttpCookieCollection MyCookieCollection = Response.Cookies;
 
 for(loop1 = 0; loop1 < MyCookieCollection.Count; loop1++)
 {
    if(MyCookieCollection.GetKey(loop1) == "LastVisit")
    {
       MyCookieCollection[loop1].Value = DateTime.Now.ToString();
       MyCookieCollection.Set(MyCookieCollection[loop1]);
    }
 }
    

[C++] 
int loop1;

 HttpCookieCollection* MyCookieCollection = Response->Cookies;
 
 for(loop1 = 0; loop1 < MyCookieCollection->Count; loop1++)
 {
    if(MyCookieCollection->GetKey(loop1)->Equals(S"LastVisit"))
    {
       MyCookieCollection->Item[loop1]->Value = DateTime::Now.ToString();
       MyCookieCollection->Set(MyCookieCollection->Item[loop1]);
    }
 }
    

[JScript] 
var myCookie : HttpCookie
var myCookieCollection : HttpCookieCollection = Request.Cookies

for(var i= 0; i < myCookieCollection.Count; i++){
  if(myCookieCollection.GetKey(i) == "LastVisit"){
      myCookieCollection(i).Value = DateTime.Now.ToString()
      myCookieCollection.Set(myCookieCollection(i))
      break
  }
}
  

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

HttpCookieCollection Class | HttpCookieCollection Members | System.Web Namespace

Show: