passing variable to function

T

thomasp

In the code below I have a function that tests if a file exists. It takes a
variable named strFileName, simple enough. My question is, is there a way
to pass it a variable with another name as long as the variable is a string?
In different subs the variable of the file name may have a different name.
An example would be the subOpenFile listed below. I have two files that I
want to test with the function: strFileName and strFileName2. I worked
around the issue by using a temp variable, but would like a better way.

Thanks,

Thomas

Public Sub subOpenFile(ByVal strFileName As String, ByVal strFileName2 As
String, _
ByVal intImportType As Integer, ByRef
bolExitImport As Boolean)

'This sub either opens one or two file stream readers depending on
which type of import was started.
'If a Q36 import is being processed the second file stream is
opened. Since a file exists function
'was not performed on the .htg file when the import file was
selected, it is performed now. The file
'name is moved to a temp variable long enough for the function to be
ran.

Dim strTempFileName As String
srdImportFile1 = New System.IO.StreamReader(strFileName)

bolExitImport = False

If intImportType = 2 Then
Dim intLen As Integer
intLen = Len(strFileName) - 3
strFileName2 = Left(strFileName, intLen) & "htg"

strTempFileName = strFileName
strFileName = strFileName2

If funFileExists(strFileName) Then
srdImportFile2 = New System.IO.StreamReader(strFileName)
Else
bolExitImport = True
Response = MsgBox("The Targets.htg import file was not
found, exiting import.", MsgBoxStyle.MsgBoxHelp, _
"File Not Found Error!")
End If

strFileName = strTempFileName
End If

End Sub

-------------------------------------------------------------------------------------------------------------

Public Function funFileExists(ByVal strFileName As String) As Boolean

Dim Attr As FileAttribute

On Error Resume Next
Attr = GetAttr(strFileName)
If Err.Number <> 0 Then
funFileExists = False
ElseIf (Attr And FileAttribute.Directory) Then
funFileExists = False
Else
funFileExists = True
End If

Err.Clear()

On Error GoTo 0

End Function
 
L

Larry Lard

In the code below I have a function that tests if a file exists. It takes a
variable named strFileName, simple enough. My question is, is there a way
to pass it a variable with another name as long as the variable is a string?
In different subs the variable of the file name may have a different name.
An example would be the subOpenFile listed below. I have two files that I
want to test with the function: strFileName and strFileName2. I worked
around the issue by using a temp variable, but would like a better way.

You seem to have a slight misunderstanding of the way procedure
arguments work. Your function funFileExists takes *a String* as its
argument - it doesn't care what the caller names this String, or even
that it has a name at all. *Within* funFileExists, the String is named
strFileName and is a normal variable. Thus all of these are legitimate
calls to funFileExists:

Dim s As String
If funFileExists(s) Then ...

Dim o As Object
If funFileExists(o.ToString) Then ...

Dim s1 As String, s2 As String
If funFileExists(s1 & s2) Then ...

All that matters is that the argument passed to funFileExists is *a
value of type String*. Hope this helps clear things up for you.

By the way, the .NET Framework includes a method for testing for file
existence, so you don't really need to write your own. It is
File.Exists in the System.IO namespace.
 
C

Chris Dunaway

I don't understand why you're using a temporary variable. The name of
the parameter in the function is irrelevant.

You can simply call it like this:

If funFileExists(strFileName2) Then
'do stuff
Else
'do other stuff
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top