vb.net类 [英] vb.net class

查看:76
本文介绍了vb.net类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我写了我的第一堂课并将其发布到这个新闻组希望

,我可以得到一些关于它的正确和错误的反馈。

欢迎所有评论,但更感兴趣的是什么会让它变得更好。

如果我完全错过了课程的用法,请告知我。


谢谢,


托马斯

''*************** ********************************** **************** *********************

''******************* ****************************** ******************** *****************

''****这个类接受一个参数。此参数作为

日期/时间****

''****数组传递。从这个变量中,Class计算并公开了三个

****

''****属性。 AvgTime - 阵列中每个

值之间的平均时间****

''****。 TimeSpan - 这是

第一个****

''****和数组中的最后日期/时间之间的时间长度。属性以分钟形式返回

和****

''****仅限第二个值。计数 - 这是可用日期/时间的数量

组****

''****在传递的数组中

****

''************************************ ************* *************************************

''**************************************** ********* *************************************


Public Class clsTime


Private _AvgTime As String

Private _TimeSpan As String

Private _Count As整数

私有datFirst为日期

私有datLast为日期

私有intFirst为整数= 0

私有intMinute As整数= 0

私有intSecond为整数= 0



Public Sub New (ByVal aryTime()As String)


_AvgTime = funAvgTime(aryTime)

_TimeSpan = funTimeSpan(aryTime)

_Count =第一页


结束子


#End Region


#Region" Properties"


Public Property AvgTime()As String

Get

返回_AvgTime

结束获取

设置(ByVal值为字符串)

_AvgTime = value

结束集

结束财产


公共财产TimeSpan()字符串

获取

返回_TimeSpan

结束获取

设置(ByVal值为字符串)

_TimeSpan = value

结束集

结束物业


公共财产数量()为整数

获取

返回_Count

结束获取

设置(ByVal值为整数)

_Count = value

结束集

结束财产


#End Region


#Region" Functions"


私函数funAvgTime(ByVal aryTime()As String)As String


Dim intAvTime As Integer = 0

intFirst = 0

intMinute = 0

intSecond = 0


Array.Sort(aryTime)


for x As Integer = 0 to aryTime.GetUpperBound(0)

If IsDate (aryTime(x))那么

intFirst + = 1

如果intFirst = 1那么

datFirst = CDate(aryTime(x))

结束如果

datLast = CDate(aryTime(x))


如果intFirst> 1然后

intAvTime + = DateDiff(DateInterval.Second,

CDate(aryTime(x - 1)),CDate(aryTime(x)))

intAvTime = System.Math.Abs​​(intAvTime)

结束如果

结束如果

下一页


如果是intFirst> 0然后

intAvTime = intAvTime / intFirst

如果intAvTime> 60然后

intMinute = intAvTime \ 60

intSecond = intAvTime Mod 60

返回CStr(intMinute)& ":" &安培; CStr(intSecond)

Else

返回CStr(intAvTime)& "秒'

结束如果

否则

返回String.Empty

结束如果


结束函数


私函数funTimeSpan(ByVal aryTimeSpan()As String)As String


Dim intLength As Integer = 0

intMinute = 0

intSecond = 0

如果IsDate(datLast)AndAlso IsDate(datFirst)那么

intLength = DateDiff(DateInterval.Second,datFirst,datLast)

如果intLength> 60然后

intMinute = intLength \ 60

intSecond = intLength Mod 60

返回CStr(intMinute)& ":" &安培; CStr(intSecond)

Else

返回CStr(intLength)& "秒'

结束如果

否则

返回String.Empty

结束如果

结束功能


#End Region


结束课


-

发布于NewsDemon.com - 高级未经审查的新闻组服务

------->>>>>> http://www.NewsDemon.com< ;<<<<< ------

无限制访问,匿名帐户,未经审查的宽带访问


I have written my first Class and am posting it to this newsgroup with hopes
that I can get some feedback on what is right and what is wrong with it.
All comments are welcome, but more interested in what would make it better.
If I have totally missed on what a class is used for please inform me.

Thanks,

Thomas
''************************************************* *************************************
''************************************************* *************************************
''**** This class accepts one parameter. This parameter is passed as a
Date/Time ****
''**** array. From this variable the Class calculates and exposes three
****
''**** properties. AvgTime - which is average time between each of the
values ****
''**** in the array. TimeSpan - which is the length of time between the
first ****
''**** and last date/time in the array. Properties are returned as Minute
and ****
''**** Second values only. Count - which is the number of usable Date/Time
groups ****
''**** in the passed array
****
''************************************************* *************************************
''************************************************* *************************************

Public Class clsTime

Private _AvgTime As String
Private _TimeSpan As String
Private _Count As Integer
Private datFirst As Date
Private datLast As Date
Private intFirst As Integer = 0
Private intMinute As Integer = 0
Private intSecond As Integer = 0

#Region "Constructors"

Public Sub New(ByVal aryTime() As String)

_AvgTime = funAvgTime(aryTime)
_TimeSpan = funTimeSpan(aryTime)
_Count = intFirst

End Sub

#End Region

#Region "Properties"

Public Property AvgTime() As String
Get
Return _AvgTime
End Get
Set(ByVal value As String)
_AvgTime = value
End Set
End Property

Public Property TimeSpan() As String
Get
Return _TimeSpan
End Get
Set(ByVal value As String)
_TimeSpan = value
End Set
End Property

Public Property Count() As Integer
Get
Return _Count
End Get
Set(ByVal value As Integer)
_Count = value
End Set
End Property

#End Region

#Region "Functions"

Private Function funAvgTime(ByVal aryTime() As String) As String

Dim intAvTime As Integer = 0
intFirst = 0
intMinute = 0
intSecond = 0

Array.Sort(aryTime)

For x As Integer = 0 To aryTime.GetUpperBound(0)
If IsDate(aryTime(x)) Then
intFirst += 1
If intFirst = 1 Then
datFirst = CDate(aryTime(x))
End If
datLast = CDate(aryTime(x))

If intFirst > 1 Then
intAvTime += DateDiff(DateInterval.Second,
CDate(aryTime(x - 1)), CDate(aryTime(x)))
intAvTime = System.Math.Abs(intAvTime)
End If
End If
Next

If intFirst > 0 Then
intAvTime = intAvTime / intFirst
If intAvTime > 60 Then
intMinute = intAvTime \ 60
intSecond = intAvTime Mod 60
Return CStr(intMinute) & ":" & CStr(intSecond)
Else
Return CStr(intAvTime) & " seconds"
End If
Else
Return String.Empty
End If

End Function

Private Function funTimeSpan(ByVal aryTimeSpan() As String) As String

Dim intLength As Integer = 0
intMinute = 0
intSecond = 0

If IsDate(datLast) AndAlso IsDate(datFirst) Then
intLength = DateDiff(DateInterval.Second, datFirst, datLast)
If intLength > 60 Then
intMinute = intLength \ 60
intSecond = intLength Mod 60
Return CStr(intMinute) & ":" & CStr(intSecond)
Else
Return CStr(intLength) & " seconds"
End If
Else
Return String.Empty
End If
End Function

#End Region

End Class

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

推荐答案

我会在使用它们的函数中声明以下变量:


私有datLast作为日期

私有intFirst为整数= 0

私有intMinute为整数= 0

私有intSecond为整数= 0


没有必要在班级宣布他们。


Venki

" th ***** @ msala.net"写道:
I would declare the following variables inside the functions that use them:

Private datFirst As Date
Private datLast As Date
Private intFirst As Integer = 0
Private intMinute As Integer = 0
Private intSecond As Integer = 0

There is no need for them to be declared at the Class level.

Venki
"th*****@msala.net" wrote:

我写了我的第一堂课,并将其发布到这个新闻组,希望我能得到一些关于什么是正确的,什么是正确的反馈错了。
欢迎所有的评论,但更感兴趣的是什么会让它变得更好。
如果我完全错过了课程的用途,请通知我。
谢谢,

托马斯

''**************************** ********************* ***************************** ********
''************************************ ************* *************************************
''****这个类接受一个参数。此参数作为日期/时间****
''****数组传递。从这个变量中,Class计算并公开了三个
****
''****属性。 AvgTime - 数组中每个
值之间的平均时间****
''****。 TimeSpan - 这是
第一个****
''****和数组中最后一个日期/时间之间的时间长度。属性以分钟形式返回
和****
''****仅限第二个值。计数 - 这是可用日期/时间组的数量****
'****在传递的数组中
****
''** *********************************************** *** ********************************** *
''********** *********************************** *********** ************************** *

Public Class clsTime

Private _AvgTime As String <私有_TimeSpan为字符串
私有_Count为整数
私有datFirst为日期
私有datLast为日期
私有intFirst为整数= 0
私有intMinute为整数= 0
Private intSecond As Integer = 0

#Region" Constructors"

Public Sub New(ByVal aryTime()As String)

_AvgTime = funAvgTime(aryTime)
_TimeSpan = funTimeSpan(aryTime)
_Count = intFirst

结束子

#End Region

#Region" Properties"

公共财产AvgTime()作为字符串
获取
返回_AvgTime
结束获取
设置(ByVal值为字符串)
_AvgTime = value
结束集
结束属性

公共属性TimeSpan()为字符串
获取
返回_TimeSpan
结束获取
设置(ByVal值为字符串)_TimeSpan = value
结束集
结束财产

公共财产统计()作为整数
获取
返回_Count
结束获取
设置(ByVal值为整数)
_Count = value
结束集
结束属性

#End Region
#Region" Functions"

私函数funAvgTime(ByVal aryTime()As String)As String

Dim intAvTime As Integer = 0
intFirst = 0
intMinute = 0
intSecond = 0

Array.Sort(aryTime)
对于x为Integer = 0到aryTime.GetUpperBound(0)
如果是IsDate(aryTime(x))然后
intFirst + = 1
如果intFirst = 1那么
datFirst = CDate(aryTime(x))
结束如果
datLast = CDate(aryTime(x))

如果是第一个> 1然后
intAvTime + = DateDiff(DateInterval.Second,
CDate(aryTime(x - 1)),CDate(aryTime(x)))
intAvTime = System.Math.Abs​​(intAvTime )
结束如果
结束如果

如果intFirst> 0然后
intAvTime = intAvTime / intFirst
如果intAvTime> 60然后
intMinute = intAvTime \ 60
intSecond = intAvTime Mod 60
返回CStr(intMinute)& ":" &安培; CStr(intSecond)
其他
返回CStr(intAvTime)& "秒
结束如果
否则
返回String.Empty
结束如果

结束功能

私人功能funTimeSpan(ByVal aryTimeSpan()As String)As String

Dim intLength As Integer = 0
intMinute = 0
intSecond = 0

如果是IsDate(datLast)AndAlso IsDate(datFirst)然后
intLength = DateDiff(DateInterval.Second,datFirst,datLast)
如果intLength> 60然后
intMinute = intLength \ 60
intSecond = intLength Mod 60
返回CStr(intMinute)& ":" &安培; CStr(intSecond)
其他
返回CStr(intLength)& "秒
结束如果
否则
返回String.Empty
结束如果
结束功能

#End Region

-
发表于NewsDemon.com - 高级未经审查的新闻组服务
------->>>>>> ; http://www.NewsDemon.com<<<<<<< ------
无限制访问,匿名帐户,未经审查的宽带访问

I have written my first Class and am posting it to this newsgroup with hopes
that I can get some feedback on what is right and what is wrong with it.
All comments are welcome, but more interested in what would make it better.
If I have totally missed on what a class is used for please inform me.

Thanks,

Thomas
''************************************************* *************************************
''************************************************* *************************************
''**** This class accepts one parameter. This parameter is passed as a
Date/Time ****
''**** array. From this variable the Class calculates and exposes three
****
''**** properties. AvgTime - which is average time between each of the
values ****
''**** in the array. TimeSpan - which is the length of time between the
first ****
''**** and last date/time in the array. Properties are returned as Minute
and ****
''**** Second values only. Count - which is the number of usable Date/Time
groups ****
''**** in the passed array
****
''************************************************* *************************************
''************************************************* *************************************

Public Class clsTime

Private _AvgTime As String
Private _TimeSpan As String
Private _Count As Integer
Private datFirst As Date
Private datLast As Date
Private intFirst As Integer = 0
Private intMinute As Integer = 0
Private intSecond As Integer = 0

#Region "Constructors"

Public Sub New(ByVal aryTime() As String)

_AvgTime = funAvgTime(aryTime)
_TimeSpan = funTimeSpan(aryTime)
_Count = intFirst

End Sub

#End Region

#Region "Properties"

Public Property AvgTime() As String
Get
Return _AvgTime
End Get
Set(ByVal value As String)
_AvgTime = value
End Set
End Property

Public Property TimeSpan() As String
Get
Return _TimeSpan
End Get
Set(ByVal value As String)
_TimeSpan = value
End Set
End Property

Public Property Count() As Integer
Get
Return _Count
End Get
Set(ByVal value As Integer)
_Count = value
End Set
End Property

#End Region

#Region "Functions"

Private Function funAvgTime(ByVal aryTime() As String) As String

Dim intAvTime As Integer = 0
intFirst = 0
intMinute = 0
intSecond = 0

Array.Sort(aryTime)

For x As Integer = 0 To aryTime.GetUpperBound(0)
If IsDate(aryTime(x)) Then
intFirst += 1
If intFirst = 1 Then
datFirst = CDate(aryTime(x))
End If
datLast = CDate(aryTime(x))

If intFirst > 1 Then
intAvTime += DateDiff(DateInterval.Second,
CDate(aryTime(x - 1)), CDate(aryTime(x)))
intAvTime = System.Math.Abs(intAvTime)
End If
End If
Next

If intFirst > 0 Then
intAvTime = intAvTime / intFirst
If intAvTime > 60 Then
intMinute = intAvTime \ 60
intSecond = intAvTime Mod 60
Return CStr(intMinute) & ":" & CStr(intSecond)
Else
Return CStr(intAvTime) & " seconds"
End If
Else
Return String.Empty
End If

End Function

Private Function funTimeSpan(ByVal aryTimeSpan() As String) As String

Dim intLength As Integer = 0
intMinute = 0
intSecond = 0

If IsDate(datLast) AndAlso IsDate(datFirst) Then
intLength = DateDiff(DateInterval.Second, datFirst, datLast)
If intLength > 60 Then
intMinute = intLength \ 60
intSecond = intLength Mod 60
Return CStr(intMinute) & ":" & CStr(intSecond)
Else
Return CStr(intLength) & " seconds"
End If
Else
Return String.Empty
End If
End Function

#End Region

End Class

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access



另外,要意识到你的所有函数都被声明为Private,你不能从类外面访问
。将它们声明为公共所以你可以调用

它们。


Venki


" th ***** @ msala.net"写道:
Also, realize that all your functions are declared as Private, you cannot
access them from outside the class. Declare them Public so you can invoke
them.

Venki

"th*****@msala.net" wrote:

我写了我的第一堂课,并将其发布到这个新闻组,希望我能得到一些关于什么是正确的,什么是正确的反馈错了。
欢迎所有的评论,但更感兴趣的是什么会让它变得更好。
如果我完全错过了课程的用途,请通知我。
谢谢,

托马斯

''**************************** ********************* ***************************** ********
''************************************ ************* *************************************
''****这个类接受一个参数。此参数作为日期/时间****
''****数组传递。从这个变量中,Class计算并公开了三个
****
''****属性。 AvgTime - 数组中每个
值之间的平均时间****
''****。 TimeSpan - 这是
第一个****
''****和数组中最后一个日期/时间之间的时间长度。属性以分钟形式返回
和****
''****仅限第二个值。计数 - 这是可用日期/时间组的数量****
'****在传递的数组中
****
''** *********************************************** *** ********************************** *
''********** *********************************** *********** ************************** *

Public Class clsTime

Private _AvgTime As String <私有_TimeSpan为字符串
私有_Count为整数
私有datFirst为日期
私有datLast为日期
私有intFirst为整数= 0
私有intMinute为整数= 0
Private intSecond As Integer = 0

#Region" Constructors"

Public Sub New(ByVal aryTime()As String)

_AvgTime = funAvgTime(aryTime)
_TimeSpan = funTimeSpan(aryTime)
_Count = intFirst

结束子

#End Region

#Region" Properties"

公共财产AvgTime()作为字符串
获取
返回_AvgTime
结束获取
设置(ByVal值为字符串)
_AvgTime = value
结束集
结束属性

公共属性TimeSpan()为字符串
获取
返回_TimeSpan
结束获取
设置(ByVal值为字符串)_TimeSpan = value
结束集
结束财产

公共财产统计()作为整数
获取
返回_Count
结束获取
设置(ByVal值为整数)
_Count = value
结束集
结束属性

#End Region
#Region" Functions"

私函数funAvgTime(ByVal aryTime()As String)As String

Dim intAvTime As Integer = 0
intFirst = 0
intMinute = 0
intSecond = 0

Array.Sort(aryTime)
对于x为Integer = 0到aryTime.GetUpperBound(0)
如果是IsDate(aryTime(x))然后
intFirst + = 1
如果intFirst = 1那么
datFirst = CDate(aryTime(x))
结束如果
datLast = CDate(aryTime(x))

如果是第一个> 1然后
intAvTime + = DateDiff(DateInterval.Second,
CDate(aryTime(x - 1)),CDate(aryTime(x)))
intAvTime = System.Math.Abs​​(intAvTime )
结束如果
结束如果

如果intFirst> 0然后
intAvTime = intAvTime / intFirst
如果intAvTime> 60然后
intMinute = intAvTime \ 60
intSecond = intAvTime Mod 60
返回CStr(intMinute)& ":" &安培; CStr(intSecond)
其他
返回CStr(intAvTime)& "秒
结束如果
否则
返回String.Empty
结束如果

结束功能

私人功能funTimeSpan(ByVal aryTimeSpan()As String)As String

Dim intLength As Integer = 0
intMinute = 0
intSecond = 0

如果是IsDate(datLast)AndAlso IsDate(datFirst)然后
intLength = DateDiff(DateInterval.Second,datFirst,datLast)
如果intLength> 60然后
intMinute = intLength \ 60
intSecond = intLength Mod 60
返回CStr(intMinute)& ":" &安培; CStr(intSecond)
其他
返回CStr(intLength)& "秒
结束如果
否则
返回String.Empty
结束如果
结束功能

#End Region

-
发表于NewsDemon.com - 高级未经审查的新闻组服务
------->>>>>> ; http://www.NewsDemon.com<<<<<<< ------
无限制访问,匿名帐户,未经审查的宽带访问

I have written my first Class and am posting it to this newsgroup with hopes
that I can get some feedback on what is right and what is wrong with it.
All comments are welcome, but more interested in what would make it better.
If I have totally missed on what a class is used for please inform me.

Thanks,

Thomas
''************************************************* *************************************
''************************************************* *************************************
''**** This class accepts one parameter. This parameter is passed as a
Date/Time ****
''**** array. From this variable the Class calculates and exposes three
****
''**** properties. AvgTime - which is average time between each of the
values ****
''**** in the array. TimeSpan - which is the length of time between the
first ****
''**** and last date/time in the array. Properties are returned as Minute
and ****
''**** Second values only. Count - which is the number of usable Date/Time
groups ****
''**** in the passed array
****
''************************************************* *************************************
''************************************************* *************************************

Public Class clsTime

Private _AvgTime As String
Private _TimeSpan As String
Private _Count As Integer
Private datFirst As Date
Private datLast As Date
Private intFirst As Integer = 0
Private intMinute As Integer = 0
Private intSecond As Integer = 0

#Region "Constructors"

Public Sub New(ByVal aryTime() As String)

_AvgTime = funAvgTime(aryTime)
_TimeSpan = funTimeSpan(aryTime)
_Count = intFirst

End Sub

#End Region

#Region "Properties"

Public Property AvgTime() As String
Get
Return _AvgTime
End Get
Set(ByVal value As String)
_AvgTime = value
End Set
End Property

Public Property TimeSpan() As String
Get
Return _TimeSpan
End Get
Set(ByVal value As String)
_TimeSpan = value
End Set
End Property

Public Property Count() As Integer
Get
Return _Count
End Get
Set(ByVal value As Integer)
_Count = value
End Set
End Property

#End Region

#Region "Functions"

Private Function funAvgTime(ByVal aryTime() As String) As String

Dim intAvTime As Integer = 0
intFirst = 0
intMinute = 0
intSecond = 0

Array.Sort(aryTime)

For x As Integer = 0 To aryTime.GetUpperBound(0)
If IsDate(aryTime(x)) Then
intFirst += 1
If intFirst = 1 Then
datFirst = CDate(aryTime(x))
End If
datLast = CDate(aryTime(x))

If intFirst > 1 Then
intAvTime += DateDiff(DateInterval.Second,
CDate(aryTime(x - 1)), CDate(aryTime(x)))
intAvTime = System.Math.Abs(intAvTime)
End If
End If
Next

If intFirst > 0 Then
intAvTime = intAvTime / intFirst
If intAvTime > 60 Then
intMinute = intAvTime \ 60
intSecond = intAvTime Mod 60
Return CStr(intMinute) & ":" & CStr(intSecond)
Else
Return CStr(intAvTime) & " seconds"
End If
Else
Return String.Empty
End If

End Function

Private Function funTimeSpan(ByVal aryTimeSpan() As String) As String

Dim intLength As Integer = 0
intMinute = 0
intSecond = 0

If IsDate(datLast) AndAlso IsDate(datFirst) Then
intLength = DateDiff(DateInterval.Second, datFirst, datLast)
If intLength > 60 Then
intMinute = intLength \ 60
intSecond = intLength Mod 60
Return CStr(intMinute) & ":" & CStr(intSecond)
Else
Return CStr(intLength) & " seconds"
End If
Else
Return String.Empty
End If
End Function

#End Region

End Class

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access





1)命名(次要)

在命名clsTime上可疑。

- 更新的命名标准已经离开了匈牙利语

表示法。几乎所有的东西都是现在的班级

2)Ctors

- 如果班级打算按日期/时间值工作,那么通过

他们这样而不是字符串。

- 如果我们坚持使用值作为字符串那么我们就是它

如果我们可以避免这种情况会更好


- 如果可能的话,arraylist更容易使用。这将不会影响这个类,但是当生成输入时,arraylist将会更容易。


- 计算在需要时,而不是在

施工时生成它们。您可能并不总是同时使用

- 您需要存储对传入

日期/时间的引用(如果您希望更改平均值/跨度更新)任何

原始值)

或者,如果你想要快照视图,请复制值。


2)

私有_Count为整数

私有datFirst为日期

私有datLast为日期

私有intFirst为整数= 0

私有intMinute为整数= 0

私有intSecond为整数= 0


不需要存在于班级

3)Properies


计数可以从时间列表生成,不需要存储

它应该是只读的

AvgTime和TimeSpan都应该返回TimeSpan


它们都应该是只读的

如果允许输入被修改那么你会想要

每次重新计算它们(没有变量存储它们,

私有_AvgTime作为字符串

私有_TimeSpan作为字符串

都消失了)或者你只在他们第一次使用
时计算它们,将它们存储在课程和每个后续时间,返回

储值。

4)功能问题?


有什么预先要求收到的数据?

它们都保证是有效时间吗?

如果不是,我们如何处理无效时间?

如果是,你为什么要测试IsDate()?

它们是否保证按时间顺序排列?

如果没有,那么funTimeSpan()将不会给出正确的值 - 你需要找到第一次和最后一次然后找到差异

如果是,那么我们为什么要使用System.Math.Abs​​(),t + 1 - t将

总是非负的

5)在类中添加一个共享函数,该函数将从

TimeSpan创建一个字符串为您做正确的格式化,或者您可以输入

AsString()版本属性。

通过仅提供输出的String()版本,您可以限制

如何/在何处使用它。任何想要使用这些值的类都需要

才能将字符串解析回一个时间跨度,因此它需要知道你以什么格式存储它的
。这可能会改变。


a)dim tm as new clsTime(次)

MessageBox.Show(clsTime.FormatTimespan(tm.AvgTime()))


b)dim tm as new clsTime(次)

MessageBox.Show(tm.AvgTimeAsString())


dim calc as OtherClass

calc.AvgTime = tm.AvgTime

6)格式代码在两个函数中都是重复的 - 应该只是

一份把它放在一个私人的功能中


hth,

Alan。


1) Naming (minor)
Dubious on the naming clsTime.
- The newer naming standards have moved away from the Hungarian
notation. Pretty much everything is a class now
2) Ctors
- If the class is intended to work upon date/time values then pass
them in as such rather than strings.
- if we are stuck with the values being strings then we are it
would be better if we can avoid this

- If possible, an arraylist is simpler for working with. It will
not impact this class but when generating the inputs an arraylist will
probably make things easier.

- Calculate the properties when needed instead of generating them at
time of construction. You may not always use both
- you will need to either store a reference to the incoming
date/times (if you want the average/span to update if you change any of
the original values)
or, copy the values if you want a snapshot view.

2)
Private _Count As Integer
Private datFirst As Date
Private datLast As Date
Private intFirst As Integer = 0
Private intMinute As Integer = 0
Private intSecond As Integer = 0

do not need to exist at the class level
3) Properies

Count can be generated from the time list, not need to store
It should be read only
AvgTime and TimeSpan should both return TimeSpan

They both should be read-only
If allowing the inputs to be modified then you will want to
recalc them every time (no variables to store them,
Private _AvgTime As String
Private _TimeSpan As String
both disappear) or else you calc them only the first time they
are used, store them in the class and each subsequent time, return the
stored value.
4) Functionality Questions?

What are the pre-reqs for the incoming data?
Are they all guaranteed to be valid times?
if no, what do we do with the invalid times?
if yes, why are you testing IsDate()?
Are they guaranteed to be in time sequence?
if no, then funTimeSpan() will not give the correct value - you
need to find the first and last time and then find the difference
if yes, then why are we using System.Math.Abs(), t+1 - t will
always be non-negative
5) Add a shared function to the class that will create a string from a
TimeSpan that will do the correct formatting for you or you can put in
AsString() versions of the properties.
By providing only String() versions of the output you limit
how/where it can be used. Any class that wants to use the values needs
to be able to parse the string back to a timespan so it needs to know
in what format you have stored it. Which may change.

a) dim tm as new clsTime(times)
MessageBox.Show(clsTime.FormatTimespan(tm.AvgTime( )))

b) dim tm as new clsTime(times)
MessageBox.Show(tm.AvgTimeAsString())

dim calc as OtherClass
calc.AvgTime = tm.AvgTime
6) The formatting code is duplicated in both functions - should only be
one copy of it, place it in a private function

hth,
Alan.


这篇关于vb.net类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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