cQueryString Class to resolve Read Only problem

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

    cQueryString Class to resolve Read Only problem

    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.Collecti ons.Specialized

    Public Class cQueryString
    Inherits NameValueCollec tion

    Public Sub New(ByVal QueryString As NameValueCollec tion)

    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.Str ingBuilder()

    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.ToStri ng
    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(Re quest.QueryStri ng)

    objQueryString. Add("SomeKey", "SomeValue" , True)
    objQueryString. Add("AnotherNee dedKey", "AnotherVal ue", True)
    objQueryString. Remove("NoLonge rNeededKey")

    Response.Redire ct("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(Re quest.QueryStri ng)
    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 ", "SecondItemValu e", True)
    objQueryString. Add("ThirdItem" , "ThirdItemValue ", True)

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

    '-- Hopefully a little more readible then piling stuff at the end
    '-- of a redirect.
Working...