473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

passing variable to function


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
4 2536
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


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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
4
by: hello smith | last post by:
I have a lot of functions that add values to an array. They alos update a global variable of type int. Currently, I use a global variable to hold this array. All functions access this array...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.