0

Can't seem to figure out how to use a function return variable in global Dims example code:

Public Class Main
  Dim Path As String = FixPath()
  Dim fixwrongtxt As String = Path & "tryme.txt"

  Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    FixPath()
    On_load()
  End Sub

  Private Function FixPath() As String
    Path = "C:\test"
    MsgBox(Path) //First Message Box'
    Return Path
  End Function

  Sub On_load()
    MsgBox(fixwrongtxt) //Second Message Box
  End Sub
End Class

when I run it all I get the first message box that contains "C:\test" and I click ok and on the second messagebox I get "custom.dll" with out the "C:\test" or "Path Return" What am I doing wrong? I know I can't use // in vb.net. I have also tried adding "FixPath()" under Sub On_load() but got same result. Also the reason I have to have these global is because I have around 30 Subs that refer to "Path" Variable... Thanks

1 Answer 1

1

Change your public variable to this:

Dim Path As String 


Private Sub Main_Load(....

Path = FixPath()

It's not possible to call a function in global space

Sign up to request clarification or add additional context in comments.

4 Comments

so I need to put Path = FixPath() in all my subs?
No when you set the variable once you can use it everywhere else. Just get sure that you set your variable on the first method so load is the proper method to do this.
thx it worked great! :) I decided to put the global Dims in the On_Load Sub and it works.. Thanks again
I just noticed I didn't, sorry about that MUG4N! Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.