[Source] CallByNameEx y argumentos aleatorios
BlackZeroX:
Este codigo que les dejo me arreglo una duda que tenia la cual me la respondio Google a una liga interesante ( Almenos para mi xP )
intentaba hacer algo similar a esto:
Código
Private Sub Form_Load()
Dim Args() As Variant
ReDim Args(0 To 1)
Args(0) = "hola Mundo"
Args(1) = "12"
CallByName Me, "Mensaje", VbMethod, Args()
End Sub
Public Sub mensaje(mensaje As String, Optional hola As Long)
MsgBox mensaje, vbOKOnly, hola
End Sub
pero no daba y google me respodio:
http://www.devx.com/tips/Tip/15422
Código
' Required for use in VB5!
Public Enum VbCallType
VbMethod = 1
VbGet = 2
VbLet = 4
VbSet = 8
End Enum
Public Function CallByNameEx(Obj As Object, _
ProcName As String, CallType As VbCallType, _
Optional vArgsArray As Variant)
Dim oTLI As Object
Dim ProcID As Long
Dim numArgs As Long
Dim i As Long
Dim v()
On Error GoTo Handler
Set oTLI = CreateObject("TLI.TLIApplication")
ProcID = oTLI.InvokeID(Obj, ProcName)
If IsMissing(vArgsArray) Then
CallByNameEx = oTLI.InvokeHook( _
Obj, ProcID, CallType)
End If
If IsArray(vArgsArray) Then
numArgs = UBound(vArgsArray)
ReDim v(numArgs)
For i = 0 To numArgs
v(i) = vArgsArray(numArgs - i)
Next i
CallByNameEx = oTLI.InvokeHookArray( _
Obj, ProcID, CallType, v)
End If
Exit Function
Handler:
Debug.Print Err.Number, Err.Description
End Function
con lo cual ahora el CallByName bueno mejor dicho CallByNameEx acepta ahora expresiones de este tipo:
Código
Call CallByNameEx(Me, "xx", VbMethod, x)
Call CallByNameEx(Me, "xx", VbMethod, Array(1, 2))
Result=CallByNameEx(Me, "xx", VbMethod, x)
siendo que antes array() o pasandole un vector a CallByName No servia
Código
Private Sub Form_Load()
Dim Args() As Variant
ReDim Args(0 To 1)
Args(0) = "hola Mundo"
Args(1) = "12"
CallByNameEx Me, "Mensaje", VbMethod, Args()
End Sub
Public Sub mensaje(mensaje As String, Optional hola As Long)
MsgBox mensaje, vbOKOnly, hola
End Sub
Sangrienta Luna Infernal!¡.
LeandroA:
Interesante che, que aplicación sera TLI? saves cual es la refencia¿?
Saludos.
BlackZeroX:
TypeLib Information
TLBInf32.dll
Sangriento Infierno Lunar.
BlackZeroX:
Agregando la referencia... TypeLib Information:
Código
' Required for use in VB5!
Public Enum VbCallType
VbMethod = 1
VbGet = 2
VbLet = 4
VbSet = 8
End Enum
Public Function CallByNameEx(Obj As Object, ProcName As String, CallType As VbCallType, Optional vArgsArray As Variant)
On Error GoTo Handler
Dim oTLI As TLIApplication ' // Set oTLI = CreateObject("TLI.TLIApplication")
Dim ProcID As Long
Dim numArgs As Long
Dim i As Long
Dim v() As Variant
Set oTLI = New TLIApplication
If Not oTLI Is Nothing Then
ProcID = oTLI.InvokeID(Obj, ProcName)
If IsMissing(vArgsArray) Then
CallByNameEx = oTLI.InvokeHook(Obj, ProcID, CallType)
End If
If IsArray(vArgsArray) Then
numArgs = UBound(vArgsArray)
ReDim v(numArgs)
For i = 0 To numArgs
v(i) = vArgsArray(numArgs - i)
Next i
CallByNameEx = oTLI.InvokeHookArray(Obj, ProcID, CallType, v)
End If
End If
Exit Function
Handler:
Debug.Print Err.Number, Err.Description
End Function
Sangriento Infierno Lunar!¡.
Karcrack:
Tambien podrias hacer esto, no?
Código
Private Sub Form_Load()
Dim vParams(1) As Variant
vParams(0) = "Hi ho"
vParams(1) = 1500
CallByName Me, "Mensaje", VbMethod, vParams()
End Sub
Public Sub Mensaje(ByRef vParams() As Variant)
Call MsgBox(vParams(0), , vParams(1))
End Sub
Navegación
[#] Página Siguiente