Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scope Question - How do you make a variable global to other forms? 2

Status
Not open for further replies.

emaduddeen

Programmer
Mar 22, 2007
184
US
Hi Everyone,

I have a scoping question.

I have 2 forms and on the main form there are several variables like this:

Code:
Public Class frmMainForm
    Dim strSourceMSDataFolder As String = "c:\data\ms data"
    Dim strSourceDBADataFolder As String = "C:\Data\DBA"
    Dim strDestinationDrive As String = Nothing
    Dim strDestinationFolder As String = _
        strDestinationDrive & "Data_" & Format(Now, "MM_dd_yyyy")

I also have several procedures like this as well:

Code:
    Public Sub FindThumbDrive()
        ' Find the highest drive letter of a thumb drive inserted into the computer.
        '---------------------------------------------------------------------------
        For Each objDrive As DriveInfo In DriveInfo.GetDrives
            If objDrive.DriveType = DriveType.Removable Then
                strDestinationDrive = objDrive.Name
            End If
        Next

        If strDestinationDrive = Nothing Then
            MessageBox.Show("I can't do anything else now because there is no " & _
                                            "thumb drive found on your computer." & _
                                            vbCrLf & vbCrLf & _
                                            "Please insert one and try again.", "Important", _
                                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

In the other form I tried to issue a call like this:

Code:
            FindThumbDrive()

How do I make these global so they can be seen by my other form?

Thanks.

Truly,
Emad
 
The easiest way would be to simply move your code over into a Module. All of the members within your module would be shared across all class instances of your application.

For example:

Code:
Imports System.IO
Module Module1
    Public strSourceMSDataFolder As String = "c:\data\ms data"
    Public strSourceDBADataFolder As String = "C:\Data\DBA"
    Public strDestinationDrive As String = Nothing
    Public strDestinationFolder As String = _
        strDestinationDrive & "Data_" & Format(Now, "MM_dd_yyyy")


    Public Sub FindThumbDrive()
        ' Find the highest drive letter of a thumb drive inserted into the computer.
        '---------------------------------------------------------------------------
        For Each objDrive As DriveInfo In DriveInfo.GetDrives
            If objDrive.DriveType = DriveType.Removable Then
                strDestinationDrive = objDrive.Name
            End If
        Next

        If strDestinationDrive = Nothing Then
            MessageBox.Show("I can't do anything else now because there is no " & _
                                            "thumb drive found on your computer." & _
                                            vbCrLf & vbCrLf & _
                                            "Please insert one and try again.", "Important", _
                                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub
End Module

And then just call the subroutine from any form you need.
 

You can add a Module to your app and have your var in there, along with the Sub if you want to access it from anywhere in your app:
Code:
[blue]Module[/blue] Module1
    [blue]Public[/blue] strDestinationDrive [blue]As[/blue] String = [blue]Nothing[/blue]

    [blue]Public Sub[/blue] FindThumbDrive()
        [green]' Find the highest drive letter of a thumb drive [/green]
.....
[blue]End Module[blue]

Have fun.

---- Andy
 

Well, that's what happens when your boss walks in in the middle of typing the answer.... Somebody beats you to it :)

Have fun.

---- Andy
 
Hi,

Thanks guys for the help.

It's appreciated!

Truly,
Emad
 
Hi Andrzejek,

Hope you don't get in trouble with your boss.

Truly,
Emad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top