• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Beginners C#query

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Beginners C#query

    How does the bolded red line below do what it does in detecting the EOF of the text file?

    Coming from a VBA background I'm slightly confused.

    I understand the readline assigns to my string field (data) but how is the not equal to null functioning?

    I tested and has nothing to do with being a blank row.

    Thanks.


    using (StreamReader sr = new StreamReader(args[0]))
    {
    string data =null;
    // Assumes all rows are populated
    while ((data=sr.ReadLine()) !=null) {
    iRows++;
    Console.WriteLine(iRows + " - " + data);
    }
    }
    Last edited by Scrag Meister; 8 November 2012, 16:31.
    Never has a man been heard to say on his death bed that he wishes he'd spent more time in the office.

    #2
    A line is defined as a sequence of characters followed by a line feed ("\n") or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed. The returned value is a null reference (Nothing in Visual Basic) if the end of the input stream is reached.
    StreamReader.ReadLine Method

    ReadLine reads the line untill it gets to the end of the file and then it returns null.

    GE

    Comment


      #3
      (data=sr.ReadLine()) evaluates first, giving a value for data that is either a string or null which is then tested against then != null.

      It's not pretty.

      Comment


        #4
        Thanks for that Gareth, so what is the !=null comparing too?

        The string or the success of the assignment to the string?

        Edit : So Assigning a value returns the value assigned?

        If I have a blank line in the file what is that value if not a null?
        Last edited by Scrag Meister; 8 November 2012, 16:44.
        Never has a man been heard to say on his death bed that he wishes he'd spent more time in the office.

        Comment


          #5
          Originally posted by Scrag Meister View Post
          Thanks for that Gareth, so what is the !=null comparing too?

          The string or the success of the assignment to the string?

          Edit : So Assigning a value returns the value assigned?

          If I have a blank line in the file what is that value if not a null?
          This should help. C# While Loop Examples

          A blank line in a file isn't null, it is an empty string.

          Comment


            #6
            Originally posted by Scrag Meister View Post
            Edit : So Assigning a value returns the value assigned?
            It returns the object assigned to (which has the same value as you just assigned it).

            if( ( data=sr.ReadLine() ) != null )

            is exactly the same as:

            data=sr.ReadLine();
            if( data != null )
            {

            }

            But that doesn't work in a while.

            Despite being lambasted in the past for daring to suggest knowing the syntax of the language is important, I would never do that. I've always thought it's a horrible left over from the days of yore (or days of C to be accurate), and it's better in that case to write the code more clearly.
            Will work inside IR35. Or for food.

            Comment


              #7
              Originally posted by Scrag Meister View Post
              Edit : So Assigning a value returns the value assigned?
              Yes that's essentially what is happening, C# operates very similarly to other languages here. Making expressions like x = y = z = 10 possible.

              From the following link: 7.13.1 Simple assignment (C#)

              The = operator is called the simple assignment operator. In a simple assignment, the right operand must be an expression of a type that is implicitly convertible to the type of the left operand. The operation assigns the value of the right operand to the variable, property, or indexer element given by the left operand.

              The result of a simple assignment expression is the value assigned to the left operand. The result has the same type as the left operand and is always classified as a value.

              Comment


                #8
                Terrible, terrible code.

                I know exactly why it is done though as they are thinking...

                using (StreamReader sr = new StreamReader(args[0]))
                {
                while ( sr.ReadLine() !=null) {
                iRows++;
                Console.WriteLine(iRows + " - " + sr.ReadLine() );
                }
                }

                involves 2 method calls and they figure that dumping the contents of the call into a variable and using that next is quicker. All balls of course but they think that is how it works. See that kind of tulip everyday sadly.

                Comment


                  #9
                  ++iRows;
                  FTFY

                  Comment


                    #10
                    Originally posted by RasputinDude View Post
                    FTFY
                    Not in that context.

                    iRows is going to be the same with ++ appended or prepended when it comes to the next line

                    I would have written it...

                    using (StreamReader sr = new StreamReader(args[0]) {
                    while ( sr.ReadLine() !=null) {
                    Console.WriteLine(iRows++ + " - " + sr.ReadLine() );
                    }

                    but then I am a maverick who sees every line like a penny spent.

                    7 rows down to 4.

                    You can see why my code metrics don't do me any favours in clientCo.

                    Comment

                    Working...
                    X