Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: Variable scope and functions


Message #1 by "Joe Hagerty" <joeh@t...> on Fri, 30 Aug 2002 10:16:09 -0700
I don't get it...what are you trying to do?  Do you have a defined set of
strings for which each has a specific value?  If so, then it would seem to
me that the strings and their values could be loaded into an array.  The
array could then be searched to find the string, and the value returned.
You could define the findstring function as public in one module, then set a
form object to that module in a second module and reference the first
module's public function as a property.  For example, in a new project:

'Form1
'================================
Option Explicit

Private mForm2 As Form2
Private strArray() As String

Private Sub Form_Activate()
    Set mForm2 = New Form2
    Call Form2.Start(Me)

End Sub

Private Sub Form_Load()
    Call psLoadArray  ' this would redim the array and load it

End Sub

Private Sub psLoadArray()
    ReDim strArray(1, 4) 'make array two columns by 5 rows
    strArray(0, 0) = "string 1"
    strArray(1, 0) = "A"
    strArray(0, 1) = "string 2"
    strArray(1, 1) = "B"
    strArray(0, 2) = "string 3"
    strArray(1, 2) = "C"
    strArray(0, 3) = "string 4"
    strArray(1, 3) = "D"
    strArray(0, 4) = "string 5"
    strArray(1, 4) = "E"

End Sub


Public Function findstring(strKey) As Variant
Dim i As Integer

    findstring = "Not Found"
    For i = 0 To UBound(strArray, 2)
        If strArray(0, i) = strKey Then
            findstring = strArray(1, i)
            Exit For
        End If
    Next i

End Function

'Form2
'=================================
Option Explicit

Private mForm1 As Form1

Public Sub Start(ByVal aForm As Form1)
    Set mForm1 = aForm
    Me.Show

End Sub

Private Sub Form_Load()
Dim lsString As String
Dim lsValue As String

    lsString = "string 1"
    lsValue = mForm1.findstring(lsString)
    MsgBox lsString & "'s value is " & lsValue

End Sub

HTH, Pete


-----Original Message-----
From: Joe Hagerty [mailto:joeh@t...]
Sent: Friday, August 30, 2002 1:16 PM
To: professional vb
Subject: [pro_vb] Variable scope and functions



I am dealing with a large number of variables in my program.  Say 100+.
Currently I have them defined using a private scope.  I need now to process
them inside a function somthing like this

sub main()
	dim x as string

	findstring("string 1","value 1")


end sub



function findstring(strKey,StrValue)
	if strkey = "string 1" then
		x=strValue
	end if
end function


But I am looking up a list of possible values that is huge.  Is there a way
to do this without setting all my variables as public?  I guess what I am
asking is this:  is there a way to set a variable in another routines scope?

Thanks for any pointers


Joe Hagerty
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.384 / Virus Database: 216 - Release Date: 8/21/2002



---
Visual C# - A Guide for VB6 Developers
This book will make it easy to transfer your skills
from Visual Basic 6 to C#, the language of choice
of the .NET Framework.
http://www.wrox.com/ACON11.asp?ISBN=1861007175&p2p0059



  Return to Index