Variable Scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bryan

    Variable Scope

    I have the following and am crashing at the specified line. I've declared Function Globals which should extend the scope of a variable to the end of the funtion. But it seems that my varaiables are ending at the end of the case statement. They shouldn't be. I'm really starting to wondering if I found a bug being that the variables have been declared Globally.

    Public Function Scrape(ByVal URL As String, ByVal ReadandWrite As Boolean, ByVal FileType As String) As Object

    Dim objWebRequest As System.Net.Http WebRequest
    Dim objWebResponse As System.Net.Http WebResponse
    Dim file As System.IO.Strea mReader
    Dim strFile As String

    Select Case URL
    Case "Text File"
    file = New System.IO.Strea mReader(URL)
    Case "URL File"
    objWebRequest = CType(System.Ne t.WebRequest.Cr eate(URL), System.Net.Http WebRequest)
    objWebRequest.M ethod = "GET"
    objWebResponse = CType(objWebReq uest.GetRespons e(), System.Net.Http WebResponse)
    file = New System.IO.Strea mReader(objWebR esponse.GetResp onseStream)
    End Select

    'Program crashes right here.
    strFile = file.ReadToEnd

    If ReadandWrite Then
    Scrape = strFile
    'Program crashes here too if the above line was set in the Global as 'Dim strFile as String = file.ReadToEnd
    file.Close()
    Return Scrape
    End If

  • Scott M.

    #2
    Re: Variable Scope

    How about giving us some information about the exception that was thrown?
    Since you know where it crashes, put that code in a Try...Catch statement.
    Catch the exception and get it's type if need be.


    "Bryan" <anonymous@disc ussions.microso ft.com> wrote in message
    news:D420A800-78EE-4F4E-86BC-1C46A3B36922@mi crosoft.com...[color=blue]
    > I have the following and am crashing at the specified line. I've declared[/color]
    Function Globals which should extend the scope of a variable to the end of
    the funtion. But it seems that my varaiables are ending at the end of the
    case statement. They shouldn't be. I'm really starting to wondering if I
    found a bug being that the variables have been declared Globally.[color=blue]
    >
    > Public Function Scrape(ByVal URL As String, ByVal ReadandWrite As[/color]
    Boolean, ByVal FileType As String) As Object[color=blue]
    >
    > Dim objWebRequest As System.Net.Http WebRequest
    > Dim objWebResponse As System.Net.Http WebResponse
    > Dim file As System.IO.Strea mReader
    > Dim strFile As String
    >
    > Select Case URL
    > Case "Text File"
    > file = New System.IO.Strea mReader(URL)
    > Case "URL File"
    > objWebRequest = CType(System.Ne t.WebRequest.Cr eate(URL),[/color]
    System.Net.Http WebRequest)[color=blue]
    > objWebRequest.M ethod = "GET"
    > objWebResponse = CType(objWebReq uest.GetRespons e(),[/color]
    System.Net.Http WebResponse)[color=blue]
    > file = New[/color]
    System.IO.Strea mReader(objWebR esponse.GetResp onseStream)[color=blue]
    > End Select
    >
    > 'Program crashes right here.
    > strFile = file.ReadToEnd
    >
    > If ReadandWrite Then
    > Scrape = strFile
    > 'Program crashes here too if the above line was set in the Global as 'Dim[/color]
    strFile as String = file.ReadToEnd[color=blue]
    > file.Close()
    > Return Scrape
    > End If
    >[/color]


    Comment

    • Scott M.

      #3
      Re: Variable Scope

      How about giving us some information about the exception that was thrown?
      Since you know where it crashes, put that code in a Try...Catch statement.
      Catch the exception and get it's type if need be.


      "Bryan" <anonymous@disc ussions.microso ft.com> wrote in message
      news:D420A800-78EE-4F4E-86BC-1C46A3B36922@mi crosoft.com...[color=blue]
      > I have the following and am crashing at the specified line. I've declared[/color]
      Function Globals which should extend the scope of a variable to the end of
      the funtion. But it seems that my varaiables are ending at the end of the
      case statement. They shouldn't be. I'm really starting to wondering if I
      found a bug being that the variables have been declared Globally.[color=blue]
      >
      > Public Function Scrape(ByVal URL As String, ByVal ReadandWrite As[/color]
      Boolean, ByVal FileType As String) As Object[color=blue]
      >
      > Dim objWebRequest As System.Net.Http WebRequest
      > Dim objWebResponse As System.Net.Http WebResponse
      > Dim file As System.IO.Strea mReader
      > Dim strFile As String
      >
      > Select Case URL
      > Case "Text File"
      > file = New System.IO.Strea mReader(URL)
      > Case "URL File"
      > objWebRequest = CType(System.Ne t.WebRequest.Cr eate(URL),[/color]
      System.Net.Http WebRequest)[color=blue]
      > objWebRequest.M ethod = "GET"
      > objWebResponse = CType(objWebReq uest.GetRespons e(),[/color]
      System.Net.Http WebResponse)[color=blue]
      > file = New[/color]
      System.IO.Strea mReader(objWebR esponse.GetResp onseStream)[color=blue]
      > End Select
      >
      > 'Program crashes right here.
      > strFile = file.ReadToEnd
      >
      > If ReadandWrite Then
      > Scrape = strFile
      > 'Program crashes here too if the above line was set in the Global as 'Dim[/color]
      strFile as String = file.ReadToEnd[color=blue]
      > file.Close()
      > Return Scrape
      > End If
      >[/color]


      Comment

      • Bryan

        #4
        Re: Variable Scope

        Here is the error
        Object reference not set to an instance of an object.
        Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

        Exception Details: System.NullRefe renceException: Object reference not set to an instance of an object

        Source Error:

        Line 23: If ReadandWrite The
        Line 24: Scrape = strFil
        Line 25: file.Close() 'Error occurs her
        Line 26: Return Scrap
        Line 27: End I


        Source File: c:\inetpub\wwwr oot\CompetitorK iller\ScreenScr aper.vb Line: 25

        Stack Trace:

        [NullReferenceEx ception: Object reference not set to an instance of an object.
        CompetitorKille r.ScreenScraper .Scrape(String URL, Boolean ReadandWrite, String FileType) in c:\inetpub\wwwr oot\CompetitorK iller\ScreenScr aper.vb:2
        CompetitorKille r.WebForm1.Butt on1_Click(Objec t sender, EventArgs e) in c:\inetpub\wwwr oot\CompetitorK iller\WebForm1. aspx.vb:3
        System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e
        System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring eventArgument
        System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler sourceControl, String eventArgument
        System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData
        System.Web.UI.P age.ProcessRequ estMain(



        Comment

        • Bryan

          #5
          Re: Variable Scope

          Here is the error
          Object reference not set to an instance of an object.
          Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

          Exception Details: System.NullRefe renceException: Object reference not set to an instance of an object

          Source Error:

          Line 23: If ReadandWrite The
          Line 24: Scrape = strFil
          Line 25: file.Close() 'Error occurs her
          Line 26: Return Scrap
          Line 27: End I


          Source File: c:\inetpub\wwwr oot\CompetitorK iller\ScreenScr aper.vb Line: 25

          Stack Trace:

          [NullReferenceEx ception: Object reference not set to an instance of an object.
          CompetitorKille r.ScreenScraper .Scrape(String URL, Boolean ReadandWrite, String FileType) in c:\inetpub\wwwr oot\CompetitorK iller\ScreenScr aper.vb:2
          CompetitorKille r.WebForm1.Butt on1_Click(Objec t sender, EventArgs e) in c:\inetpub\wwwr oot\CompetitorK iller\WebForm1. aspx.vb:3
          System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e
          System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring eventArgument
          System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler sourceControl, String eventArgument
          System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData
          System.Web.UI.P age.ProcessRequ estMain(



          Comment

          • yEaH rIgHt

            #6
            Re: Variable Scope

            Your code doesn't make sense.

            Change this line
            Select Case URL

            to read this:
            Select Case FileType


            I tried your code this way and it worked perfectly. Your select case
            never selected any statement. That's why you got an error. Your
            filestream was never craeted.




            Bryan wrote:[color=blue]
            > Here is the error:
            > Object reference not set to an instance of an object.
            > Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
            >
            > Exception Details: System.NullRefe renceException: Object reference not set to an instance of an object.
            >
            > Source Error:
            >
            > Line 23: If ReadandWrite Then
            > Line 24: Scrape = strFile
            > Line 25: file.Close() 'Error occurs here
            > Line 26: Return Scrape
            > Line 27: End If
            >
            >
            > Source File: c:\inetpub\wwwr oot\CompetitorK iller\ScreenScr aper.vb Line: 25
            >
            > Stack Trace:
            >
            > [NullReferenceEx ception: Object reference not set to an instance of an object.]
            > CompetitorKille r.ScreenScraper .Scrape(String URL, Boolean ReadandWrite, String FileType) in c:\inetpub\wwwr oot\CompetitorK iller\ScreenScr aper.vb:25
            > CompetitorKille r.WebForm1.Butt on1_Click(Objec t sender, EventArgs e) in c:\inetpub\wwwr oot\CompetitorK iller\WebForm1. aspx.vb:39
            > System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e)
            > System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring eventArgument)
            > System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler sourceControl, String eventArgument)
            > System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData)
            > System.Web.UI.P age.ProcessRequ estMain()
            >
            >
            >[/color]

            Comment

            • yEaH rIgHt

              #7
              Re: Variable Scope

              Your code doesn't make sense.

              Change this line
              Select Case URL

              to read this:
              Select Case FileType


              I tried your code this way and it worked perfectly. Your select case
              never selected any statement. That's why you got an error. Your
              filestream was never craeted.




              Bryan wrote:[color=blue]
              > Here is the error:
              > Object reference not set to an instance of an object.
              > Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
              >
              > Exception Details: System.NullRefe renceException: Object reference not set to an instance of an object.
              >
              > Source Error:
              >
              > Line 23: If ReadandWrite Then
              > Line 24: Scrape = strFile
              > Line 25: file.Close() 'Error occurs here
              > Line 26: Return Scrape
              > Line 27: End If
              >
              >
              > Source File: c:\inetpub\wwwr oot\CompetitorK iller\ScreenScr aper.vb Line: 25
              >
              > Stack Trace:
              >
              > [NullReferenceEx ception: Object reference not set to an instance of an object.]
              > CompetitorKille r.ScreenScraper .Scrape(String URL, Boolean ReadandWrite, String FileType) in c:\inetpub\wwwr oot\CompetitorK iller\ScreenScr aper.vb:25
              > CompetitorKille r.WebForm1.Butt on1_Click(Objec t sender, EventArgs e) in c:\inetpub\wwwr oot\CompetitorK iller\WebForm1. aspx.vb:39
              > System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e)
              > System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring eventArgument)
              > System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler sourceControl, String eventArgument)
              > System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData)
              > System.Web.UI.P age.ProcessRequ estMain()
              >
              >
              >[/color]

              Comment

              Working...