VB.Net 重构以删除重复代码

发布于 2024-10-15 22:45:13 字数 1167 浏览 3 评论 0原文

这是我的项目中的类的简化版本。由于奖金在每个函数中的计算方式完全相同,因此我想删除此处出现的明显重复代码,并将三个不同的函数合而为一。但是我不确定如何提供这个新函数所需的参数。

例如,我目前只是从代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷荒 2024-10-22 22:45:13

为什么不一个简单的

Public Shared Function GiveBonus(ByVal TicketAmount As Double) As Double
    Return TicketAmount  * 5
End Function



lblVolumeBonus.Content = TestClass.VolumeBonusAmountStore(bonus.TicketAmount)

why not a simple

Public Shared Function GiveBonus(ByVal TicketAmount As Double) As Double
    Return TicketAmount  * 5
End Function



lblVolumeBonus.Content = TestClass.VolumeBonusAmountStore(bonus.TicketAmount)
香草可樂 2024-10-22 22:45:13

怎么样:

Public Shared Function GetBonus(ByVal bonus as Double) As Double
    Return bonus * 5
End Function

其中奖金是 bonus.TicketAmount。或者如果它们都继承自像BaseBonusClass这样的基类。

Public Shared Function GetBonus(ByVal bonus as BaseBonusClass) As Double
    Return bonus.TicketAmount * 5
End Function

编辑:或尝试泛型

Public Shared Function GetBonus(Of T As BaseBonusClass)(ByVal bonus as T) As Double
    Return bonus.TicketAmount * 5
End Function

How about:

Public Shared Function GetBonus(ByVal bonus as Double) As Double
    Return bonus * 5
End Function

Where bonus is bonus.TicketAmount. Or if They all inherit from a base class like BaseBonusClass.

Public Shared Function GetBonus(ByVal bonus as BaseBonusClass) As Double
    Return bonus.TicketAmount * 5
End Function

Edit: or try generics

Public Shared Function GetBonus(Of T As BaseBonusClass)(ByVal bonus as T) As Double
    Return bonus.TicketAmount * 5
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文