以下类用于ASP.NET应用程序从数据库结果集中读取货币并将其添加(例如,以美元显示总计加显示总计以GB磅为单位)。它的工作原理以下方式:继承一个类以删除一个属性并更改方法逻辑
- 读货币ID值
- 如果货币ID已经存在,增加了总该货币
- 如果货币ID不存在,将其添加到它的值列表
- 下一页
它的工作原理以及使用CurrencyID
财产为每一个独特的货币之间的区别。但是,现在已经很明显,默认情况下,IsoCurrencySymbol
对于每种货币也是唯一的,所以CurrencyID
实际上并不需要。
所以...我想知道是否可以从这个类继承并删除任何对CurrencyID
的引用,因此使CompareTo
方法使用IsoCurrencySymbol
来代替。
诀窍是将现有的类广泛使用,但引入一个不需要CurrencyID
的修改版本。这可能吗?
<Serializable()> _
Public Class CurrencyCounter
<Serializable()> _
Private Class CurrencyType
Implements IComparable
Public IsoCurrencySymbol As String
Public CurrencySymbol As String
Public CurrencyID As Int16
Public Amount As Decimal
Public Function CompareTo(obj As Object) As Integer Implements System.IComparable.CompareTo
If Not TypeOf (obj) Is CurrencyType Then
Throw New ArgumentException("Object is not a currency type")
Else
Dim c2 As CurrencyType = CType(obj, CurrencyType)
Return Me.CurrencyID.CompareTo(c2.CurrencyID)
End If
End Function
End Class
Private _Currencies As List(Of CurrencyType)
Public Sub New()
_Currencies = New List(Of CurrencyType)
End Sub
Private Sub AddStructToList(CurrencyID As Integer, IsoCurrencySymbol As String, CurrencySymbol As String, Amount As Decimal)
If IsoCurrencySymbol <> String.Empty AndAlso Amount > 0 Then
Dim s As New CurrencyType
s.CurrencyID = CurrencyID
s.IsoCurrencySymbol = IsoCurrencySymbol
s.CurrencySymbol = CurrencySymbol
s.Amount = Amount
_Currencies.Add(s)
End If
End Sub
Public Sub Add(CurrencyID As Integer, IsoCurrencySymbol As String, CurrencySymbol As String, Amount As Decimal)
Dim ct As CurrencyType = _Currencies.Find(Function(obj) obj.CurrencyID = CurrencyID)
If ct IsNot Nothing Then
ct.Amount += Amount
Else
AddStructToList(CurrencyID, IsoCurrencySymbol, CurrencySymbol, Amount)
End If
End Sub
Public Sub Clear()
_Currencies.Clear()
End Sub
Public Function Count() As Integer
Return _Currencies.Count
End Function
Public Function RenderTotals() As String
' ...
End Function
End Class
嗨史蒂文。这是一个非常好的答案。非常感谢您抽出时间将其分解到我的外行人员身上:-) – EvilDr 2013-02-21 16:30:30