c# to vb.net conversion

M

mcateer77

This is driving me insane!
I'm trying to solve "The underlying connection was closed" issue when
using WSE 3.0 in VS2005.
I'm trying to convert the following c# code to vb.net:

(This is from:
http://weblogs.asp.net/jan/archive/2004/05/08/128394.aspx)

Using System.Net;
Using System.Reflection;

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
PropertyInfo requestPropertyInfo = null;
WebRequest request = base.GetWebRequest(uri);
if (requestPropertyInfo==null)
requestPropertyInfo = request.GetType().GetProperty("Request");
// Retrieve underlying web request
HttpWebRequest webRequest =
(HttpWebRequest)requestPropertyInfo.GetValue(request, null);
webRequest.KeepAlive = false;
return request;
}


I've done this so far:

Imports System.Net
Imports System.Reflection

Protected Overloads Overrides Function GetWebRequest(ByVal uri As Uri)
As System.Net.WebRequest
Dim requestPropertyInfo As PropertyInfo = Nothing
Dim request As WebRequest = MyBase.GetWebRequest(uri)
If requestPropertyInfo Is Nothing Then
requestPropertyInfo =
request.GetType.GetProperty("Request")
End If
Dim webRequest As HttpWebRequest =
CType(requestPropertyInfo.GetValue(request, Nothing), HttpWebRequest)
webRequest.KeepAlive = False
Return request
End Function

When I run this i get the following error:

"System.NullReferenceException: Object reference not set to an instance
of an object."

This occurs on the following line:

Dim webRequest As HttpWebRequest =
CType(requestPropertyInfo.GetValue(request, Nothing), HttpWebRequest)

I think this is due to the "Nothing" being passed???

Does anyone know how to convert this? I've tried everything!
The parameter details are "index() As Object".

Any help would be much appreciated!!!
 
H

Herfried K. Wagner [MVP]

mcateer77 said:
I'm trying to solve "The underlying connection was closed" issue when
using WSE 3.0 in VS2005.
I'm trying to convert the following c# code to vb.net:

Your translation seems to be correct.
 
M

mcateer77

Here you go:

System.NullReferenceException: Object reference not set to an instance
of an object.
at DPS.dpsauthentication.dpsauthentication.GetWebRequest(Uri uri) in
C:\Documents and Settings\brianmcateer.BRANDON\My Documents\Visual
Studio Projects\DPS\dpsauthentication.vb:line 98
at
System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String
methodName, Object[] parameters)
at DPS.dpsauthentication.dpsauthentication.DPSrequestToken(Int32
version, String vendorID) in C:\Documents and
Settings\brianmcateer.BRANDON\My Documents\Visual Studio
Projects\DPS\dpsauthentication.vb:line 49
at DPS.Form1.Button1_Click(Object sender, EventArgs e) in
C:\Documents and Settings\brianmcateer.BRANDON\My Documents\Visual
Studio Projects\DPS\Form1.vb:line 94

Thanks!
 
G

GhostInAK

Hello mcateer77,

Your translation is wrong. You can't Dim uri As Uri.

Here's the correct translation:

Imports System
Imports System.Net
Imports System.Reflection



Protected Overrides Function GetWebRequest(ByVal tURI As Uri) As System.Net.WebRequest

Dim tPropertyInfo As PropertyInfo = Nothing
Dim tRequest As WebRequest = MyBase.GetWebRequest(tURI)
Dim tRequestToo As WebRequest = Nothing

If Nothing Is tPropertyInfo Then
tPropertyInfo = tRequest.GetType.GetProperty("Request")
End If
tRequestToo = tPropertyInfo.GetValue(tRequest, Nothing)
tRequestToo.KeepAlive = False

Return tRequest

End Function

-Boo
 
G

GhostInAK

Hello Herfried K. Wagner [MVP],

Seriously?? Then how the hell does VB figure out if you are talking about
the class or the variable?

-Boo
 
H

Herfried K. Wagner [MVP]

GhostInAK said:
Seriously?? Then how the hell does VB figure out if you are talking about
the class or the variable?

Well, the class doesn't have instance members and the object doesn't have
shared members, so there is no ambiguity the compiler could not resolve.
 
G

GhostInAK

Hello Herfried K. Wagner [MVP],

Wow. That's so.. wrong. It makes me sad. I wanna cry.

Thanks Herfried.

-Boo
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top