0

My first post here and really basic question as I have just started learning. I am following a step by step tutorial from youtube. https://www.youtube.com/watch?v=6utWyl8agDY code is working alright in the video. below is the code:

Imports System
Imports System.IO

Module Program
Sub Main()

    Dim myReader As StreamReader = New StreamReader("values.txt")
    Dim line As String = ""

    While Not IsNothing(line)
        line = myReader.ReadLine()
        If Not IsNothing(line) Then
            Console.WriteLine(line)
        End If
    End While

    myReader.Close()
    Console.ReadLine()
End Sub
End Module

Problem I am facing is that I have red error (squiggly) line under IsNothing. One thing to note is the in the video tutorial version of VS under use is 2013 while I am using VS2017 community. any idea what I am doing wrong here?

JayV
  • 3,238
  • 2
  • 9
  • 14
Sam
  • 3
  • 3
  • 1
    Whenever VS flags an issue in your code, it tells you why. Any reason you chose to keep that secret? Did you even look for yourself? – jmcilhinney Mar 19 '20 at 10:32
  • Does this answer your question? [IsNothing versus Is Nothing](https://stackoverflow.com/questions/5791/isnothing-versus-is-nothing) – help-info.de Mar 19 '20 at 10:52
  • Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour) and read through the [help center](http://stackoverflow.com/help), in particular [how to ask](https://stackoverflow.com/help/how-to-ask). Your best bet here is to do your research, search for related topics on SO, and give it a go. After doing more research and searching, post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of your attempt and say specifically where you're stuck, which can help you get better answers. – help-info.de Mar 19 '20 at 11:12
  • Hi Guys, Sorry for writing back late. Thanks for pointing that I have not spend enough time to right direction and my question might have been ill prepared as well. I am totally new to code and lot of things are not making sense to me and I am trying to learn. Thanks for the patience and my apologies for all the shortcomings in my question. I will try to be better next. Regards, Sam – Sam Mar 31 '20 at 20:16

2 Answers2

2

I'm not sure what your tutorial says but there are issues with that code. It's fairly poorly structured and, to be honest, you shouldn't really use IsNothing anyway. The most immediate issue is where you're reading the text and that you're testing for Nothing twice. The code would be better written like this:

Dim myReader As New StreamReader("values.txt")
Dim line As String = myReader.ReadLine()

While line IsNot Nothing
    Console.WriteLine(line)
    line = myReader.ReadLine()
End While

myReader.Close()
Console.ReadLine()

Comparing directly to Nothing using Is or IsNot is the way to go and there's also no need for that check inside the loop.

If you want to get a bit more advanced, you can also create the StreamReader with a Using statement, so it gets closed implicitly:

Using myReader As New StreamReader("values.txt")
    Dim line As String = myReader.ReadLine()

    While line IsNot Nothing
        Console.WriteLine(line)
        line = myReader.ReadLine()
    End While
End Using

Console.ReadLine()

Better still would be to not create the StreamReader yourself at all but let the Framework do that for you:

For Each line As String In File.ReadLines("values.txt")
    Console.WriteLine(line)
End Using

Console.ReadLine()

Note that that code uses ReadLines rather than ReadAllLines. Both would work but the former will only read one line at a time while the latter will read the whole file first, load the lines into an array and return that. In this case it probably doesn't really matter which you use but ReadLines is generally preferable unless you specifically need all the lines first or you want to loop over them multiple times.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Hi, Thank you so much for taking time to look into my really basic code. I must say that your two improved examples really making me think more about why more learning is required to write minimum and most effective solution to do anything. Thanks again, Stay safe and take care. Best regards, – Sam Mar 31 '20 at 20:21
1

You have to decide whether your console application requires .net Core or .net Framework is sufficient.

As others have written in responses, your code is in need of improvement.

However, your code will run correctly if you choose the .net framework for your new project in Visual Studio 2017 Community as shown in the screenshot below.

enter image description here

help-info.de
  • 6,695
  • 16
  • 39
  • 41