What does Rob Conery have against strings?

by Mikael Henriksson 12. November 2009 17:08

Well I sort of get it can be a pain in the ass to have to deal with strings. But what if you also have to deal with VB.NET at the same time? I just have to show this nice little helper I have.

Public Function ConvertToDictionary(ByVal input As String) As IDictionary(Of String, String)
	Dim dic As New Dictionary(Of String, String)
	Dim pairsplitter As Char = Char.Parse("&")
	Dim keysplitter As Char = Char.Parse("=")
	Dim stringArray As String() = input.Split(pairsplitter)

	If (input.Length > 0) Then
		If (stringArray(0).Contains("?")) Then
			Dim tmp As String = stringArray(0)
			stringArray(0) = tmp.Remove(0, tmp.IndexOf("?") + 1)
		End If
	End If

	For Each keyvalpair As String In stringArray
		Dim keyarray As String() = keyvalpair.Split(keysplitter)
		dic.Add(keyarray(0), keyarray(1))
	Next

	Return dic
End Function

The fun part is when you have created your unit test and it succeeds. The boring part is revisiting such a bastard after a year trying to figure out what the hell you where doing there in the first place. Oh and to answer your question. The reason for all this is that sometimes the input stream contains the page.aspx? and sometimes it just starts with the key-value pairs and since I don’t want the page.aspx? as a key in the dictionary I remove it and replace whatever is in array(0) :) Fun times…

Tags:

vb.net

blog comments powered by Disqus

About the author

Life architect specialized in programming

Month List

Widget Twitter not found.

Root element is missing.X