473,778 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c# beginner

hello all,

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

in vb:

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

in c#:

HttpWebRequest httpRequest;

httpRequest = (HttpWebRequest )
WebRequest.Crea te("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 5519
"hharry"
hello all,

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

in vb:

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

in c#:

HttpWebRequest httpRequest;

httpRequest = (HttpWebRequest )
WebRequest.Crea te("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.Crea te 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.Crea te 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.Crea te( _
"http://www.winisp.net/goodrich/default.htm")

in c#:

HttpWebReque st httpRequest;

httpRequest = (HttpWebRequest )
WebRequest.Cre ate("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.Crea te 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.Crea te("http://www.winisp.net/goodrich/default.htm");

-or -

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

The "(HttpWebReques t)" is a cast. It is casting the result of the method as
an HttpWebRequest. However, it is unnecessary in this case. The
WebRequest.Crea te 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(WebReques t.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.Clo ne();

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*********@ny c.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
hello all,

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

in vb:

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

in c#:

HttpWebRequest httpRequest;

httpRequest = (HttpWebRequest )
WebRequest.Crea te("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.govwro te in message
news:OG******** ********@TK2MSF TNGP05.phx.gbl. ..
>That's a typecast. Probably the WebRequest.Crea te does not return a
HttpWebReque st (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.govwro te in message
news:OI******** ******@TK2MSFTN GP04.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.Crea te("http://www.winisp.net/goodrich/default.htm");

-or -

HttpWebRequest httpRequest =
WebRequest.Crea te("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.mach in AT dot.state.fl.us wrote
in message news:uE******** ******@TK2MSFTN GP05.phx.gbl...
Hi,

"Kevin Spencer" <uc*@ftc.govwro te in message
news:OI******** ******@TK2MSFTN GP04.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:

HttpWebReque st httpRequest;
httpRequest =
WebRequest.Cre ate("http://www.winisp.net/goodrich/default.htm");

-or -

HttpWebReque st httpRequest =
WebRequest.Cre ate("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.govwro te:
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.Crea te 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.Crea te
("http://www.slashdot.or g");
}
}

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

Put the cast in and it compiles fine.

--
Jon Skeet - <sk***@pobox.co m>
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.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Kevin Spencer <uc*@ftc.govwro te:
>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.Crea te 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.Crea te
("http://www.slashdot.or g");
}
}

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

Put the cast in and it compiles fine.

--
Jon Skeet - <sk***@pobox.co m>
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
3081
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 book which isn't aimed at people coming from C/Pascal/Java/Delpi/whatever... However, there seem to be plenty such books for all those other languages. Is there really no literature for people trying to learn programming by starting with C++? ...
8
2382
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
2945
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 in Nederlands, maar Engels is ook goed. Thnx, Rensjuh
27
4374
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 instead of a number, or give an error message? Also, how can I make the program verify there are two valid entries in txtBox1 and txtBox2 to then ENABLE the button operators (ie +, -, /, *).
18
2927
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? Are there better languages than c for a beginner? For instance visual basic or i should just keep the confidence of improving?
20
2298
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
2747
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
4470
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
2155
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 like python, ruby etc. I would like to know if there is a prerequisite to learning any computer language, is there something I have to learn before learning any computer language, like a basic or core?
22
18151
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 programmer and looking for a starting point in regards to practical projects to work on. What are some projects that beginner programmers usually start with? Please list a few that would be good for a beginner PHP programmer to
0
9628
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.