Xaaf Code
Xaaf Code

Reputation: 43

Trouble with OnLoad Sub (Visual Basic)

I have some trouble with declaring a default path file on startup.
Everytime I run the program, it's saying that pathFile is null. Does someone know what I need to change in my code?

Imports System
Imports System.IO
Imports System.Text

Public Class GlobalVariables
    Public Shared pathFile As String
End Class

Public Class Form1
    Protected Overridable Sub OnLoad(e As EventArgs)
        GlobalVariables.pathFile = My.Computer.FileSystem.SpecialDirectories.Desktop
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        ' create or overwrite the file
        Dim fs As FileStream = File.Create(GlobalVariables.pathFile)

        ' add text to file
        Dim info As Byte() = New UTF8Encoding(True).GetBytes(rtbText.Text)
        fs.Write(info, 0, info.Length)
        fs.Close()
    End Sub
End Class

Thanks in advance!
- Xaaf Code

Upvotes: 0

Views: 1335

Answers (1)

Mark
Mark

Reputation: 8160

Instead of trying to override OnLoad (which would be Overrides instead of Overridable), I would handle the load event:

Private Sub Form_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    GlobalVariables.pathFile = My.Computer.FileSystem.SpecialDirectories.Desktop
End Sub

You could probably just set the value where pathFile is declared instead:

Public Class GlobalVariables
    Public Shared pathFile As String = My.Computer.FileSystem.SpecialDirectories.Desktop
End Class

Upvotes: 1

Related Questions