HttpServerUtility contains a public function called UrlEncode. It is not a shared function. HttpServerUtility does not have any public constructors.
Doing this fails:
Dim encodeMe As String = "a string to be encoded!"
HttpServerUtility.UrlEncode(encodeMe) 'Bombs out
This works, and is how Microsoft says to do it:
Dim instance As HttpServerUtility
Dim encodeMe As String = "a string to be encoded!"
instance.UrlEncode(encodeMe ) 'Works!
How did they accomplish this? You can’t instantiate an instance of it using a constructor, yet you can’t access UrlEncode by just referencing HttpServerUtility.UrlEncode.
EDIT: While I thoroughly enjoyed everyone getting into a big OO debate, I believe the problem is faulty MSDN documentation. The line “Dim instance As HttpServerUtility” should read “Dim instance As HttpServerUtility = Context.Server” The code which I included (which is from the MSDN documentation) does not actually work, and instead throws a null reference exception – just as you’d expect. Thank you, Jason!
Are you sure this works?
This will give you a
NullReferenceExceptionat runtime (and the compiler will give you a warning thatinstanceis not being assigned to). Seriously, Microsoft didn’t do anything here. The above code is disastrously wrong and will die at runtime.And you can’t do this
because
UrlEncodeis not defined as aSharedmethod inHttpServerUtility.You need a non-null instance of
HttpServerUtility. The right way to useHttpServerUtilityis like this:Another option is to just use
HttpUtilityfor which there is aSharedmethodHttpUtility.UrlEncode: