CallByName请教高手相助,谢谢!!!

msdn165168 高中 2011-03-19 06:32:48
我想通过CallByName(对象,函数名,CallType,参数)来执行一个对象的一个方法

在类模块里:
有:
Private Sub PriAdd(ByVal TEMP As String, ByVal strTemp As String )
If InStr(TEMP, strTemp) > 0 Then Primitives.Add(strTemp, CallByName(strTemp))End Sub

函数为(同在类模块)
Public Function CLOSE() As Single
CLOSE = hq(CSTJJ / 1000
End Function

call PriAdd(newTEMP , CLOSE )

CallByName()挎号里该如何写,谢谢!!!
...全文
39 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xingyuebuyu 2011-03-19
Imports System.Reflection

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
''这个是VB6.0的方法,只能调用Public的方法
CallByName(Me, "test", CallType.Method, New Object() {"abc", "def"})

''没有参数就设为nothing,有参数时就对后面的Object类型数组进行赋值
Dim s As Single = CallByName(Me, "CLOSE2", CallType.Method, Nothing)


'建议用反射
Dim mi As MethodInfo = Me.GetType.GetMethod("test", BindingFlags.Instance Or BindingFlags.Public)
mi.Invoke(Me, New Object() {"abc", "def"})
End Sub

Public Sub test(ByVal s As String, ByVal s2 As String)
Console.WriteLine(s + s2)
End Sub

Public Sub test2()
Console.WriteLine("1111111")
End Sub

Public Function CLOSE2() As Single '()
CLOSE2 = 100 ' hq(CSTJJ).SPJ / 1000
End Function
End Class
回复
msdn165168 2011-03-19
谢谢两位的帮助!!!
我有点看不懂,还请帮助看看我的实际问题,谢谢!!!
我要在散表(Primitives)添加一个数值(检查已有就删除,再添加,如没有就添加)
Public Primitives As Hashtable
Private Sub mcjcdy(ByVal nebem As String)

Dim i As Long
Dim StrArr() As String = Split("OPEN,CLOSE,HIGH,LOW,VOL,CAPITAL,BARPOS,BIGORDER1,BIGORDER2,ORDER1,ORDER2,AMOUNT,ZLCC,TDCC,ZHCC,DHCC,SHCC,TDCCZH,ZHCCZH,SHCCZH,JGCJB,SMCJB,SHCJB,BSMCL,BSMRL,DJMCL,DJMRL,TUMCL,TUMRL", ",")
Do While i < UBound(StrArr)
If InStr(nebem, StrArr(i)) > 0 Then Call PriAdd(nebem, StrArr(i))
i += 1
Loop
End Sub


Private Sub PriAdd(ByVal TEMP As String, ByVal strTemp As String)

If InStr(TEMP, strTemp) > 0 Then
If Primitives.ContainsKey(strTemp) Then
Primitives.Remove(strTemp)
Primitives.Add(strTemp, CallByName(Me, strTemp, CallType.Method, strTemp)) Else
Primitives.Add(strTemp, CallByName(Me, strTemp, CallType.Method, New Object()))
End If
End If

End Sub

Public Function CLOSE() As Single '()
CLOSE = hq(CSTJJ).SPJ / 1000
End Function

红色部分要求能调用不同的(和strTemp一样名称的函数)函数值

就像下面这句一样:
If InStr(TEMP, "CLOSE") > 0 Then Primitives.Add("CLOSE", CLOSE)(执行时CLOSE就是100)

Public Function CLOSE() As Single '()
CLOSE =100‘ hq(CSTJJ).SPJ / 1000
End Function

还请高手相助,谢谢!!!
回复
红衣老大 2011-03-19
你百度一下反射的使用方法
回复
xingyuebuyu 2011-03-19
Imports System.Reflection

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
''这个是VB6.0的方法,只能调用Public的方法
CallByName(Me, "test", CallType.Method, New Object() {"abc", "def"})

'建议用反射
Dim mi As MethodInfo = Me.GetType.GetMethod("test", BindingFlags.Instance Or BindingFlags.Public)
mi.Invoke(Me, New Object() {"abc", "def"})
End Sub

Public Sub test(ByVal s As String, ByVal s2 As String)
Console.WriteLine(s + s2)
End Sub
End Class


http://msdn.microsoft.com/zh-cn/library/4d848zkb(v=VS.80).aspx
回复
相关推荐
发帖
VB
创建于2007-09-28

1.6w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
帖子事件
创建了帖子
2011-03-19 06:32
社区公告
暂无公告