c# beginner

  • Thread starter Thread starter hharry
  • Start date Start date
"hharry"
hello all,

switching to c# from vb.net and had quick syntax question...

in vb:

Dim httpRequest as HttpWebRequest
httpRequest = WebRequest.Create( _
"http://www.winisp.net/goodrich/default.htm")

in c#:

HttpWebRequest httpRequest;

httpRequest = (HttpWebRequest)
WebRequest.Create("http://www.winisp.net/goodrich/default.htm");

================================================

what is the purpose of (HttpWebRequest) and why the ellipsis ?


thanks in advance

That's a typecast. Probably the WebRequest.Create does not return a
HttpWebRequest (but a polymorphic type for example) thus the need for the
cast.
 
I'm not sure where you got the idea that the C# code you provided was *the*
C# code for what you're doing. This would work just as well:

HttpWebRequest httpRequest;
httpRequest =
WebRequest.Create("http://www.winisp.net/goodrich/default.htm");

-or -

HttpWebRequest httpRequest =
WebRequest.Create("http://www.winisp.net/goodrich/default.htm");

The "(HttpWebRequest)" is a cast. It is casting the result of the method as
an HttpWebRequest. However, it is unnecessary in this case. The
WebRequest.Create method returns an instance of one of the classes that
inherit WebRequest, depending upon the URL passed to it. In this case, it
returns an HttpWebRequest, so it is not necessary to cast it.

The equivalent in VB would be:

httpRequest = CType(WebRequest.Create( _
"http://www.winisp.net/goodrich/default.htm"), HttpWebRequest)

And as you've already observed, that is not necessary.

It is important to note that while VB.Net and C# are doing the same things
under the covers (they both compile to MSIL), the syntax and process may
differ from time to time. For example, in C#, there is a distinct different
between casting and converting (which is actually true). VB.Net hides this
by using the CType() (and related) methods, which are conversion methods.

The difference between casting and converting is a bit murky. Basically,
casting in C# is done when an instance of a class is of the same type or an
inherited type of another class. For example, take the ICloneable interface.
This interface defines a single method (Clone()) which is defined as
returning a type of Object. This is because it can be implemented by any
class, and the interface cannot know what type of class it will be
implemented on. So, in the implementation of ICloneable for any class, the
return type is going to be object. However, as the Clone method makes a copy
of a class, it will return an instance of the cloned class. Because of
strong typing, you must explicitly cast the returned instance as the class
that it (already)is:

Bitmap bmp = Bitmap.FromFile(filePath)
Bitmap bmpClone = (Bitmap)bmp.Clone();

Otherwise, it will be treated as an Object (which it is), but the extra
members of the Bitmap class will not be available, as they are not part of
the Object class. In other words, an object is what it is, but for other
objects and code to know how to work with it, they have to know what it is
also. Casting is how they are told.

Conversion is used when the types are not inherited from one another, but
can be converted with a little work. For example, an Int32 and a String are
2 entirely different types, with little in common. But an integer can always
be represented as a string, so you can convert an integer into a string,
using the ToString method, or the Convert class.

Another difference is that converting does not return the original object,
but an object created by the conversion. Casting simply puts a "sign" on the
object that advertises how it is to be treated.

As for the "ellipsis," I didn't see any. An ellipsis is a series of several
marks that indicates an omission in text. Did you mean parentheses? If so,
you should get used to the fact that C# comes from a family of languages
(the C family) which are well-known for being succinct (compact). The
parentheses in this case simply indicate a cast.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
 
That's a typecast. Probably the WebRequest.Create does not return a
HttpWebRequest (but a polymorphic type for example) thus the need for the
cast.

Actually, the method will return an HttpWebRequest if an HTTP URL is passed
to it. The cast is unnecessary.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
 
Hi,


Kevin Spencer said:
Actually, the method will return an HttpWebRequest if an HTTP URL is
passed to it. The cast is unnecessary.

Nop, Create ALWAYS return a WebRequest reference.
Now IF the passed string start with http:// it will create an instance of
HttpWebRequest. but it will be returned as a WebRequest ( the parent
abstract class). That is why the cast is needed.
 
Hi,

Kevin Spencer said:
I'm not sure where you got the idea that the C# code you provided was
*the* C# code for what you're doing. This would work just as well:

HttpWebRequest httpRequest;
httpRequest =
WebRequest.Create("http://www.winisp.net/goodrich/default.htm");

-or -

HttpWebRequest httpRequest =
WebRequest.Create("http://www.winisp.net/goodrich/default.htm");

It will not work, Create return a WebRequest

From MSDN:
The Create method returns a descendant of the WebRequest class determined at
run time as the closest registered match for requestUri.

For example, when a URI beginning with http:// is passed in requestUri, an
HttpWebRequest is returned by Create. If a URI beginning with file:// is
passed instead, the Create method will return a FileWebRequest instance.

The .NET Framework includes support for the http://, https://, and file://
URI schemes. Custom WebRequest descendants to handle other requests are
registered with the RegisterPrefix method.
 
You need to re-read what you just posted.
From MSDN:
The Create method returns a descendant of the WebRequest class determined
at run time as the closest registered match for requestUri.

Note that it says "a descendant of the WebRequest class determined at run
time as the closest registered match for the requestUri." Of course it
returns a WebRequest. You might also say that it returns an Object. But it
more specifically returns the *descendant* of WebRequest that is the closest
match to the URI passed. So, no casting is needed.

Still don't believe me? Here's another quote:

"Requests are sent from an application to a particular URI, such as a Web
page on a server. The URI determines the proper descendant class to create
from a list of WebRequest descendants registered for the application.
WebRequest descendants are typically registered to handle a specific
protocol, such as HTTP or FTP, but can be registered to handle a request to
a specific server or path on a server."

http://msdn2.microsoft.com/en-us/library/system.net.webrequest.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
 
Kevin Spencer said:
You need to re-read what you just posted.

You need to try compiling the code you've posted :)
Note that it says "a descendant of the WebRequest class determined at run
time as the closest registered match for the requestUri." Of course it
returns a WebRequest. You might also say that it returns an Object. But it
more specifically returns the *descendant* of WebRequest that is the closest
match to the URI passed. So, no casting is needed.

The cast is needed because the compiler can't read the MSDN
documentation. The cast tells the compiler that we *know* that the
reference returned will actuall refer to an HttpWebRequest, even though
the WebRequest.Create signature only guarantees that it will be a
WebRequest. It's the same reason we need to cast the results of
fetching from an ArrayList or Hashtable (as opposed to the generics in
2.0).

Here's a short but complete program demonstrating the problem:

using System;
using System.Net;

class Test
{
static void Main()
{
HttpWebRequest req = WebRequest.Create
("http://www.slashdot.org");
}
}

Test.cs(8,30): error CS0266: Cannot implicitly convert type
'System.Net.WebRequest' to 'System.Net.HttpWebRequest'. An
explicit conversion exists (are you missing a cast?)

Put the cast in and it compiles fine.
 
You need to try compiling the code you've posted :)

That's odd, Jon. You're correct, of course. The odd part is that I would
have thought what you (and Ignacio) said was true, and in fact, I couldn't
think of any way that a cast would *not* be needed, but I could almost swear
that I saw a Microsoft example that did *not* use a cast. Going back, I
can't find it. :P

Sorry for any confusion.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
 

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

Back
Top