passing variable to function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thomasp@msala.net

    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
  • Cor Ligthert

    #2
    Re: passing variable to function

    Thomas,

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



    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


    Comment

    • Larry Lard

      #3
      Re: passing variable to function



      thomasp@msala.n et wrote:[color=blue]
      > 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.[/color]

      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


      [color=blue]
      >
      > 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[/color]

      Comment

      • Larry Lard

        #4
        Re: passing variable to function



        thomasp@msala.n et wrote:[color=blue]
        > 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.[/color]

        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


        [color=blue]
        >
        > 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[/color]

        Comment

        • Chris Dunaway

          #5
          Re: passing variable to function

          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

          Comment

          Working...