I have a function that reads a text file that contains information about the sql database it connects to. It has to be invoked across multiple forms. How could I make it to where it is invoked once and then stores into variables that the program can continue to use no matter which class it's under?

Here's the code:

        'Begin reading config file to receive database info
        Dim fileReader As System.IO.StreamReader
        fileReader = My.Computer.FileSystem.OpenTextFileReader("config.txt")
        Dim configvars(0 To 3) As String
        Dim count As Integer = 0
        Do Until count = 4
            configvars(count) = fileReader.ReadLine()
            count = count + 1
        Loop

put it in a module

Module ConnectionHelper
    Public Function foo()

        'Begin reading config file to receive database info
        Dim fileReader As System.IO.StreamReader
        fileReader = My.Computer.FileSystem.OpenTextFileReader("config.txt")
        Dim configvars(0 To 3) As String
        Dim count As Integer = 0
        Do Until count = 4
            configvars(count) = fileReader.ReadLine()
            count = count + 1
        Loop

    End Function
End Module
Comments
that works

The option that I prefer is to extend the "My" namespace. It is designed just for this type of use.

I assumed that the config file only would need to be read once. If this is not the case you can easily modify the logic. Just add this like you would a Class or Module to your code.

 Namespace My
   Friend Class MyApplication
      Private _configvars(0 To 3) As String
      Public ReadOnly Property configvars(ByVal index As Int32) As String
         Get
            If _configvars(0) Is Nothing Then
               ' read config file
               Dim fileReader As System.IO.StreamReader
               fileReader = My.Computer.FileSystem.OpenTextFileReader("config.txt")

               Dim count As Integer = 0
               Do Until count = 4
                  _configvars(count) = fileReader.ReadLine()
                  count = count + 1
               Loop

            End If
            Return _configvars(index)
         End Get
      End Property 'configvars
   End Class
End Namespace 

Now to retrieve your values: My.Application.configvars(0)

P.S.: Glad to see that I'm not the only left using the old array declaration syntax (0 to something). :))

Edited 3 Years Ago by TnTinMN

@TnTinMN - Just curious - is their an advantage to doing this rather than using My.Settings?
It really depends on what you want to do.

My.Settings typically are used to persist a value between runs of an application. You could however, just use them as a global variable definition as long as you did not care if the value was written to disk when you told the app to save them (or you could zero them out before saving).

The OP asked about creating a Global function, so I went this route. I just like the logic of "My.Application.VariableName". The "My" namespace is once of the niceties provided by VB; so, I figure why not use it.

I assumed that the OP's "config.txt" file was generated outside of the application. If not, then definitely a My.Settings route would make more sense.

Options, options, options, to many options. :)

Oops: Just noticed that I forgot the Partial keyword in my original post.

It should have been: Partial Friend Class MyApplication

Sorry if I'm not as on topic as maybe I should be but isn't there a static object decleration in VB? Can you not just define a static class / object with the method and the members then invoke the method which would populate the members, any class or object should then be able to read from the same static name space / scope.

What's declaring it partial do as opposed to your initial?

What's declaring it partial do as opposed to your initial?

VB.Net creates a Class MyApplication within the "My" namespace. Since I was extending that class (my code is added to the overall Class definition) and only one of the Class Definitions can omit the Partial keyword, it is proper to use the Partial keyword. You can have many Partial Class segments.

For more info, Click Here.

@daniwan

Sorry if I'm not as on topic as maybe I should be but isn't there a static object decleration in VB? Can you not just define a static class / object with the method and the members then invoke the method which would populate the members, any class or object should then be able to read from the same static name space / scope.

That is essentially what Ancient Dragon presented. All elements of a Module are static. For more info see: Standard Modules vs. Class Modules

thanks for the help, I just started a couple days ago programming in Visual Basic and I like it. But I still like c++ more :)

This article has been dead for over six months. Start a new discussion instead.