473,569 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ByV al 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.Strea mReader(strFile Name)

bolExitImport = False

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

strTempFileName = strFileName
strFileName = strFileName2

If funFileExists(s trFileName) Then
srdImportFile2 = New System.IO.Strea mReader(strFile Name)
Else
bolExitImport = True
Response = MsgBox("The Targets.htg import file was not
found, exiting import.", MsgBoxStyle.Msg BoxHelp, _
"File Not Found Error!")
End If

strFileName = strTempFileName
End If

End Sub

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

Public Function funFileExists(B yVal strFileName As String) As Boolean

Dim Attr As FileAttribute

On Error Resume Next
Attr = GetAttr(strFile Name)
If Err.Number <> 0 Then
funFileExists = False
ElseIf (Attr And FileAttribute.D irectory) 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.c om<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Nov 21 '05 #1
4 2550
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.n et 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 misunderstandin g 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(s 1 & 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(ByV al 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.Strea mReader(strFile Name)

bolExitImport = False

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

strTempFileName = strFileName
strFileName = strFileName2

If funFileExists(s trFileName) Then
srdImportFile2 = New System.IO.Strea mReader(strFile Name)
Else
bolExitImport = True
Response = MsgBox("The Targets.htg import file was not
found, exiting import.", MsgBoxStyle.Msg BoxHelp, _
"File Not Found Error!")
End If

strFileName = strTempFileName
End If

End Sub

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

Public Function funFileExists(B yVal strFileName As String) As Boolean

Dim Attr As FileAttribute

On Error Resume Next
Attr = GetAttr(strFile Name)
If Err.Number <> 0 Then
funFileExists = False
ElseIf (Attr And FileAttribute.D irectory) 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.c om<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


Nov 21 '05 #3


th*****@msala.n et 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 misunderstandin g 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(s 1 & 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(ByV al 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.Strea mReader(strFile Name)

bolExitImport = False

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

strTempFileName = strFileName
strFileName = strFileName2

If funFileExists(s trFileName) Then
srdImportFile2 = New System.IO.Strea mReader(strFile Name)
Else
bolExitImport = True
Response = MsgBox("The Targets.htg import file was not
found, exiting import.", MsgBoxStyle.Msg BoxHelp, _
"File Not Found Error!")
End If

strFileName = strTempFileName
End If

End Sub

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

Public Function funFileExists(B yVal strFileName As String) As Boolean

Dim Attr As FileAttribute

On Error Resume Next
Attr = GetAttr(strFile Name)
If Err.Number <> 0 Then
funFileExists = False
ElseIf (Attr And FileAttribute.D irectory) 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.c om<<<<<<------
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(s trFileName2) 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
14916
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
58
10077
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 code... TCHAR myArray; DoStuff(myArray);
39
7616
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 indicate: a) That I don't know enough b) Passing arguments by ref is bad
4
2488
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 global variable and add their values. Then they increment the global int variable. Is it faster to pass the array and the int variable by reference...
17
3577
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 (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
11
8103
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub...
1
3261
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 irrelavent. My syntax is surely where I am going wrong. I'd like to be able to call this routine to read different values from another device. ...
12
2669
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
3292
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class...
12
2573
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 by value temporaries are created?(I'm not touching yet the cases where standard allows optimizations from the side of implementations to avoid...
0
7700
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.