tags:

views:

35

answers:

1

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  A: 

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

MUG4N
so I need to put Path = FixPath() in all my subs?
Cold Assassin
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.
MUG4N
thx it worked great! :) I decided to put the global Dims in the On_Load Sub and it works.. Thanks again
Cold Assassin
Have you considered accepting this answer?!
Tim Schmelter