Conerting String into Collection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apurvaG
    New Member
    • Apr 2007
    • 14

    #1

    Conerting String into Collection

    Hi All,

    Can I use "DirectCast " or "Ctype" to convert a string into NameValueCollec tion in VB.Net?

    Example I want to convert the string "a=200&b=30 0" to NameValueCollec tion class using inbuilt method.

    Don't want to go for split option.

    Please let me know ASAP

    Apurva G
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    No.
    Split would be the prefered method. If there were a method, it would probably use Split().

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      I provided a method for this in the following thread:



      It still relies on the Split() function, however, as far as your procedural code goes, it might be a little clearer. I built a class called NameValueCollec tionEx that takes a string in the format you specified after which it functions as if it were a regular NameValueCollec tion object...with the exception that you can then return all the items in the collection as another data string.

      [Code=vbnet]Dim oNVC As New NameValueCollec tionEx("Value1= 1&Value2=2&Valu e3=3")

      'You can then reference items in the collection like:
      Dim Val1 As String = oNVC("Value1")
      Dim Val2 As String = oNVC("Value2")
      Dim Val3 As String = oNVC("Value3")

      'Viewing items in the collection as a NameValue string:
      Dim sOut As String = oNVC.DataString
      'Output would be
      ' "Value1=New Value&Value2=2& Value3=3"

      'Modifying items
      oNVC("Value1") = "New Value"

      'Removing items:
      oNVC.Item("Valu e2").Remove
      Dim sOut As String = oNVC.DataString
      ' Output would be
      ' "Value1=New Value&Value3=3"

      'Adding items:
      oNVC.Add("Value 1", "FirstItem" )
      oNVC.Add("Value 2", "SecondItem ")
      oNVC.Add("Value 3", "ThirdItem)[/Code]

      Comment

      Working...