By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,996 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,996 IT Pros & Developers. It's quick & easy.

passing variable to function

P: n/a

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

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Thomas,

I did not read all, however have a look at overloading.

http://msdn.microsoft.com/library/de...ingmethods.asp

I think that in that are all your current questions.

(And try to use VBNet code, On Error is not really that way).

I hope this helps,

Cor
Nov 21 '05 #2

P: n/a


th*****@msala.net wrote:
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.

--
Larry Lard
Replies to group please

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

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


Nov 21 '05 #3

P: n/a


th*****@msala.net wrote:
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.

--
Larry Lard
Replies to group please

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

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


Nov 21 '05 #4

P: n/a
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

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.