2010 VBA - 传递类型作为参数 [英] 2010 VBA - Pass Type as parameter

查看:56
本文介绍了2010 VBA - 传递类型作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我想传递一个对象类型作为比较逻辑的参数.

I have a situation where i want to pass a object Type as a parameter for a comparison logic.

示例:

Sub Some (val as Control, typ as Type, ....)
   ...
   if typeof val is typ then
      ...
   end if
   ...
End Sub

我发现自己在多种方法中执行此逻辑模式,并希望将逻辑压缩到一个位置而不是多个位置以进行简化.

I have found myself doing this logic pattern in multiple methods and want to condense the logic into one location instead of multiple spots, for simplification.

这是验证此类结构的正确方法还是有更好的方法来做这些事情?

Is this the correct way of validating this type of structure or is there a better way of doing such things?

推荐答案

那没多久

如果您想将比较抽象为通用,这应该可以让您找到正确的逻辑.

If you want to abstract a comparison to be generic this should get you to the right logic.

Public Sub CompareType(val as Control, typ as String)
   ...
   if TypeName(val) = typ then
      ...
   end if
   ...
End Sub

基本上传递对象(val),如果您想进一步抽象,可以是变体,然后如果您知道将为该类型返回的字符串值是什么,则将其作为字符串值传递

Basically pass the object (val), can be variant if you want to take one step up of abstraction, and then if you know what the String value that will be returned for that Type then pass it as a String value

在代码中运行:

...
AutoSizeControl CurrentDb, frm, "Textbox"
...

模块声明:

Public Sub AutoSizeControl(ByRef db As Database, ByRef frm As Form, typ As String)
    Dim ctl As Control, i As Integer
    For i = 0 To (frm.Controls.Count - 1)
        Set ctl = frm.Controls(i)
        If TypeName(ctrl) = typ Then
            ctl.ColumnWidth = -2
        End If
    Next
End Sub

希望这能帮助任何可能需要这种抽象的人.

Hope this helps anyone else that may need such abstraction.

这篇关于2010 VBA - 传递类型作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆