473,322 Members | 1,241 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

c# beginner

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

Jul 13 '06 #1
9 5502
"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.

Jul 13 '06 #2
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.
"Padu" <pa**@merlotti.comwrote in message
news:75******************************@iswest.net.. .
"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.

Jul 14 '06 #3
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.
"hharry" <pa*********@nyc.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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

Jul 14 '06 #4
Hi,
"Kevin Spencer" <uc*@ftc.govwrote in message
news:OG****************@TK2MSFTNGP05.phx.gbl...
>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.
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.

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jul 14 '06 #5
Hi,

"Kevin Spencer" <uc*@ftc.govwrote in message
news:OI**************@TK2MSFTNGP04.phx.gbl...
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.


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jul 14 '06 #6
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/lib...ebrequest.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.uswrote
in message news:uE**************@TK2MSFTNGP05.phx.gbl...
Hi,

"Kevin Spencer" <uc*@ftc.govwrote in message
news:OI**************@TK2MSFTNGP04.phx.gbl...
>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.


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Jul 14 '06 #7
Kevin Spencer <uc*@ftc.govwrote:
You need to re-read what you just posted.
You need to try compiling the code you've 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.
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 15 '06 #8
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.
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Kevin Spencer <uc*@ftc.govwrote:
>You need to re-read what you just posted.

You need to try compiling the code you've 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.

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jul 15 '06 #9
thanks all..

Jul 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richard B. Kreckel | last post by:
Hi! I was recently asked what book to recommend for a beginner in C++. I am convinced that you needn't study C in depth before learning C++ (though it helps), but cannot find any beginner's...
8
by: Grrrbau | last post by:
I'm a beginner. I'm looking for a good C++ book. Someone told me about Lafore's "Object-Oriented Programming in C++". What do you think? Grrrbau
7
by: Rensjuh | last post by:
Hello, does someone have / know a good C++ tutorial for beginnners? I would prefer Dutch, but English is also fine. Hoi, heeft / kent iemand nog een goede C++ tutorial voor beginners? Het liefste...
27
by: MHoffman | last post by:
I am just learning to program, and hoping someone can help me with the following: for a simple calculator, a string is entered into a text box ... how do I prevent the user from entering a text...
18
by: mitchellpal | last post by:
Hi guys, am learning c as a beginner language and am finding it rough especially with pointers and data files. What do you think, am i being too pessimistic or thats how it happens for a beginner?...
20
by: weight gain 2000 | last post by:
Hello all! I'm looking for a very good book for an absolute beginner on VB.net or VB 2005 with emphasis on databases. What would you reccommend? Thanks!
5
by: macca | last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP beginner - Reccommendations please? Thanks Paul
10
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
10
by: hamza612 | last post by:
I want to start learning how to program. But I dont know where to start. From what I've heard so far c++ is not a good lang. to learn as a beginner because its very complicated compared to others...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.