参数引用调用无效,问为什么
部分代码如下:
Private Sub txtMain_KeyPress(KeyAscii As Integer)
MakeUsable (KeyAscii)
RaiseEvent KeyPress(KeyAscii)
End Sub
Private Sub MakeUsable(word As Integer)
If (word > = 48 And word <= 57) Then
If (m_InputType <> Letters) = False Then word = 0
ElseIf (word > = 97 And word <= 122) Then
If (m_InputType = Letters And m_CaseType <> UpperCase _
Or m_InputType = AllInput And m_CaseType = LowerCase) = False Then word = 0
ElseIf (word > = 65 And word <= 90) Then
If (m_InputType = Letters And m_CaseType <> LowerCase _
Or m_InputType = AllInput And m_CaseType = UpperCase) = False Then word = 0
Else
word = 0
End If
End Sub
目的是使得输入的字符必须是满足要求的,否则置为空,但是引用调用的时候虽然word能够置0,但是并没有影响到KeyAscii的值,这是为什么?小弟初来乍到,多多关照。
[解决办法]
我搞错了,没弄懂你的意思:重来
Private Sub MakeUsable(word As Integer)
这句话把KeyAscii的值复制到word里,这之后,两者是独立的,互相不影响。
所以你改变word,不会影响keyascii,在c++中,通过传引用或指针能达到同时修改的目的。
但是,看你的程序不需要那么做,我修改一下吧
Private Sub txtMain_KeyPress(KeyAscii As Integer)
Dim iModiValue As Integer
iModiValue = MakeUsable(KeyAscii)
RaiseEvent KeyPress(iModiValue)
End Sub
Private Function MakeUsable(word As Integer) As Integer
If (word > = 48 And word <= 57) Then
If (m_InputType <> Letters) = False Then MakeUsable = 0
ElseIf (word > = 97 And word <= 122) Then
If (m_InputType = Letters And m_CaseType <> UpperCase _
Or m_InputType = AllInput And m_CaseType = LowerCase) = False Then MakeUsable = 0
ElseIf (word > = 65 And word <= 90) Then
If (m_InputType = Letters And m_CaseType <> LowerCase _
Or m_InputType = AllInput And m_CaseType = UpperCase) = False Then MakeUsable = 0
Else
MakeUsable = word
End If
End Function
说实话,你的程序的内聚性太差,很难读,例如MakeUsable里面的好多变量都找不到出处
[解决办法]
楼主的 txtMain 是在自定义的控件里还是自定义的类里面,
不管哪种情况,我测试都成功的
Private Sub MakeUsable(byref word As Integer)
byRef 是按引用传递,也就是方法里改变word的值也会改原相应实参的值,
且不加byref VB中也默认是按引用传递的
问题应该不出在是方法还是函数中,希望楼主多贴一些代码出来
参数引用调用无效,问为什么解决办法
参数引用调用无效,问为什么部分代码如下:PrivateSubtxtMain_KeyPress(KeyAsciiAsInteger)MakeUsable(KeyAs
