Byref Assignment order

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
it appears that the byref flag for a parameter is more like a: "Assignment on exit" than a "Here's my Address", is that true?

Reason:

VB.NET:
[u]SqlConnectionMgr[/u]
   Public Shadows Function Add(ByVal ConnectionString As String) As SqlConnection
      Dim con As New SqlConnection(ConnectionString)
      MyBase.Add(con)
      Return con
   End Function

   Protected Overrides Sub InsertItem(ByVal index As Integer, _
            ByVal item As System.Data.SqlClient.SqlConnection)
      AddHandler item.StateChange, AddressOf Me.Connection_StateChange
      AddHandler item.Disposed, AddressOf Me.Connection_Disposed
      MyBase.InsertItem(index, item)
   End Sub

   Public Function GetConnection(ByRef Connection As SqlClient.SqlConnection, _
            Optional ByVal TrapException As Boolean = True) As Boolean
      If CanConnect Then
         Try
            Connection = Add(_connstr.ConnectionString)
            Connection.Open()
            If Connection.State = ConnectionState.Open Then
               _state = MgrConnectionState.Connected
               Return True
            End If
         Catch ex As Exception When TrapException
            _ex = ex
            If ex.Message.ToLower.IndexOf("login failed").Between(0, 10) Then
               _state = MgrConnectionState.Failed
            Else : _state = MgrConnectionState.NoConnection
            End If
         End Try
         If Connection IsNot Nothing Then Connection.Dispose()
      Else : _state = MgrConnectionState.InvalidParameter
      End If
      Connection = Nothing
      Return False
   End Function

  Private Sub Connection_StateChange(ByVal sender As Object, _
            ByVal e As System.Data.StateChangeEventArgs) _
                  Handles con.StateChange
      RaiseEvent ConnectionEvent(Me, New MgrStateChangeEventArgs(sender, e))
      If e.CurrentState = ConnectionState.Closed Then
         DirectCast(sender, SqlConnection).Dispose()
      End If
   End Sub

VB.NET:
      Public Function Connect(ByRef Login As LoginStruct) As MgrConnectionState
  'i execute this
         If ([b]_connmgr.GetConnection(_conn)[/b]) AndAlso (_conn IsNot Nothing) Then
            _manager.SetConnection(_conn)
            _dsFilled = False
         End If
         Return _connmgr.Status
      End Function

      Private Sub _connmgr_ConnectionEvent(ByVal sender As Object, _
                  ByVal e As FlufLib.MgrStateChangeEventArgs) _
                                    Handles _connmgr.ConnectionEvent

      [b]'This never works because at this point of execution, _conn is still nothing[/b]
         If e.Connection.Equals(_conn) Then
            RaiseEvent ConnectionEvent(Me, New ConnectionEventArgs(e))
         End If
      End Sub

      [b]'This Event Never Fires on Connection.Open().[/b]
      Private Sub _conn_StateChange(ByVal sender As Object, _
                        ByVal e As System.Data.StateChangeEventArgs) _
                                                      Handles _conn.StateChange
         RaiseEvent ConnectionEvent(Me, New ConnectionEventArgs(e.CurrentState, e.OriginalState, sender))
      End Sub

Anyway around this? I have to trap when my _conn connection opened/closed, but through the ByRef Passing to the SqlConnectionMgr it is not assigned to a value until after i exit GetConnection().

Thanks
 
For reference types ByRef uses same address by pointer, ByVal uses a copy of the address. If you pass an actual object you can modify this object in real time through the parameter variable in both cases. If you assign a new object to a ByVal parameter variable then this does not change the input variable you had when you called the method, for ByRef you're using the same pointer so new assignments also apply to the source variable.
 
well,

i need by ref, because i am "assigning" to that variable and need that value. However, I did find the events worked fine when I pulled the assigned method call out of the if statement.

VB.NET:
if BooleanEval() then
  getconnection(_conn)
end if
that worked and both events fired, and _conn was fully assigned as it should be. But
VB.NET:
if getConnect(_conn) then
end if
did not work.
*shrug*
go figure
thanks though,
 
Back
Top