Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Language question for a guru

C# Language question for a guru

Scheduled Pinned Locked Moved C#
questioncsharp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    albean
    wrote on last edited by
    #1

    WebRequest wrGETURL; wrGETURL = WebRequest.Create(sURL); ((HttpWebRequest)wrGETURL).UserAgent = "Mozilla/5.0"; Q1) On the first line above we can see that WebRequest is a class. On line 2 it seems to me that WebRequest is now an "instance". Could somebody explain that to me? I don't understand how you can call a method on a "class". I thought you had to call it on an "instance of the class". Q2) In the third line wrGETURL is being cast to HttpWebRequest. I really don't understand how this could be legal since HttpWebRequest is derived from WebRequest. Could somebody demonstrate this casting using a generic set of classes (foo, bar ... etc.) Thanks!

    J 1 Reply Last reply
    0
    • A albean

      WebRequest wrGETURL; wrGETURL = WebRequest.Create(sURL); ((HttpWebRequest)wrGETURL).UserAgent = "Mozilla/5.0"; Q1) On the first line above we can see that WebRequest is a class. On line 2 it seems to me that WebRequest is now an "instance". Could somebody explain that to me? I don't understand how you can call a method on a "class". I thought you had to call it on an "instance of the class". Q2) In the third line wrGETURL is being cast to HttpWebRequest. I really don't understand how this could be legal since HttpWebRequest is derived from WebRequest. Could somebody demonstrate this casting using a generic set of classes (foo, bar ... etc.) Thanks!

      J Offline
      J Offline
      J Dunlap
      wrote on last edited by
      #2

      A1) In the second line, WebRequest is still a class, and Create() is a static[^] method of that class. A2) Well, WebRequest.Create() can return various types of web requests (including HTTPWebRequest), therefore, it needs to have the generic web request as a return type. In this case, I suppose that the URL is assumed to start with "http://" and therefore return an HTTP request.

      "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
      "You must be the change you wish to see in the world." - Mahatma Gandhi

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups