用动态性取代静态性来处理数组或集合
这里面有两个例子,第一个是数组赋值的例子,第二个是 DataGrid 的动态列初始化
一、数组赋值的的例子
我定义了一个枚举类型和一个结构类型如下:
Public Enum SqlType '定义一个枚举类型
NoSelect = 0
[String] = 1
Numeric = 2
[Date] = 3
[Boolean] = 4
Table = 5
End Enum
Public Structure FieldItem '定义一个结构类型
Dim Text As String
Dim SqlField As String
Dim Type As SqlType
End Structure
NoSelect = 0
[String] = 1
Numeric = 2
[Date] = 3
[Boolean] = 4
Table = 5
End Enum
Public Structure FieldItem '定义一个结构类型
Dim Text As String
Dim SqlField As String
Dim Type As SqlType
End Structure
下面用到上面结构的数组:
Dim fieldItems(13) As QueryControl.FieldItem
fieldItems(0).Text = "控制号"
fieldItems(0).SqlField = "kzh"
fieldItems(0).Type = QueryControl.SqlType.String
fieldItems(1).Text = "名称"
fieldItems(1).SqlField = "mc"
fieldItems(1).Type = QueryControl.SqlType.String
fieldItems(2).Text = "单价"
fieldItems(2).SqlField = "dj"
fieldItems(2).Type = QueryControl.SqlType.Numeric
fieldItems(3).Text = "订购数量"
fieldItems(3).SqlField = "dgsl"
fieldItems(3).Type = QueryControl.SqlType.Numeric
fieldItems(4).Text = "ISBN号"
fieldItems(4).SqlField = "isbn"
fieldItems(4).Type = QueryControl.SqlType.String
fieldItems(5).Text = "订购日期"
fieldItems(5).SqlField = "dgrq"
fieldItems(5).Type = QueryControl.SqlType.Date
fieldItems(6).Text = "地址"
fieldItems(6).SqlField = "dz"
fieldItems(6).Type = QueryControl.SqlType.String
fieldItems(7).Text = "帐号"
fieldItems(7).SqlField = "zh"
fieldItems(7).Type = QueryControl.SqlType.String
fieldItems(8).Text = "开户银行"
fieldItems(8).SqlField = "khyh"
fieldItems(8).Type = QueryControl.SqlType.String
fieldItems(9).Text = "资料来源"
fieldItems(9).SqlField = "zlly"
fieldItems(9).Type = QueryControl.SqlType.String
fieldItems(10).Text = " 电话"
fieldItems(10).SqlField = "dh"
fieldItems(10).Type = QueryControl.SqlType.String
fieldItems(11).Text = "邮政编码"
fieldItems(11).SqlField = "yzbm"
fieldItems(11).Type = QueryControl.SqlType.String
fieldItems(12).Text = "摘要"
fieldItems(12).SqlField = "zy"
fieldItems(12).Type = QueryControl.SqlType.String
fieldItems(13).Text = "备注"
fieldItems(13).SqlField = "bz"
fieldItems(13).Type = QueryControl.SqlType.String
fieldItems(0).Text = "控制号"
fieldItems(0).SqlField = "kzh"
fieldItems(0).Type = QueryControl.SqlType.String
fieldItems(1).Text = "名称"
fieldItems(1).SqlField = "mc"
fieldItems(1).Type = QueryControl.SqlType.String
fieldItems(2).Text = "单价"
fieldItems(2).SqlField = "dj"
fieldItems(2).Type = QueryControl.SqlType.Numeric
fieldItems(3).Text = "订购数量"
fieldItems(3).SqlField = "dgsl"
fieldItems(3).Type = QueryControl.SqlType.Numeric
fieldItems(4).Text = "ISBN号"
fieldItems(4).SqlField = "isbn"
fieldItems(4).Type = QueryControl.SqlType.String
fieldItems(5).Text = "订购日期"
fieldItems(5).SqlField = "dgrq"
fieldItems(5).Type = QueryControl.SqlType.Date
fieldItems(6).Text = "地址"
fieldItems(6).SqlField = "dz"
fieldItems(6).Type = QueryControl.SqlType.String
fieldItems(7).Text = "帐号"
fieldItems(7).SqlField = "zh"
fieldItems(7).Type = QueryControl.SqlType.String
fieldItems(8).Text = "开户银行"
fieldItems(8).SqlField = "khyh"
fieldItems(8).Type = QueryControl.SqlType.String
fieldItems(9).Text = "资料来源"
fieldItems(9).SqlField = "zlly"
fieldItems(9).Type = QueryControl.SqlType.String
fieldItems(10).Text = " 电话"
fieldItems(10).SqlField = "dh"
fieldItems(10).Type = QueryControl.SqlType.String
fieldItems(11).Text = "邮政编码"
fieldItems(11).SqlField = "yzbm"
fieldItems(11).Type = QueryControl.SqlType.String
fieldItems(12).Text = "摘要"
fieldItems(12).SqlField = "zy"
fieldItems(12).Type = QueryControl.SqlType.String
fieldItems(13).Text = "备注"
fieldItems(13).SqlField = "bz"
fieldItems(13).Type = QueryControl.SqlType.String
后来我修改了算法结构,要在 fieldItems 数组的最前面加上下面这行代码:
fieldItems(0).Text = "----全 部----"
这意味着上面数组的初始化,下标全部要修改,一两个修改还没什么,多起来就枯燥了。即使不在最前面,而在中间某个位置插入,后面的下标还是要往下错位改动,挺烦人的。
然而可以发现,数组的下标变化有递增的规律性,可以尝试用动态的变量来替换常量下标。
先来定义一个自增函数,.Net类库里面没有找到这样的方法(如果哪位大侠知道此方法,请告知本人,不胜感激),所以自己定义一个。
Private Function Inc(ByRef iCount As Integer, Optional ByVal delta As Integer = 1) As Integer
iCount += delta
Return iCount
End Function
iCount += delta
Return iCount
End Function
上面代码中参数 iCount 是以引用方式传参的。然后利用这个自增函数来更改数组下标变量:
#Region " 用变量作数组下标 "
Dim fieldItems(14) As FieldItem
Dim i As Integer = -1
fieldItems(Inc(i)).Text = "----全 部----" 'i==0
fieldItems(Inc(i)).Text = "控制号" 'i==1
fieldItems(i).SqlField = "kzh"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "名称" 'i==2
fieldItems(i).SqlField = "mc"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "单价" 'i==3
fieldItems(i).SqlField = "dj"
fieldItems(i).Type = QueryControl.SqlType.Numeric
fieldItems(Inc(i)).Text = "订购数量" 'i==4
fieldItems(i).SqlField = "dgsl"
fieldItems(i).Type = QueryControl.SqlType.Numeric
fieldItems(Inc(i)).Text = "ISBN号"
fieldItems(i).SqlField = "isbn"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "订购日期"
fieldItems(i).SqlField = "dgrq"
fieldItems(i).Type = QueryControl.SqlType.Date
fieldItems(Inc(i)).Text = "地址" 'i==7
fieldItems(i).SqlField = "dz"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "帐号"
fieldItems(i).SqlField = "zh"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "开户银行"
fieldItems(i).SqlField = "khyh"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "资料来源" 'i==10
fieldItems(i).SqlField = "zlly"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = " 电话"
fieldItems(i).SqlField = "dh"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "邮政编码"
fieldItems(i).SqlField = "yzbm"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "摘要"
fieldItems(i).SqlField = "zy"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "备注" ' 'i==14
fieldItems(i).SqlField = "bz"
fieldItems(i).Type = QueryControl.SqlType.String
#End Region
Dim fieldItems(14) As FieldItem
Dim i As Integer = -1
fieldItems(Inc(i)).Text = "----全 部----" 'i==0
fieldItems(Inc(i)).Text = "控制号" 'i==1
fieldItems(i).SqlField = "kzh"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "名称" 'i==2
fieldItems(i).SqlField = "mc"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "单价" 'i==3
fieldItems(i).SqlField = "dj"
fieldItems(i).Type = QueryControl.SqlType.Numeric
fieldItems(Inc(i)).Text = "订购数量" 'i==4
fieldItems(i).SqlField = "dgsl"
fieldItems(i).Type = QueryControl.SqlType.Numeric
fieldItems(Inc(i)).Text = "ISBN号"
fieldItems(i).SqlField = "isbn"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "订购日期"
fieldItems(i).SqlField = "dgrq"
fieldItems(i).Type = QueryControl.SqlType.Date
fieldItems(Inc(i)).Text = "地址" 'i==7
fieldItems(i).SqlField = "dz"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "帐号"
fieldItems(i).SqlField = "zh"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "开户银行"
fieldItems(i).SqlField = "khyh"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "资料来源" 'i==10
fieldItems(i).SqlField = "zlly"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = " 电话"
fieldItems(i).SqlField = "dh"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "邮政编码"
fieldItems(i).SqlField = "yzbm"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "摘要"
fieldItems(i).SqlField = "zy"
fieldItems(i).Type = QueryControl.SqlType.String
fieldItems(Inc(i)).Text = "备注" ' 'i==14
fieldItems(i).SqlField = "bz"
fieldItems(i).Type = QueryControl.SqlType.String
#End Region
用上面的方法,不管在哪个位置要插入一个元素,后面元素的相关代码都不用修改。只需轻轻改变一下数组的长度就可以。
二、 DataGrid 动态列初始化的例子
'此段代码要在 Page_Init 事件处理函数中,或者为它所调用
Dim column1 As New BoundColumn
column1.DataField = "gh"
column1.HeaderText = "工号"
dataGrid1.Columns.Add(column1)
Dim column2 As New BoundColumn
column2.DataField = "jszh"
column2.HeaderText = "借书证号"
dataGrid1.Columns.Add(column2)
Dim column3 As New BoundColumn
column3.DataField = "dh"
column3.HeaderText = "代号"
dataGrid1.Columns.Add(column3)
Dim column4 As New HyperLinkColumn
column4.DataNavigateUrlField = "id"
column4.DataNavigateUrlFormatString = Consts.UserInfoEditPage & "?pagemode=modify&id={0}"
column4.DataTextField = "yhname"
column4.HeaderText = "姓名"
dataGrid1.Columns.Add(column4)
Dim column5 As New BoundColumn
column5.DataField = "dw"
column5.HeaderText = "单位"
dataGrid1.Columns.Add(column5)
Dim column6 As New BoundColumn
column6.DataField = "fzrq"
column6.HeaderText = "发证日期"
dataGrid1.Columns.Add(column6)
Dim column7 As New BoundColumn
column7.DataField = "js_id"
column7.HeaderText = "角色"
dataGrid1.Columns.Add(column7)
Dim column8 As New ButtonColumn
column8.Text = "删除"
column8.HeaderText = "删除"
column8.CommandName = "Delete"
dataGrid1.Columns.Add(column8)
Dim column1 As New BoundColumn
column1.DataField = "gh"
column1.HeaderText = "工号"
dataGrid1.Columns.Add(column1)
Dim column2 As New BoundColumn
column2.DataField = "jszh"
column2.HeaderText = "借书证号"
dataGrid1.Columns.Add(column2)
Dim column3 As New BoundColumn
column3.DataField = "dh"
column3.HeaderText = "代号"
dataGrid1.Columns.Add(column3)
Dim column4 As New HyperLinkColumn
column4.DataNavigateUrlField = "id"
column4.DataNavigateUrlFormatString = Consts.UserInfoEditPage & "?pagemode=modify&id={0}"
column4.DataTextField = "yhname"
column4.HeaderText = "姓名"
dataGrid1.Columns.Add(column4)
Dim column5 As New BoundColumn
column5.DataField = "dw"
column5.HeaderText = "单位"
dataGrid1.Columns.Add(column5)
Dim column6 As New BoundColumn
column6.DataField = "fzrq"
column6.HeaderText = "发证日期"
dataGrid1.Columns.Add(column6)
Dim column7 As New BoundColumn
column7.DataField = "js_id"
column7.HeaderText = "角色"
dataGrid1.Columns.Add(column7)
Dim column8 As New ButtonColumn
column8.Text = "删除"
column8.HeaderText = "删除"
column8.CommandName = "Delete"
dataGrid1.Columns.Add(column8)
这里面有 8 个 DataGridColumn 的子类对象,column1,column2,...column8。这里面用到了 8 个临时变量,并且命名怪怪的,如果突然要在某个位置插入一个datagrid 列,那后面列的名字改不改?不改也可以,但是给临时变量命名就怪怪的,简直是“鸡肋”,其实有方法可以免掉这样的临时变量,并且不给具体的列命名。代码如下:
'此段代码要在 Page_Init 事件处理函数中,或者为它所调用
Dim bc As BoundColumn
Dim hc As HyperLinkColumn
Dim btc As ButtonColumn
dataGrid1.Columns.Add(New BoundColumn)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
bc.DataField = "gh"
bc.HeaderText = "工号"
dataGrid1.Columns.Add(New BoundColumn)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
bc.DataField = "jszh"
bc.HeaderText = "借书证号"
dataGrid1.Columns.Add(New BoundColumn)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
bc.DataField = "dh"
bc.HeaderText = "代号"
dataGrid1.Columns.Add(New HyperLinkColumn)
hc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
hc.DataNavigateUrlField = "id"
hc.DataNavigateUrlFormatString = Consts.UserInfoEditPage & "?pagemode=modify&id={0}"
hc.DataTextField = "yhname"
hc.HeaderText = "姓名"
dataGrid1.Columns.Add(New BoundColumn)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
bc.DataField = "dw"
bc.HeaderText = "单位"
dataGrid1.Columns.Add(New BoundColumn)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
bc.DataField = "fzrq"
bc.HeaderText = "发证日期"
dataGrid1.Columns.Add(New BoundColumn)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
bc.DataField = "js_id"
bc.HeaderText = "角色"
dataGrid1.Columns.Add(New ButtonColumn)
btc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
btc.Text = "删除"
btc.HeaderText = "删除"
btc.CommandName = "Delete"
Dim bc As BoundColumn
Dim hc As HyperLinkColumn
Dim btc As ButtonColumn
dataGrid1.Columns.Add(New BoundColumn)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
bc.DataField = "gh"
bc.HeaderText = "工号"
dataGrid1.Columns.Add(New BoundColumn)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
bc.DataField = "jszh"
bc.HeaderText = "借书证号"
dataGrid1.Columns.Add(New BoundColumn)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
bc.DataField = "dh"
bc.HeaderText = "代号"
dataGrid1.Columns.Add(New HyperLinkColumn)
hc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
hc.DataNavigateUrlField = "id"
hc.DataNavigateUrlFormatString = Consts.UserInfoEditPage & "?pagemode=modify&id={0}"
hc.DataTextField = "yhname"
hc.HeaderText = "姓名"
dataGrid1.Columns.Add(New BoundColumn)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
bc.DataField = "dw"
bc.HeaderText = "单位"
dataGrid1.Columns.Add(New BoundColumn)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
bc.DataField = "fzrq"
bc.HeaderText = "发证日期"
dataGrid1.Columns.Add(New BoundColumn)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
bc.DataField = "js_id"
bc.HeaderText = "角色"
dataGrid1.Columns.Add(New ButtonColumn)
btc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
btc.Text = "删除"
btc.HeaderText = "删除"
btc.CommandName = "Delete"
这样,不管要在那个位置插入、删除一个列,都不用触动程序的其他部分。在普通情况下,BoundColumn 类型的列占主要部分,那么下面这两行代码就可以复用:
dataGrid1.Columns.Add(New BoundColumn)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
bc = dataGrid1.Columns(dataGrid1.Columns.Count - 1)
用到的时候,直接 Ctrl+C, Ctrl+P 就可以了,不用改动字符。