Visual Basic Help Needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • crokett
    The Full Monte
    • Jan 2003
    • 10627
    • Mebane, NC, USA.
    • Ryobi BT3000

    #1

    Visual Basic Help Needed

    If I have code in a subroutine thusly:

    Code:
    Dim aReadStream = IO.StreamReader
    [SIZE=2]aReadStream = IO.File.OpenText("c:\test.txt")[/SIZE]
     
    [SIZE=2]someSub(aReadStream)[/SIZE]
    Then I have someSub declared elsewhere defined like:

    Code:
    Public Sub someSub(ByVal aStream as IO.StreamReader)
    when someSub is called it will be passed a StreamReader object pointing to a file called test.txt, no? Or should I be passing it by reference? Asking because it does not appear that someSub is actually reading test.txt - I have this code in someSub:

    Code:
    aLine = aStream.ReadLine
    A logic statement I have is not working I think because aLine is showing up with no value in my Locals window. Do I need to pass it by reference instead?
    David

    The chief cause of failure in this life is giving up what you want most for what you want at the moment.
  • Alex Franke
    Veteran Member
    • Feb 2007
    • 2641
    • Chapel Hill, NC
    • Ryobi BT3100

    #2
    Object references are passed by value. If you use byref, then the reference will be passed by reference. In any case you're working with only one instance of streamreader. (You'd pass byref, for example, if you wanted to set the original varuable to null/Nothing).

    So you're passing the streamreader by reference, and ReadLine() should read the first line of test.txt.

    I don't know how VB escapes characters in strings -- if you're running into a problem opening the file, you may need to escape the backslash: "c:\\test.txt"

    BTW, this applies to reference types (objects), not value types (int, bool, enums, structs, etc). The StreamReader is a reference type. But if you were using a value type, then byval would be making a copy of the variable and byref would be passing a reference to it. For a reference type, byval copies the value of the reference to the object, and byref is a reference to the reference to the object.
    Last edited by Alex Franke; 09-07-2007, 02:37 PM.
    online at http://www.theFrankes.com
    while ( !( succeed = try() ) ) ;
    "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

    Comment

    • Alex Franke
      Veteran Member
      • Feb 2007
      • 2641
      • Chapel Hill, NC
      • Ryobi BT3100

      #3
      One last thing: Remember to dispose the streamreader by calling aReadStream.Close() or by putting it into a using block (if VB has that).
      online at http://www.theFrankes.com
      while ( !( succeed = try() ) ) ;
      "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

      Comment

      • Hoakie
        Established Member
        • Feb 2007
        • 382
        • Iowa
        • Craftsman 21829

        #4
        Agree with Alex on how the references are passed. VB does not escape paths and it does have the Using block (i am not a fan tho)

        This code compiles in VB.Net2005 and reads the first line of the file for me.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim aReadStream As System.IO.StreamReader
        aReadStream = IO.File.OpenText("c:\test.txt")

        someSub(aReadStream)

        aReadStream.Close()
        aReadStream.Dispose()
        End Sub

        Private Sub someSub(ByVal reader As System.IO.StreamReader)
        Console.WriteLine(reader.ReadLine)
        End Sub
        Last edited by Hoakie; 09-07-2007, 03:05 PM.
        John
        To invent, you need a good imagination and a pile of junk. ~ Edison

        Comment

        • Alex Franke
          Veteran Member
          • Feb 2007
          • 2641
          • Chapel Hill, NC
          • Ryobi BT3100

          #5
          Yeah, that looks perfect, Hoakie.

          crokett, you might also check to see if the file is empty or if the first line is blank. If it's empty, you'll get null/Nothing when you call ReadLine(). Usually you would use this to test for EOF like "while ((line = aReadStream.ReadLine()) != null) { /* Do something with line */ }" , but it might throw you off if you're not using it that way.

          Also, ReadLine() will stop at \r, \n, or \r\n (Windows and Linux handle and display EOL's differently), so even if your file looks like it has no lines in Windows, it might actually have some.... Just a thought...
          Last edited by Alex Franke; 09-07-2007, 03:35 PM.
          online at http://www.theFrankes.com
          while ( !( succeed = try() ) ) ;
          "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

          Comment

          • Hoakie
            Established Member
            • Feb 2007
            • 382
            • Iowa
            • Craftsman 21829

            #6
            I forgot... after looking up the .Close() method again, it handles the dispose so the .Dispose() call in my code is not necessary
            John
            To invent, you need a good imagination and a pile of junk. ~ Edison

            Comment

            • tedkitch
              Senior Member
              • Jul 2006
              • 646
              • NE Suburbs, Chicago
              • Ryobi BT3100 What else is there?

              #7
              I've seen it all now. Programming help on a woodworking forum. The funny thing is if you post this to a VB forum, you will get about 10 different methods of doing this and multiple critiques of the code. You guys have given some of the most straightforward answers to a programming question I've ever seen.

              Kudos,
              Ted Kitch

              Comment

              • crokett
                The Full Monte
                • Jan 2003
                • 10627
                • Mebane, NC, USA.
                • Ryobi BT3000

                #8
                That's why I post the questions here Ted. I know we have some programmers here. I figured out the problem though.

                Here's the actual code from the subroutine.
                Code:
                [SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Until[/COLOR][/SIZE][SIZE=2] ((inDataFile.Peek <> -1) [/SIZE][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][SIZE=2] (InStr(strDataLine, strParseZone) > 0))[/SIZE]
                [SIZE=2][/SIZE]


                I pass the streamReader object in as inDataFile. strDataline is declared in the subroutine but it never gets set to a value. This is why I don't get paid to be a programmer.
                David

                The chief cause of failure in this life is giving up what you want most for what you want at the moment.

                Comment

                • Hoakie
                  Established Member
                  • Feb 2007
                  • 382
                  • Iowa
                  • Craftsman 21829

                  #9
                  Nah, Even paid programmers make mistakes too.

                  Glad you figured it out. either you won't do it again or it won't take nearly as long to figure out next time.
                  John
                  To invent, you need a good imagination and a pile of junk. ~ Edison

                  Comment

                  • Alex Franke
                    Veteran Member
                    • Feb 2007
                    • 2641
                    • Chapel Hill, NC
                    • Ryobi BT3100

                    #10
                    Originally posted by Hoakie
                    Nah, Even paid programmers make mistakes too.
                    They're not mistakes -- they're "success opportunities" for testers.

                    This part looks a little curious to me: "DoUntil ((inDataFile.Peek <> -1) Or (...))". If there's a "next" character in the file, then Peek() will return it as an int and so it won't be -1. So the first condition will be true, then the second condition will be skipped because of the Or, and you'll have your end condition and break out of the "DoUntil" loop.
                    online at http://www.theFrankes.com
                    while ( !( succeed = try() ) ) ;
                    "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

                    Comment

                    • Hoakie
                      Established Member
                      • Feb 2007
                      • 382
                      • Iowa
                      • Craftsman 21829

                      #11
                      Actually, using the "OR" in VB is not short circuited. Both expressions ARE evaluated as written. VB recently got the AndAlso and OrElse features that gives it the long overdue short circuit capabilities. Previously you needed to use two IF statements.
                      John
                      To invent, you need a good imagination and a pile of junk. ~ Edison

                      Comment

                      Working...