Home All Groups Group Topic Archive Search About

c# to vb.net conversion

Author
21 Jun 2006 10:17 AM
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!!!

Author
21 Jun 2006 10:34 AM
Herfried K. Wagner [MVP]
"mcateer77" <bmcat***@gmail.com> schrieb:
> 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 S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
21 Jun 2006 10:55 AM
mcateer77
Any idea why I'm getting this error?

Thks

Herfried K. Wagner [MVP] wrote:
Show quoteHide quote
> "mcateer77" <bmcat***@gmail.com> schrieb:
> > 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 S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
Author
21 Jun 2006 10:58 AM
Herfried K. Wagner [MVP]
"mcateer77" <bmcat***@gmail.com> schrieb:
> Any idea why I'm getting this error?

Could you post the complete exception message?

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
21 Jun 2006 11:15 AM
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!
Author
21 Jun 2006 10:29 PM
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

Show quoteHide quote
> 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!!!
>
Author
21 Jun 2006 11:01 PM
Herfried K. Wagner [MVP]
"GhostInAK" <ghosti***@gmail.com> schrieb:
> Your translation is wrong.  You can't Dim uri As Uri.

'Dim uri As Uri' is actually allowed in VB.NET.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
21 Jun 2006 11:09 PM
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

Show quoteHide quote
> "GhostInAK" <ghosti***@gmail.com> schrieb:
>
>> Your translation is wrong.  You can't Dim uri As Uri.
>>
> 'Dim uri As Uri' is actually allowed in VB.NET.
>
Author
21 Jun 2006 11:25 PM
Herfried K. Wagner [MVP]
"GhostInAK" <ghosti***@gmail.com> schrieb:
> Seriously??  Then how the hell does VB figure out if you are talking about
> the class or the variable?
>
>>> Your translation is wrong.  You can't Dim uri As Uri.
>>>
>> 'Dim uri As Uri' is actually allowed in VB.NET.

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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
21 Jun 2006 11:36 PM
GhostInAK
Hello Herfried K. Wagner [MVP],

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

Thanks Herfried.

-Boo

Show quoteHide quote
> "GhostInAK" <ghosti***@gmail.com> schrieb:
>
>> Seriously??  Then how the hell does VB figure out if you are talking
>> about the class or the variable?
>>
>>>> Your translation is wrong.  You can't Dim uri As Uri.
>>>>
>>> 'Dim uri As Uri' is actually allowed in VB.NET.
>>>
> 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.
>