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 4 2537 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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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) {...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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);
|
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...
|
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: 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
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |