0

I'm trying to write a simple VB program to read the file names in a given directory. This is what I've written:

Imports System
Imports System.IO
Imports System.Text

Module Program
  Sub Main(args As String())
    Dim FolderPath As String = 'C:\Users\uarenet\Downloads\Projects\Mock_Tables\_ExcelTables'
    Dim FileNames As String() =  Directory.GetFiles(FolderPath)
    For Each FileName As String In FileNames
        Console.WriteLine(FileName)
    Next
End Sub
End Module

For some reason Visual Studio is not recognizing the line Dim FileNames.... statement, and, because of this, refuses to process the For Each.... statement.

Thanks in advance for any help or suggestions you can offer.

E. Arenson
  • 23
  • 3
  • Hello and Welcome to Stack Overflow! In VB.NET you use double quotes (`"`) to denote strings. Single quotes/apostrophes (`'`) are used to write comments, thus they are ignored by the compiler. Because of this the compiler recognizes your first line (`Dim FolderPath As String = ...`) as incomplete. -- I'm voting to close this question as caused by a typographical error. – Visual Vincent Oct 17 '18 at 18:30
  • In the future it might be helpful to include the exact error message(s) you are getting as well as on which lines they occur. Doing so can help us identify the problem even faster. – Visual Vincent Oct 17 '18 at 18:33
  • There is a setting for Visual Basic which can help you with things like that - it is best to switch it to on as the default for new projects: [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360). – Andrew Morton Oct 17 '18 at 18:40
  • Thank you, @VisualVincent, for your helpful feedback. – E. Arenson Oct 17 '18 at 19:22

1 Answers1

1

You used this character ' to create a string although this character in Visual Basic language used for writing comments not string as you expected, string written in double quotes like "Hello world", so I suggest replacing your code with this:-

Sub Main(args As String())
    Dim FolderPath = "C:\Users\uarenet\Downloads\Projects\Mock_Tables\_ExcelTables"
    Dim FileNames =  Directory.GetFiles(FolderPath)
    For Each FileName As String In FileNames
        Console.WriteLine(FileName)
    Next
End Sub
Azhy
  • 704
  • 3
  • 16
  • 1
    Your second suggestion only works if the OP has `Option Infer` set to `On`, and besides not letting the compiler infer the types for you is not really a _mistake_. If the OP is new to VB.NET (or programming in general) I'd rather recommend sticking to explicitly specifying the types, as doing so is stricter and helps you get comfortable with the nature of a type-safe programming language more quickly (however this is my personal opinion). – Visual Vincent Oct 17 '18 at 18:39
  • Yes you're right, I removed this suggestion. – Azhy Oct 17 '18 at 18:45