VB.Net 重构以删除重复代码
这是我的项目中的类的简化版本。由于奖金在每个函数中的计算方式完全相同,因此我想删除此处出现的明显重复代码,并将三个不同的函数合而为一。但是我不确定如何提供这个新函数所需的参数。
例如,我目前只是从代码
lblVolumeBonus.Content = TestClass.VolumeBonusAmountStore(bonus) 传递这样的参数。
我想我必须基本上扭转这个逻辑,并用类似
的东西来调用它lblVolumeBonus.Content = TestClass.VolumeBonusAmountStore(BonusTrackerBO.StoreBonus)
但我不确定语法是否正确或我是否处于正确的轨道上。
有人可以帮助我走向正确的方向吗?
Public Class TestClass
Public Shared Function StoreBonus(ByVal bonus As BonusTrackerBO.StoreBonus) As Double
Dim myBonus As Double = bonus.TicketAmount
Return myBonus * 5
End Function
Public Shared Function DistrictBonus(ByVal bonus As BonusTrackerBO.DistrictBonus) As Double
Dim myBonus As Double = bonus.TicketAmount
Return myBonus * 5
End Function
Public Shared Function CompanyBonus(ByVal bonus As BonusTrackerBO.CompanyBonus) As Double
Dim myBonus As Double = bonus.TicketAmount
Return myBonus * 5
End Function
End Class
编辑:这可能没有什么区别,但我应该明确指出等式中还有其他因素(我只是想保持简单)。那么,当奖金有其他回调(例如bonus.MaximumAmount、bonus.MinimumAmount)时,答案是否相同?奖励对象有 5 次回调,金额根据是商店、地区还是公司要求而有所不同。
This is a simplified version of a class that I have in my project. Since the Bonus is figured the exact same way in each function I want to remove the obvious code duplication that appears here and make the three different functions into one. However I am not sure how to provide the argument that this new function would require.
For instance I am currently just passing the argument like this from code
lblVolumeBonus.Content = TestClass.VolumeBonusAmountStore(bonus).
I think I have to basically turn this logic around and call it with something like
lblVolumeBonus.Content = TestClass.VolumeBonusAmountStore(BonusTrackerBO.StoreBonus)
but I am not sure of the correct syntax or whether I am on the right track at all.
Can someone help get me in the right direction?
Public Class TestClass
Public Shared Function StoreBonus(ByVal bonus As BonusTrackerBO.StoreBonus) As Double
Dim myBonus As Double = bonus.TicketAmount
Return myBonus * 5
End Function
Public Shared Function DistrictBonus(ByVal bonus As BonusTrackerBO.DistrictBonus) As Double
Dim myBonus As Double = bonus.TicketAmount
Return myBonus * 5
End Function
Public Shared Function CompanyBonus(ByVal bonus As BonusTrackerBO.CompanyBonus) As Double
Dim myBonus As Double = bonus.TicketAmount
Return myBonus * 5
End Function
End Class
Edit: It may not make a difference but I should have made clear that there are other factors in the equation (I was just trying to keep it simple). So is the answer the same when there are other callbacks to the bonus like bonus.MaximumAmount, bonus.MinimumAmount? There are 5 callbacks to the bonus object and the amounts are different depending on whether it is a Store, District or Company asking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不一个简单的
why not a simple
怎么样:
其中奖金是
bonus.TicketAmount
。或者如果它们都继承自像BaseBonusClass
这样的基类。编辑:或尝试泛型
How about:
Where bonus is
bonus.TicketAmount
. Or if They all inherit from a base class likeBaseBonusClass
.Edit: or try generics