Passing varibles using "Byref"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter

    #1

    Passing varibles using "Byref"

    I will excute the code below to get MethodName array. These codes are
    edited under VS2003. Now I want to upgrade it to VS2005,but VS2005 gives me
    a varning message: The varible "sSourceMethodN ame" may throw a Null
    reference exception because it passes using "Byref" before being assigned.

    Although it's just a warning I still want to let ift fit the VS2005's
    recommended mode.

    Any suggestions will be appreciated,

    Peter

    '------------------------------------------
    Dim sSourceSampleNa me() As String
    Dim Count as integer
    Count = GetMethodName( sSourceMethodNa me)

    Public Function GetMethodName( ByRef sMethodName() As String) As Integer
    Dim odr As OleDbDataReader
    Dim i, j As Integer

    j = GetMethodCount( ) 'Defined in other places
    ReDim sMethodName(j - 1)

    Dim oCmd As New OleDbCommand

    'Connection ConnImport has been opened
    With oCmd
    .Connection = ConnImport
    .CommandType = CommandType.Sto redProcedure
    .CommandText = "udpGetMethodNa me"
    End With

    odr = oCmd.ExecuteRea der
    Do While odr.Read()
    sMethodName(i) = odr(0)
    i += 1
    Loop

    If Not odr.IsClosed Then odr.Close()
    Return j

    End Function
    '--------------------------------------------------------------


  • Cor Ligthert [MVP]

    #2
    Re: Passing varibles using "Byref&quo t;

    Peter,

    Get rid of that fixed array and use an arraylist, than you can pass byval
    and your program becomes direct more efficient as well.

    Cor

    "Peter" <zlxmqyt@sina.c omschreef in bericht
    news:OI4t5uIMHH A.600@TK2MSFTNG P02.phx.gbl...
    >I will excute the code below to get MethodName array. These codes are
    >edited under VS2003. Now I want to upgrade it to VS2005,but VS2005 gives me
    >a varning message: The varible "sSourceMethodN ame" may throw a Null
    >reference exception because it passes using "Byref" before being assigned.
    >
    Although it's just a warning I still want to let ift fit the VS2005's
    recommended mode.
    >
    Any suggestions will be appreciated,
    >
    Peter
    >
    '------------------------------------------
    Dim sSourceSampleNa me() As String
    Dim Count as integer
    Count = GetMethodName( sSourceMethodNa me)
    >
    Public Function GetMethodName( ByRef sMethodName() As String) As Integer
    Dim odr As OleDbDataReader
    Dim i, j As Integer
    >
    j = GetMethodCount( ) 'Defined in other places
    ReDim sMethodName(j - 1)
    >
    Dim oCmd As New OleDbCommand
    >
    'Connection ConnImport has been opened
    With oCmd
    .Connection = ConnImport
    .CommandType = CommandType.Sto redProcedure
    .CommandText = "udpGetMethodNa me"
    End With
    >
    odr = oCmd.ExecuteRea der
    Do While odr.Read()
    sMethodName(i) = odr(0)
    i += 1
    Loop
    >
    If Not odr.IsClosed Then odr.Close()
    Return j
    >
    End Function
    '--------------------------------------------------------------
    >
    >

    Comment

    • Peter

      #3
      Re: Passing varibles using &quot;Byref&quo t;

      Thanks for your reply, Cor,

      I will try to use arraylist to get rid of that fixed array problem.
      But I also want to get the return values by calling a function. I used
      "Byref" because the function can change the "Byref" parameter just like a
      return value. Can I still use "Byref" under VS2005?

      Peter


      Comment

      • Tom Leylan

        #4
        Re: Passing varibles using &quot;Byref&quo t;

        Peter:

        I think you'll find the use of the array has little to do with it. You
        should use a collection class, it's just easier but the problem is that you
        don't need to declare the parameter as ByRef. I just replied to another
        message along the same lines. An array "is" a reference, you pass it ByVal.
        When the value gets to the function it is a reference to the array.

        You _can_ pass it by reference but you open it up to be completely swapped
        out. Create a new array in your function and assign that one to the ByRef
        variable and you will see you destroyed the original one.

        Picture what is happening in the computer when you pass variables to
        functions. They are placed onto the "stack" if you had a 20,000 element
        array would we imagine it would push 20,000 values onto the stack? It
        always pushes a single value that acts as a pointer to where the array is.

        Don't worry about keywords like ByRef... conceptually languages always have
        the option to pass values or references, it isn't something they can
        eliminate in VS2005 or even VS2009.

        Tom


        "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
        news:%232x8M4IM HHA.4376@TK2MSF TNGP04.phx.gbl. ..
        Peter,
        >
        Get rid of that fixed array and use an arraylist, than you can pass byval
        and your program becomes direct more efficient as well.
        >
        Cor
        >
        "Peter" <zlxmqyt@sina.c omschreef in bericht
        news:OI4t5uIMHH A.600@TK2MSFTNG P02.phx.gbl...
        >>I will excute the code below to get MethodName array. These codes are
        >>edited under VS2003. Now I want to upgrade it to VS2005,but VS2005 gives
        >>me a varning message: The varible "sSourceMethodN ame" may throw a Null
        >>reference exception because it passes using "Byref" before being assigned.
        >>
        >Although it's just a warning I still want to let ift fit the VS2005's
        >recommended mode.
        >>
        >Any suggestions will be appreciated,
        >>
        >Peter
        >>
        >'------------------------------------------
        >Dim sSourceSampleNa me() As String
        >Dim Count as integer
        >Count = GetMethodName( sSourceMethodNa me)
        >>
        >Public Function GetMethodName( ByRef sMethodName() As String) As Integer
        > Dim odr As OleDbDataReader
        > Dim i, j As Integer
        >>
        > j = GetMethodCount( ) 'Defined in other places
        > ReDim sMethodName(j - 1)
        >>
        > Dim oCmd As New OleDbCommand
        >>
        > 'Connection ConnImport has been opened
        > With oCmd
        > .Connection = ConnImport
        > .CommandType = CommandType.Sto redProcedure
        > .CommandText = "udpGetMethodNa me"
        > End With
        >>
        > odr = oCmd.ExecuteRea der
        > Do While odr.Read()
        > sMethodName(i) = odr(0)
        > i += 1
        > Loop
        >>
        > If Not odr.IsClosed Then odr.Close()
        > Return j
        >>
        >End Function
        >'--------------------------------------------------------------
        >>
        >>
        >
        >

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: Passing varibles using &quot;Byref&quo t;

          Tom,

          There is a redim in Peter's code, in my idea that is creating a complete new
          array at a new point. I am not sure if you can than pass by value and get it
          back on the same position. My idea was not.

          Therefore my answer was as it was.

          Cor

          "Tom Leylan" <tleylan@nospam .netschreef in bericht
          news:%2389nRnOM HHA.4916@TK2MSF TNGP06.phx.gbl. ..
          Peter:
          >
          I think you'll find the use of the array has little to do with it. You
          should use a collection class, it's just easier but the problem is that
          you don't need to declare the parameter as ByRef. I just replied to
          another message along the same lines. An array "is" a reference, you pass
          it ByVal. When the value gets to the function it is a reference to the
          array.
          >
          You _can_ pass it by reference but you open it up to be completely swapped
          out. Create a new array in your function and assign that one to the ByRef
          variable and you will see you destroyed the original one.
          >
          Picture what is happening in the computer when you pass variables to
          functions. They are placed onto the "stack" if you had a 20,000 element
          array would we imagine it would push 20,000 values onto the stack? It
          always pushes a single value that acts as a pointer to where the array is.
          >
          Don't worry about keywords like ByRef... conceptually languages always
          have the option to pass values or references, it isn't something they can
          eliminate in VS2005 or even VS2009.
          >
          Tom
          >
          >
          "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
          news:%232x8M4IM HHA.4376@TK2MSF TNGP04.phx.gbl. ..
          >Peter,
          >>
          >Get rid of that fixed array and use an arraylist, than you can pass byval
          >and your program becomes direct more efficient as well.
          >>
          >Cor
          >>
          >"Peter" <zlxmqyt@sina.c omschreef in bericht
          >news:OI4t5uIMH HA.600@TK2MSFTN GP02.phx.gbl...
          >>>I will excute the code below to get MethodName array. These codes are
          >>>edited under VS2003. Now I want to upgrade it to VS2005,but VS2005 gives
          >>>me a varning message: The varible "sSourceMethodN ame" may throw a Null
          >>>reference exception because it passes using "Byref" before being
          >>>assigned.
          >>>
          >>Although it's just a warning I still want to let ift fit the VS2005's
          >>recommended mode.
          >>>
          >>Any suggestions will be appreciated,
          >>>
          >>Peter
          >>>
          >>'------------------------------------------
          >>Dim sSourceSampleNa me() As String
          >>Dim Count as integer
          >>Count = GetMethodName( sSourceMethodNa me)
          >>>
          >>Public Function GetMethodName( ByRef sMethodName() As String) As Integer
          >> Dim odr As OleDbDataReader
          >> Dim i, j As Integer
          >>>
          >> j = GetMethodCount( ) 'Defined in other places
          >> ReDim sMethodName(j - 1)
          >>>
          >> Dim oCmd As New OleDbCommand
          >>>
          >> 'Connection ConnImport has been opened
          >> With oCmd
          >> .Connection = ConnImport
          >> .CommandType = CommandType.Sto redProcedure
          >> .CommandText = "udpGetMethodNa me"
          >> End With
          >>>
          >> odr = oCmd.ExecuteRea der
          >> Do While odr.Read()
          >> sMethodName(i) = odr(0)
          >> i += 1
          >> Loop
          >>>
          >> If Not odr.IsClosed Then odr.Close()
          >> Return j
          >>>
          >>End Function
          >>'--------------------------------------------------------------
          >>>
          >>>
          >>
          >>
          >
          >

          Comment

          • dgk

            #6
            Re: Passing varibles using &quot;Byref&quo t;

            On Fri, 5 Jan 2007 14:33:12 +0800, "Peter" <zlxmqyt@sina.c omwrote:
            >Thanks for your reply, Cor,
            >
            >I will try to use arraylist to get rid of that fixed array problem.
            >But I also want to get the return values by calling a function. I used
            >"Byref" because the function can change the "Byref" parameter just like a
            >return value. Can I still use "Byref" under VS2005?
            >
            >Peter
            >
            ByVal often does allow the object's properties to be changed (value
            type vs reference type objects). Simple things like integers and
            strings are value types and a copy is actually made and passed to the
            function so changes to them are indeed lost. But most things, and I'm
            not sure about the arraylist that Cor suggested, can be changed within
            a subroutine or function and persist when the sub exits.

            Try it yourself. Create a simple object with a text property and pass
            it either byval or byref to a subroutine and have that sub change the
            property. When it gets back to the calling program, it won't matter
            how you passed it, the property is still changed. Try it again, but
            this time have the sub set the parameter to a new instance of the
            object. That will make a difference.

            Comment

            • Tom Leylan

              #7
              Re: Passing varibles using &quot;Byref&quo t;

              Ah yes the redim might be a problem. Might be interesting to find out (I'm
              not likely to redim an array) so I won't do it however :-) Clearly he would
              be better of with a dynamic array.

              It seems as I look the code over again that the return value "j" is just the
              value of GetMethodCount( ) which likely could be called before
              GetMethodName() and in that way the count wouldn't have to be returned, the
              array could be resized prior to calling GetMethodName() and if it was zero
              the call to GetMethodName() could be skipped entirely.

              Tom

              "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
              news:OD32vEQMHH A.1912@TK2MSFTN GP02.phx.gbl...
              Tom,
              >
              There is a redim in Peter's code, in my idea that is creating a complete
              new array at a new point. I am not sure if you can than pass by value and
              get it back on the same position. My idea was not.
              >
              Therefore my answer was as it was.
              >
              Cor
              >
              "Tom Leylan" <tleylan@nospam .netschreef in bericht
              news:%2389nRnOM HHA.4916@TK2MSF TNGP06.phx.gbl. ..
              >Peter:
              >>
              >I think you'll find the use of the array has little to do with it. You
              >should use a collection class, it's just easier but the problem is that
              >you don't need to declare the parameter as ByRef. I just replied to
              >another message along the same lines. An array "is" a reference, you
              >pass it ByVal. When the value gets to the function it is a reference to
              >the array.
              >>
              >You _can_ pass it by reference but you open it up to be completely
              >swapped out. Create a new array in your function and assign that one to
              >the ByRef variable and you will see you destroyed the original one.
              >>
              >Picture what is happening in the computer when you pass variables to
              >functions. They are placed onto the "stack" if you had a 20,000 element
              >array would we imagine it would push 20,000 values onto the stack? It
              >always pushes a single value that acts as a pointer to where the array
              >is.
              >>
              >Don't worry about keywords like ByRef... conceptually languages always
              >have the option to pass values or references, it isn't something they can
              >eliminate in VS2005 or even VS2009.
              >>
              >Tom
              >>
              >>
              >"Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
              >news:%232x8M4I MHHA.4376@TK2MS FTNGP04.phx.gbl ...
              >>Peter,
              >>>
              >>Get rid of that fixed array and use an arraylist, than you can pass
              >>byval and your program becomes direct more efficient as well.
              >>>
              >>Cor
              >>>
              >>"Peter" <zlxmqyt@sina.c omschreef in bericht
              >>news:OI4t5uIM HHA.600@TK2MSFT NGP02.phx.gbl.. .
              >>>>I will excute the code below to get MethodName array. These codes are
              >>>>edited under VS2003. Now I want to upgrade it to VS2005,but VS2005 gives
              >>>>me a varning message: The varible "sSourceMethodN ame" may throw a Null
              >>>>reference exception because it passes using "Byref" before being
              >>>>assigned.
              >>>>
              >>>Although it's just a warning I still want to let ift fit the VS2005's
              >>>recommende d mode.
              >>>>
              >>>Any suggestions will be appreciated,
              >>>>
              >>>Peter
              >>>>
              >>>'------------------------------------------
              >>>Dim sSourceSampleNa me() As String
              >>>Dim Count as integer
              >>>Count = GetMethodName( sSourceMethodNa me)
              >>>>
              >>>Public Function GetMethodName( ByRef sMethodName() As String) As
              >>>Integer
              >>> Dim odr As OleDbDataReader
              >>> Dim i, j As Integer
              >>>>
              >>> j = GetMethodCount( ) 'Defined in other places
              >>> ReDim sMethodName(j - 1)
              >>>>
              >>> Dim oCmd As New OleDbCommand
              >>>>
              >>> 'Connection ConnImport has been opened
              >>> With oCmd
              >>> .Connection = ConnImport
              >>> .CommandType = CommandType.Sto redProcedure
              >>> .CommandText = "udpGetMethodNa me"
              >>> End With
              >>>>
              >>> odr = oCmd.ExecuteRea der
              >>> Do While odr.Read()
              >>> sMethodName(i) = odr(0)
              >>> i += 1
              >>> Loop
              >>>>
              >>> If Not odr.IsClosed Then odr.Close()
              >>> Return j
              >>>>
              >>>End Function
              >>>'--------------------------------------------------------------

              Comment

              • Tom Leylan

                #8
                Re: Passing varibles using &quot;Byref&quo t;

                Well I just tried it and as we knew it would it failed when the array is
                passed by value. Thank goodness :-) Clearly redimming the array creates a
                new reference and the calling routine never gets it (unless it was returned
                expressly of course).

                The ArrayList is doubtlessly the better option.


                "Tom Leylan" <tleylan@nospam .netwrote in message
                news:%23X85zhQM HHA.3668@TK2MSF TNGP02.phx.gbl. ..
                Ah yes the redim might be a problem. Might be interesting to find out
                (I'm not likely to redim an array) so I won't do it however :-) Clearly
                he would be better of with a dynamic array.
                >
                It seems as I look the code over again that the return value "j" is just
                the value of GetMethodCount( ) which likely could be called before
                GetMethodName() and in that way the count wouldn't have to be returned,
                the array could be resized prior to calling GetMethodName() and if it was
                zero the call to GetMethodName() could be skipped entirely.
                >
                Tom
                >
                "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                news:OD32vEQMHH A.1912@TK2MSFTN GP02.phx.gbl...
                >Tom,
                >>
                >There is a redim in Peter's code, in my idea that is creating a complete
                >new array at a new point. I am not sure if you can than pass by value and
                >get it back on the same position. My idea was not.
                >>
                >Therefore my answer was as it was.
                >>
                >Cor
                >>
                >"Tom Leylan" <tleylan@nospam .netschreef in bericht
                >news:%2389nRnO MHHA.4916@TK2MS FTNGP06.phx.gbl ...
                >>Peter:
                >>>
                >>I think you'll find the use of the array has little to do with it. You
                >>should use a collection class, it's just easier but the problem is that
                >>you don't need to declare the parameter as ByRef. I just replied to
                >>another message along the same lines. An array "is" a reference, you
                >>pass it ByVal. When the value gets to the function it is a reference to
                >>the array.
                >>>
                >>You _can_ pass it by reference but you open it up to be completely
                >>swapped out. Create a new array in your function and assign that one to
                >>the ByRef variable and you will see you destroyed the original one.
                >>>
                >>Picture what is happening in the computer when you pass variables to
                >>functions. They are placed onto the "stack" if you had a 20,000 element
                >>array would we imagine it would push 20,000 values onto the stack? It
                >>always pushes a single value that acts as a pointer to where the array
                >>is.
                >>>
                >>Don't worry about keywords like ByRef... conceptually languages always
                >>have the option to pass values or references, it isn't something they
                >>can eliminate in VS2005 or even VS2009.
                >>>
                >>Tom
                >>>
                >>>
                >>"Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                >>news:%232x8M4 IMHHA.4376@TK2M SFTNGP04.phx.gb l...
                >>>Peter,
                >>>>
                >>>Get rid of that fixed array and use an arraylist, than you can pass
                >>>byval and your program becomes direct more efficient as well.
                >>>>
                >>>Cor
                >>>>
                >>>"Peter" <zlxmqyt@sina.c omschreef in bericht
                >>>news:OI4t5uI MHHA.600@TK2MSF TNGP02.phx.gbl. ..
                >>>>>I will excute the code below to get MethodName array. These codes are
                >>>>>edited under VS2003. Now I want to upgrade it to VS2005,but VS2005
                >>>>>gives me a varning message: The varible "sSourceMethodN ame" may throw a
                >>>>>Null reference exception because it passes using "Byref" before being
                >>>>>assigned .
                >>>>>
                >>>>Although it's just a warning I still want to let ift fit the VS2005's
                >>>>recommend ed mode.
                >>>>>
                >>>>Any suggestions will be appreciated,
                >>>>>
                >>>>Peter
                >>>>>
                >>>>'------------------------------------------
                >>>>Dim sSourceSampleNa me() As String
                >>>>Dim Count as integer
                >>>>Count = GetMethodName( sSourceMethodNa me)
                >>>>>
                >>>>Public Function GetMethodName( ByRef sMethodName() As String) As
                >>>>Integer
                >>>> Dim odr As OleDbDataReader
                >>>> Dim i, j As Integer
                >>>>>
                >>>> j = GetMethodCount( ) 'Defined in other places
                >>>> ReDim sMethodName(j - 1)
                >>>>>
                >>>> Dim oCmd As New OleDbCommand
                >>>>>
                >>>> 'Connection ConnImport has been opened
                >>>> With oCmd
                >>>> .Connection = ConnImport
                >>>> .CommandType = CommandType.Sto redProcedure
                >>>> .CommandText = "udpGetMethodNa me"
                >>>> End With
                >>>>>
                >>>> odr = oCmd.ExecuteRea der
                >>>> Do While odr.Read()
                >>>> sMethodName(i) = odr(0)
                >>>> i += 1
                >>>> Loop
                >>>>>
                >>>> If Not odr.IsClosed Then odr.Close()
                >>>> Return j
                >>>>>
                >>>>End Function
                >>>>'--------------------------------------------------------------
                >
                >

                Comment

                • Peter

                  #9
                  Re: Passing varibles using &quot;Byref&quo t;

                  I read all your advice several times and understand I can use a object as a
                  parameter , pass it to a subroutine then change it and returne to the caller
                  no matter Byval or Byref.

                  I should separate GetMethodCount from GetMethodName.

                  I will create an arraylist object instead of an array.

                  Thanks for all of you, I think here is a warm family and I love this family,

                  Peter


                  Comment

                  Working...