Reputation: 2241
I've been coding in VB for quite awhile and I can do plenty in C# and F# as well, but one syntax difference between VB and C# continues to throw me off. I'd Google this, but I'm not sure what to call it exactly. Consider the following examples:
In visual basic I would do this:
Dim Request As HttpWebRequest = HttpWebRequest.Create("www.google.com")
However, when I make what seems to be the "logical" conversion to C#:
HttpWebRequest Request = HttpWebRequest.Create("www.google.com");
I get the implicit type conversion error. After looking at some other code I realized this seems to be the proper way to do this:
HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create("www.google.com");
But I'm not exactly clear on what purpose the additional mention of the HttpWebRequest type in the parenthesis accomplishes. Is this some sort of casting syntax I didn't know about? What's going on here that makes this work, and the direct conversion not?
Upvotes: 5
Views: 3143
Reputation: 43743
As others have said, the HttpWebRequest.Create
method does not return an object cast as an HttpWebRequest
type. Instead, it returns an HttpWebRequest
object cast as its base WebRequest
type. As you have discovered, in C#, you are required to explicitly cast the object to the desired type.
Your confusion, however, is that you assume that you don't need to perform the cast operation in VB.NET. The only reason that you don't need to cast in VB.NET is because you have Option Strict Off
. If you turned Option Strict On
in VB.NET, as you most likely should, then you would be required to perform the cast, just as in C#:
Dim Request As HttpWebRequest = DirectCast(HttpWebRequest.Create("www.google.com"), HttpWebRequest)
This is the compiler error as displayed by Visual Studio when you have Option Strict On
:
Option Strict On disallows implicit conversions from 'System.Net.WebRequest' to 'System.Net.HttpWebRequest'.
When you have Option Strict Off
, VB.NET will perform the widening conversion for you without any warning. This can be convenient, but it shuts off some of the compiler type-checking which could catch some bugs for you. My rule of thumb is to always turn Option Strict On
except in places where you need it Off
.
In other words, by turning Option Strict On
, as is recommended by most people, VB.NET's type-checking works the same as C#'s. If you don't want to have to cast the object in C#, the you could accomplish similar things by using the var
or dynamic
keywords, but I wouldn't recommend it in most cases. Type-checking is your friend. The fact that the syntax for type-casting in C# is easier just makes it all the more convenient.
Upvotes: 3
Reputation: 2793
You can use:
var Request = HttpWebRequest.Create("www.google.com") as HttpWebRequest;
Upvotes: 1
Reputation: 46977
This is due to Option Strict
being set to off
in your VB project. If you turn it on
implicit narrowing conversions will give compilation errors just as it does in C#. And you would have to write the following in VB:
Dim Request As HttpWebRequest = DirectCast(HttpWebRequest.Create("www.google.com"), HttpWebRequest)
Which is equal to C#'s
HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create("www.google.com");
Upvotes: 1
Reputation: 31204
HttpWebRequest.Create
returns a WebRequest
.
HttpWebRequest.Create
does happen to return a HttpWebRequest
which derives from WebRequest
, but compiler doesn't know that it's actually a HttpWebRequest
, but it does know that you're trying to put it into a variable of type HttpWebRequest
.
That's why you have to explicitly type it.
Upvotes: 2
Reputation: 61369
For starters, yes, that syntax is an explicit-cast (sometimes called a C-Style cast).
The reason you need it here is pretty simple. The Create
method is actually inherited from the WebRequest
class, and it returns a WebRequest
(not HttpWebRequest
) object. Note its lack of existence in HttpWebRequest
and the signature of Create
Assigning to a HttpWebRequest
variable then requires downcasting, which is never guaranteed to be safe, so you have to explicitly cast it.
Note that the following code would also compile.
WebRequest Request = HttpWebRequest.Create("www.google.com");
Upvotes: 5