前言:
进入个人机房重构有一段时间了,这次的机房收费系统加入了七层,应用vb.net语言等等,学习了新的东西,这篇文章主要讲vb.net在机房这个系统的小知识。本篇主要介绍四个知识点:
核心:
(一)、传参与传实体
在机房重构开始的时候,一直是传实体,但是实体和值所占的空间可是差的很多。
:什么是传实体呢?在机房个人重构的时候,我们定义了一个实体层,在里面,我们写好了属性和方法:
我们需要更新的是卡表,充值表,学生表,这样就需要我们传实体比较好。
UI层
Dim Fifregister As New Facade.FRegister
Dim card As New Entity.ECard
Dim student As New Entity.EStudent
Dim recharge As New Entity.ERecharge
'卡表
card.CardNo = txtCardId.Text.Trim()
card.Nvr_Cash = txtCash.Text.Trim()
card.Type = cmbType.Text.Trim()
card.Riqi = Date.Now.ToString("yyyy - MM - dd")
card.Time = DateTime.Now.ToString("HH:mm:ss")
card.CheckStatus = True
card.UserStatus = True
'card.UserID =
'学生表
student.StudenID = txtStudentId.Text.Trim()
student.StudentName = txtName.Text.Trim()
student.Sex = cmbSex.Text.Trim()
student.Department = txtDepartment.Text.Trim()
student.Major = txtMajor.Text.Trim()
student.Grade = cmbGrade.Text.Trim()
student.classes = cmbClass.Text.Trim()
student.Explain = txtExplain.Text.Trim()
'充值表
recharge.CardNo = txtCardId.Text.Trim()
recharge.CheckStatus = False
recharge.Recharge = txtCash.Text.Trim()
recharge.RechargeDate = Date.Now.ToString("yyyy - MM - dd")
recharge.RechargeTime = DateTime.Now.ToString("HH:mm:ss")
Dim flag2 As String
flag2 = Fifregister.Ifregister(card)
If flag2 = True Then
Dim flag1 As String
flag1 = Fifregister.FAdd(card, student, recharge)
End If
B层 :这个是B层的一个方法,注意下面的Byval传的实体
Public Class BRegister
Public Function Ifuse(ByVal cardno As Entity.ECard) As String
Dim Iregister As IDAL.IRegister
Iregister = Factory.DataAccess.RegisterCardInfo
Dim myTable As New DataTable
myTable = Iregister.ifregister(cardno)
If myTable.Rows.Count = 0 Then
MsgBox("注册成功!")
Else
MsgBox("该卡号已经注册过,请重新注册!!")
End If
Return True
End Function
外观层、D层、接口层都是实体。
这是D层的一个方法,在赋值的时候,直接用实体.方法
Public Function ifregister(cardno As Entity.ECard) As DataTable Implements IDAL.IRegister.ifregister
Dim Sql As String
Dim table As New DataTable '中间变量用于存储从数据库中 查找信息
'声明并实例化参数数组
Dim sqlParams As SqlParameter() = {New SqlParameter("@Chr_CardNo", cardno.CardNo),
New SqlParameter("@Chr_UserStatus", cardno.UserStatus)}
'调用SQL语句查询
Sql = "select * from T_Card where Chr_CardID = @Chr_CardNo and Bit_UserStatus = @Chr_UserStatus"
'调用SqlHlper类中的ExecSelect()方法来执行查询,并获取返回值
Dim mylist As New List(Of Entity.ECard)
table = helper.ExecSelect(Sql, CommandType.Text, sqlParams)
Return table
End Function
传参数:(管理用户为例)—判断用户级别
Private Sub cmbUserLevel_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbUserLevel.SelectedIndexChanged
'根据用户级别加载信息
Dim userlevel As String
userlevel = cmbUserLevel.Text.Trim
Dim fac As New Facade.FManageUser
Dim mylist As List(Of Entity.ELogin)
mylist = fac.Show(userlevel)
If mylist.Count = 0 Then
MsgBox("没有该用户级别信息,请查询其他信息")
Else
dgvUserInfo.DataSource = mylist
End If
注意:userlevel定义的是字符串
同样的外观层、B层、D层传递的也是参数,
下面外观代码:
Public Function Show(ByVal level As String) As List(Of Entity.ELogin)
Dim bshow As New BLL.BManageUser
Dim flag As List(Of Entity.ELogin)
flag = bshow.Show(level)
Return flag
那么,在D层会如何运用呢?
在赋值的时候直接写这个参数就行了!
Public Function Show(level As String) As List(Of Entity.ELogin) Implements IDAL.IManagerUser.Show
Dim sql As String
Dim sqlparams As SqlParameter() = {New SqlParameter("@Chr_Level", level)}
Dim table As New DataTable
sql = "select * from T_User where Chr_Level=@Chr_Level "
table = helper.ExecSelect(sql, CommandType.Text, sqlparams)
Dim mylist As List(Of Entity.ELogin)
mylist = Convert.convertToList(Of Entity.ELogin)(table)
Return mylist
在重构的时候,自己对这个并不是很理解,所以总结出来,仅供参考,希望对你有帮助!
(二)、byval与Byref
在开始敲机房的时候,并没有多想,一直都用的是byval,自己也不清楚,之后通过查资料才发现:原来我们之前接触过:
我们先来回顾一下传值和传地址
传值:我们传递给函数是一个单纯的数值过去)按值传递参数时,传递的只是变量的 副本。如果过程改变了这个值,则所作变动只影响副本而不会影响变量本身
传地址:按地址传递参数时,过程用变量的内存地址去访问实际变量的内容。
为什么要回顾这个呢?今天我们讲到的Byval和Byref就和这个有关
Byval:按值传递
Byref:按地址传递
下面这篇文章介绍的很详细!
http://www.111cn.net/net/vb-net/58999.htm
(三)、sub与function
对于菜鸟的我,在开始个人重构的时候,一直用的是Function
sub:过程,不返回数值
function:函数,有返回数值
(四)、属性、方法的创建
Private _CardNo As String
Public Property CardNo() As String
Get
Return _CardNo
End Get
Set(value As String)
_CardNo = value
End Set
End Property
总结:
在路上的我,加油着!
标签:积累,重构,Dim,Text,Entity,机房,Trim,New,card From: https://blog.51cto.com/u_15586641/5763679