[英]PV Function and Porting VB6 to C#
I'm working on porting some classic VB6 code to C# and just stumbled across a usage of the PV
function.我正在将一些经典的 VB6 代码移植到 C#,并且偶然发现了PV
函数的用法。
I feels wrong including a reference to the Microsoft.VisualBasic Assembly.我觉得错误包括对 Microsoft.VisualBasic 程序集的引用。 Is this something that is commonly done, or should I explore further options.这是经常做的事情,还是我应该探索更多的选择。 The next idea that pops into my mind is exploring this PV function in Reflector.我想到的下一个想法是在 Reflector 中探索这个 PV 功能。
The use of Microsoft.VisualBasic from C# and VB.NET has been discussed thoroughly under this question .在这个问题下已经彻底讨论了 C# 和 VB.NET 中Microsoft.VisualBasic的使用。 The Microsoft.VisualBasic namespace is fully supported , and will be around as long as .Net is around.完全支持Microsoft.VisualBasic 命名空间,只要 .Net 存在,它就会存在。 There's no reason to avoid it.没有理由避免它。
EDIT: It's telling that at the time of typing, the other answers for this question are an incorrect reimplementation of the function, and a one-man-band unsupported library from Code Galleries.编辑:这说明在打字时,这个问题的其他答案是函数的不正确重新实现,以及来自 Code Galleries 的单人乐队不受支持的库。 Come on guys, it would take a real major event for Microsoft to drop the financial functions from VB.来吧伙计们,微软需要一个真正的大事件才能从 VB 中删除财务功能。
It's a different story for Microsoft.VisualBasic.Compatibility , which is exclusively for use by the VB6 upgrade wizard, EDIT has now been marked obsolete in .Net 4 (my prediction came true), and should not be used for new development. Microsoft.VisualBasic.Compatibility则是另一回事,它专供 VB6 升级向导使用, EDIT现在已在 .Net 4 中被标记为过时(我的预测成真),不应用于新的开发。 There would be some advantages in removing references to this, but personally I'd probably try to achieve a fully working port first referencing.Net 3.5.删除对此的引用会有一些好处,但我个人可能会尝试首先引用.Net 3.5 来实现一个完全工作的端口。
Pretty straight forward to replicate in C#在 C# 中复制非常简单
public static double PV(double Rate, int nPer, double Pmt, double FV, bool Type)
{
double ann = Math.Pow(1 + Rate, nPer);
return -(FV + Pmt * (1 + (Type ? Rate : 0)) * ((ann - 1) / Rate)) / ann;
}
Just a re-arrangement of the formula Microsoft provides .只是重新安排 Microsoft提供的公式。
I tried using the accepted answer, but I couldn't get it to work in .NET Core.我尝试使用已接受的答案,但无法在 .NET Core 中使用它。 I did some looking around and ran across this code on github that appears to be Microsoft's implementation of PV:我环顾四周,在 github 上发现了这段代码,它似乎是微软对 PV 的实现:
https://github.com/microsoft/referencesource/blob/master/Microsoft.VisualBasic/runtime/msvbalib/Financial.vb#L662 https://github.com/microsoft/referencesource/blob/master/Microsoft.VisualBasic/runtime/msvbalib/Financial.vb#L662
Public Function PV(ByVal Rate As Double, ByVal NPer As Double, ByVal Pmt As Double, Optional ByVal FV As Double = 0, Optional ByVal Due As DueDate = DueDate.EndOfPeriod) As Double
Dim dTemp As Double
Dim dTemp2 As Double
Dim dTemp3 As Double
If Rate = 0.0# Then
Return (-FV - Pmt * NPer)
Else
If Due <> 0 Then
dTemp = 1.0# + Rate
Else
dTemp = 1.0#
End If
dTemp3 = 1.0# + Rate
' WARSI Using the exponent operator for pow(..) in C code of PV. Still got
' to make sure that they (pow and ^) are same for all conditions
dTemp2 = dTemp3 ^ NPer
'Do divides before multiplies to avoid OverFlowExceptions
Return (-(FV + Pmt * dTemp * ((dTemp2 - 1.0#) / Rate)) / dTemp2)
End If
End Function
You'd still need to transpile it to C#.您仍然需要将其转换为 C#。 If I have a general enough implementation at the end of this I'll post that too.如果我在本文末尾有一个足够通用的实现,我也会发布它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.