global variable from FolderBrowserDialog.SelectedPath??

StillLearning

Member
Joined
Dec 21, 2012
Messages
12
Programming Experience
Beginner
When a folder is selected the fun starts...

Whenever I add and double-click a button in Form1.vb[Design] it creates a Private Sub in Public Class Form1.

I want to create what I can only describe in layman's terms as a global variable for the path which was selected.

I've tried several approaches similar to:

VB.NET:
Public Sub ButtonDataTable_Click(sender As Object, e As EventArgs) Handles ButtonDataTable.Click

If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then

            Const sourceDirectory As String
            sourceDirectory = FolderBrowserDialog1.SelectedPath

ModPopulateGrid.Main()
etc.

End Sub

I can't do things like this within a Private Sub

Should I change the Private Sub to a Public Class?

I tried that, but I got several errors so I did the magical Undo... Do I need to work through these errors or is there a better way to share the FolderBrowserDialog1.SelectedPath with other modules and classes?
-----------------------------------
I've just about got my toy program the way I want it so now I'm trying to make it better and learn. I've been using:

Dim sourceDirectory As String = Form1.FolderBrowserDialog1.SelectedPath

at the beginning of modules and classes

and I have also just put the text in a TextBox on Form1 and pulled it from there, but...

I know there's a better way, and I'd like to learn...

Thank you for taking the time to read this.
 
Last edited:
LOL, thank you "Miyagee"!!

I'm gonna keep digging... I thought perhaps you were inferring that I could declare such global variables in the form1_load, but I think not.

I realize I can create a public const or dim test as string in Public Class Form1 outside of any sub or function, and they will be available to any sub or function, BUT not to any other Classes or Modules.

I realize I can pass them to other Classes or Modules when calling them.

But, I haven't figured out how to make them available to other classes or modules without passing them around....

I'm thinking the "overall issue" which you identified as my need for structured guidance is absolutely correct.

I'm gonna sleep on it and play with it some more later. Thank you again for your guidance!
 
Hi,

You got my point exactly but just to help a little on this one, if you wanted to declare a variable which had a scope that was public to every object in your project, without reference to any other object in your project, then you would add a module to your project and then declare a Public variable within that module.

What you need to learn however is that variables should only be declared within the scope that they are needed so that valuable memory resources are not consumed and retained when they do not need to be.

This may sound a bit confusing at this point but you will soon get to grips with this once you learn things in a structured manner.

Cheers,

Ian
 
Thank you for that, sir!

It's funny you mention the memory resource thing to me. I was fascinated by the byRef and byVal and memory stack vs. heap thing... I dispose of my streamwriters... I was trying to figure out how to dispose of strings when I didn't need them (because I'm using them so early during form_load to build a DataTable) when I learned about the difference between = nothing and dispose, and I finally learned they are disposed of at the conclusion of the class or module or sub or function which created them so you don't explicitly dispose of them...

I will take your excellent advice. As a matter of fact, just last night I identified the book I want to buy...
I decided on Stephens' Visual Basic Programming 24-Hour Trainer (Wrox Programmer to Programmer) because I downloaded their code samples that go with the book and really liked what they are doing. Babysteps...

I hope you're not laughing about the 24 hour thing! I don't expect to become an expert overnight, but I do need some structured knowledge under my belt so I can move forward!

Thank you again, sincerely for your guidance. Have a great day!!
 
Back
Top