【机房重构】——减少代码冗余
来源:互联网 发布:2017年开淘宝店 编辑:程序博客网 时间:2024/05/11 21:38
前言
在第一版机房收费系统中,有很多都是重复的代码,代码冗余的现象很严重,降低了代码的利用率,所以在这一版机房重构中,就想着减少重复代码,减少不必要的代码。下面是我的一些总结,一起来看一下吧。
叙述
一、清空所有信息
'清空所有信息 Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click Dim ctrl As Control For Each ctrl In Me.Controls '清空所有的文本框 If TypeOf ctrl Is TextBox Then ctrl.Text = "" '清空所有的下拉框 ElseIf TypeOf ctrl Is ComboBox Then ctrl.Text = "" End If Next End Sub
二、判断控件是否为数字
在模块中定义一个公共的类,然后再定义一个函数,方便供所有的窗体使用,代码复用
'判断是否为数字 Public Shared Function IsNumeric(ByVal arrayControl() As Control) As Boolean Dim bytestr As Control Dim reg As Regex '正则表达式 Dim str As String = "^[0-9]+$" reg = New Regex(str) For Each bytestr In arrayControl Dim ma As Match ma = reg.Match(bytestr.Text) If ma.Success Then Else MessageBox.Show(bytestr.Text & "请输入数字!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) bytestr.Focus() Return True Exit Function End If Next Return False End FunctionU层调用如下
Dim arrayControl() As Control ReDim Preserve arrayControl(3) arrayControl(0) = txtRate arrayControl(1) = txtTeprate arrayControl(2) = txtLeastTime arrayControl(3) = txtLimitcash If UITerm.IsNumeric(arrayControl) Then Exit Sub End If三、判断控件是否为空
'判断数组控件是否为空 Public Shared Function IsEmptyText(ByVal arrayControl() As Control) As Boolean Dim control As Control '遍历结构体数组中的所有元素,如果控件文本为空,则给出警告提示 For Each control In arrayControl '遍历数组中所有元素 If TypeOf control Is TextBox Then '判断控件是不是文本框 If control.Text.Trim = "" Then '判断文本框内容是否为空 MessageBox.Show(control.Text & "文本框不能为空!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) control.Focus() Return True Exit Function End If ElseIf TypeOf control Is ComboBox Then '判断控件是不是组合框 If control.Text.Trim = "" Then MessageBox.Show(control.Text & "组合框不能为空!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) control.Focus() Return True Exit Function End If End If Next Return False End FunctionU层调用如上
小结
用更少的代码可以达到相同的目的,所以我们何乐而不为呢?
1 0
- 【机房重构】——减少代码冗余
- [机房重构]错误记录——代码编写很重要
- 【机房重构】——七层登陆代码分析
- 用 Groovy 减少代码冗余
- 机房重构—七层包图
- 机房重构—注册
- c#编译器的减少冗余代码优化
- Java Lombok 减少代码冗余 利刃
- 使用APT减少MVP的冗余代码
- 报表性能优化专题二—减少冗余单元格
- 【个人机房重构】——SqlHelper
- 机房重构——三层登录
- 【机房重构】——Sqlhelper
- 机房重构——三层架构
- 【机房重构】——触发器
- 机房重构——存储过程
- 【机房重构】——报表实现
- 机房重构——存储过程
- -PHP面向对象(OOP)编程入门-6.如何去使用对象中的成员
- Dell H310 Mini Raid卡,新加入热备盘显示Foreign的解决方法
- HDU 2553 (DFS) N皇后问题
- Bear and Friendship Condition
- window.location.hash属性介绍
- 【机房重构】——减少代码冗余
- MongoCola使用教程 1
- [Leetcode]7. Reverse Integer
- 水题 矩阵快速幂
- 3.19
- 行为型之状态模式
- visual studio2015生成程序时候出现This function or variable may be unsafe. Consider using ...instead
- 【bzoj2243】[SDOI2011]染色
- -PHP面向对象(OOP)编程入门-7.$this的使用