cQueryString Class to resolve Read Only problem

Discussion in 'ASP .Net' started by ScriptNFool, Jun 30, 2003.

  1. ScriptNFool

    ScriptNFool Guest

    Ok... I've mooched off this group for a while now. Time to give some
    code back.

    Here's a class I wrote to solve the problem of handling query strings.
    I use it for an application where the page needs to preserve the
    existing QueryString, but add or remove a couple items and then
    redirect it back to the same page or other locations.

    ....A good example of how polymorphism can be useful for us VB'ers who
    are less familiar with the concept.

    -SF

    Here's the class *****

    Imports System.Collections.Specialized

    Public Class cQueryString
    Inherits NameValueCollection

    Public Sub New(ByVal QueryString As NameValueCollection)

    Dim i As Integer
    With QueryString
    '--Cycle through Items and Add them back
    For i = 0 To .Count - 1
    Me.Add(.GetKey(i), .Item(i))
    Next
    End With
    End Sub

    Public Overloads Sub Add(ByVal Name As String, ByVal Value As
    String, ByVal Overwrite As Boolean)
    If Name = "" Then Exit Sub

    If Overwrite = True Then
    Me.Remove(Name)
    Me.Add(Name, Value)
    Else
    Me.Add(Name, Value)
    End If
    End Sub

    Public Overrides Function ToString() As String
    Dim i As Integer
    Dim sbOutput As New System.Text.StringBuilder()

    If Me.HasKeys = False Then
    '--Return Blank String if Empty QueryString
    Return ""
    End If

    sbOutput.Append("?")

    With Me
    '--Cycle through Items and Add them back
    For i = 0 To .Count - 1
    sbOutput.Append(.GetKey(i) & "=" & .Item(i))
    If i <> .Count - 1 Then
    sbOutput.Append("&")
    End If
    Next
    End With

    Return sbOutput.ToString
    End Function

    Protected Overrides Sub Finalize()
    MyBase.Finalize()
    End Sub
    End Class


    And here's how I use it in my code *****

    Dim objQueryString As New cQueryString(Request.QueryString)

    objQueryString.Add("SomeKey", "SomeValue", True)
    objQueryString.Add("AnotherNeededKey", "AnotherValue", True)
    objQueryString.Remove("NoLongerNeededKey")

    Response.Redirect("http://www.weblocation.com/myweb" &
    objQueryString.ToString)

    Another way to use it *****
    '-- It's just as easy to clear out all the strings, and add the ones
    you want.
    Dim objQueryString As New cQueryString(Request.QueryString)
    objQueryString.Clear

    '-- Just as easy to Add together at once,
    '-- or intersperse throughout a procedure as stuff gets determined.
    objQueryString.Add("FirstItem", "FirstItemValue", True)
    objQueryString.Add("SecondItem", "SecondItemValue", True)
    objQueryString.Add("ThirdItem", "ThirdItemValue", True)

    Response.Redirect("http://www.weblocation.com/myweb" &
    objQueryString.ToString)

    '-- Hopefully a little more readible then piling stuff at the end
    '-- of a redirect.
     
    ScriptNFool, Jun 30, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. keithb
    Replies:
    2
    Views:
    8,051
    keithb
    Jun 7, 2006
  2. Replies:
    1
    Views:
    10,312
  3. steve yee
    Replies:
    5
    Views:
    362
    Bo Persson
    Jul 1, 2008
  4. A
    Replies:
    1
    Views:
    329
    Ian Collins
    Nov 3, 2010
  5. Gancy
    Replies:
    4
    Views:
    219
    Gunnar Hjalmarsson
    Feb 9, 2005
Loading...

Share This Page