473,666 Members | 2,053 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 2557
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
14929
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
10122
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
7639
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
2491
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 to each function or just access the global variables? I have tried using gprof, I did not get...
17
3592
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
8116
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 FirstDouble(ByVal array As Integer()) Dim i As Integer
1
3267
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. This routine would be called quite simply as follows: void main() {
12
2678
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
3297
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 ExcelService : SoapHttpClientProtocol {
12
2581
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 copying) If so, please quote part of the standard that says that. Assuming it is true, I can imagine two...
0
8444
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8869
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7386
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
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
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.