以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 想问一下自定义类调用属性的方法 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=147402) |
-- 作者:a937775799 -- 发布时间:2020/3/16 9:34:00 -- 想问一下自定义类调用属性的方法 我在全局代码里面,自定义了一个类 Public Class agreement Public 合同代码 As String Public 合同名称 As String Public 合同金额 As Decimal Public 结算金额 As Decimal Public 产生金额 As Decimal Sub New(ByVal htdm As String , ByVal htmc As String) 合同代码 = htdm 合同名称 = htmc End Sub End Class ‘’‘’‘我想在代码里面这么调用 dim a as agreement = new agreement("code" ,"name") a.合同金额= 10000 dim s as string = "合同金额" 我想通过s变量 调用 a对象的合同金额,类似a.s这效果 [此贴子已经被作者于2020/3/16 10:22:09编辑过]
|
-- 作者:有点蓝 -- 发布时间:2020/3/16 9:56:00 -- dim s as string = a.合同金额 |
-- 作者:a937775799 -- 发布时间:2020/3/16 10:17:00 -- 不是这样呀,s是通过程序获得的 s 是可以变的, agreemenr对象a通过调用这个s变量,s变量放的是一个agreement的属性名称, 然后a.s得到这个名称的值 |
-- 作者:有点蓝 -- 发布时间:2020/3/16 10:36:00 -- 没看懂,截图并详细举例说明 |
-- 作者:a937775799 -- 发布时间:2020/3/16 10:50:00 -- Public Class agreement Public 合同代码 As String Public 合同名称 As String Public 合同金额 As Decimal Public 结算金额 As Decimal Public 产生金额 As Decimal Sub New(ByVal htdm As String , ByVal htmc As String) 合同代码 = htdm 合同名称 = htmc End Sub End Class dim a as agreement = new agreement("code_a" ,"name_a") a.合同金额= 10000 a.结算金额= 11000 a.产生金额=5000 dim b as agreement = new agreement("code_b" ,"name_b") b.合同金额= 100000 b.结算金额= 110000 b.产生金额=50000 dim c as agreement = new agreement("code_c" ,"name_c") c.合同金额= 1000000 c.结算金额= 1100000 c.产生金额=500000 dim l as list(of agreement) = new list() l.add(a) l.add(b) l.add(c) dim sn() as string = {"合同金额","结算金额"} \'\'\'我这个数组可以改添加删除 for i as integer = 0 to sn.length - 1 for each a as agreement in l output.show( a.sn(i) ) next next |
-- 作者:有点蓝 -- 发布时间:2020/3/16 11:06:00 -- 没有办法这样用,变量名称是无法使用变量替代的,只能是 dim sn() as string = {"合同金额","结算金额"} \'\'\'我这个数组可以改添加删除 for i as integer = 0 to sn.length - 1 for each a as agreement in l if sn(i) = "合同金额" output.show( a.合同金额 ) else output.show( a.结算金额 ) endif next next |
-- 作者:有点蓝 -- 发布时间:2020/3/16 11:10:00 -- 或者扩展一下类型 Public Class agreement Public 合同代码 As String Public 合同名称 As String Public 合同金额 As Decimal Public 结算金额 As Decimal Public 产生金额 As Decimal Sub New(ByVal htdm As String , ByVal htmc As String) 合同代码 = htdm 合同名称 = htmc End Sub public function getvalue(name asstring) as Decimal if name = "合同金额" then return 合同金额 elseif name = "结算金额" then return 结算金额 end if end function End Class ------------- dim sn() as string = {"合同金额","结算金额"} \'\'\'我这个数组可以改添加删除 for i as integer = 0 to sn.length - 1 for each a as agreement in l output.show( a.getvalue(sn(i) ) next next |
-- 作者:a937775799 -- 发布时间:2020/3/16 11:23:00 -- 这不行吧 我要是 十几个属性呢 |
-- 作者:有点蓝 -- 发布时间:2020/3/16 11:29:00 -- 那就只能写十几个判断。或者百度一下学学怎么使用反射:https://www.baidu.com/baidu?word=vb.net+反射取属性值 |
-- 作者:a937775799 -- 发布时间:2020/3/16 16:03:00 -- 网上看vb.net代码Imports System.Reflection 这个 那有呀 我在全局代码里面 编写也不行呢
|