0
Public Class Form1

    Dim pad As String = My.Application.Info.DirectoryPath

    Dim AutoCompleteDataBase As String() = File.ReadAllLines(pad + "\autocompletedb.txt")

End Class

Error BC30451 'File' is not declared.

What is going wrong? It seems VB is looking for Form1.vb in stead of autocompletedb.txt

I got many problems anyway, declaring variables in the Form part of the code. That seems only allowed with the "=" following the Dim where you immediately put a value in the variable. Which is a pity, because I would like to keep all variable declarations at the start of the code, not inside the code parts for buttons and such. What am I doing wrong?

If you want to answer, I can only understand examples of code as I got problems understanding proper explanations like true programmers can understand. I'm just an amateur connecting some dots.

Geoff Vane
  • 33
  • 1
  • 8
  • 3
    Make sure you have a reference to the `System.IO` namespace. – LarsTech Oct 21 '20 at 19:01
  • 2
    Try `IO.File.ReadAllLines()` or add `Imports System.IO` at the top of your file to be able to access the `File` class directly. As to the other question, I'm not sure I understand 100% what you mean. You may consider asking that as a separate question _with examples_ (and make sure to go through the [ask] article first). – 41686d6564 stands w. Palestine Oct 21 '20 at 19:02
  • 4
    ...and use `Path.Combine()` to glue together parts of a path. If you concatenate strings, it's probably better to use `&` instead of `+` in VB.Net – Jimi Oct 21 '20 at 19:03
  • 1
    Unrelated to the root question, but you said: "because I would like to keep all variable declarations at the start of the code" ...I suggest avoiding this practice. While opinion-based, this [discussion of global variables](https://stackoverflow.com/q/484635/3791245) is worth a read for people new to programming. (Tagged as C/C++, but the ideas hold generally.) "Inside the code parts for buttons and such" is a better place to declare variables, most of the time. – Sean Skelly Oct 21 '20 at 19:34
  • I found the addition of IO did help indeed! I saw it in other examples, although I do not know what it does exactly. Path.Combine is something I need examples of. I may find those quickly myself. I'll use & and not + in future. Good to know it may be better to declare variables where they are used; I thought that was bad. I'm not getting what 'class' and 'method' mean when I write code. I read it's like a brand and it's models. But that doesn't tell me why Dim and Private differ so much. However, I really appreciate the examples! It helps me to brew my vile apps way faster! – Geoff Vane Oct 21 '20 at 21:22
  • Path.Combine is explained here. I missed the crawl to the right to see that. Thanks! – Geoff Vane Oct 21 '20 at 21:24
  • *"I do not know what it does exactly"*. You should read [this](http://jmcilhinney.blogspot.com/2009/12/referencing-assemblies-and-importing.html?m=0). – jmcilhinney Oct 21 '20 at 23:40
  • 1
    *"I'm not getting what 'class' and 'method' mean"*. A class is a template for a type of thing. When you talk about a type of thing in real life, e.g. person, car or elephant, each of those would be a class in programming. You are a person and I am a person two, so we would be instances of the `Person` class in programming. A method is basically behaviour of a class, i.e. something that instances of that class can do. The `Person` class might have methods `Run`, `Jump`, `Eat` and `Sleep`. Properties are data, i.e. something that an object has, e.g. `Name`, `Age` or `Height`. – jmcilhinney Oct 22 '20 at 00:02
  • 1
    *"why Dim and Private differ so much"*. They differ but not the way you think. They are actually unrelated. `Dim` is the keyword that denotes a variable, like `Sub` and `Function` denote methods, `Property` denotes a property and `Event` denotes an event. It denotes both local and member variables. `Private` is an access level, like `Public`, `Friend` and `Protected`. It means that a type or member is accessible only in the scope that it is declared. This is valid VB: `Private Dim var As SomeType`. It's just that VB removes the `Dim` by default as it is implied if no other keyword is used. – jmcilhinney Oct 22 '20 at 00:06

1 Answers1

1

Here's two comments put together:

Private AutoCompleteDataBase As String() = System.IO.File.ReadAllLines(System.IO.Path.Combine(pad, "autocompletedb.txt"))

Note that I changed Dim to Private, as that is the default access, and Dim shouldn't be used at class level.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40