VB 代码到 C# - 命名空间 Microsoft.VisualBasic



我正在将VB代码转换为C#。以下是代码。

CrByMonthCr = Val(DateDiff("m", StartDate, DateSerial(Year(EndDate), Month(EndDate), 1)) + 1) * MonthCr

在 C# 代码中,我导入了命名空间 -using Microsoft.VisualBasic。 我只设法解决了DateDiff功能。ValDateSerial都给出了编译器错误。

CrByMonthCr = Val(DateAndTime.DateDiff("m", StartDate, DateSerial(Year(EndDate), Month(EndDate), 1)) + 1) * MonthCr

有谁知道为什么尽管导入了 VisualBasic 的命名空间,但它还是会出错?如何解决这个问题?

当前上下文中不存在日期序列

DateSerial

作为静态方法存在于Microsoft.VisualBasic.DateAndTime类中,因此要继续使用它,您需要DateAndTime.DateSerial(Year(EndDate), Month(EndDate), 1)
但是您可以简单地将其替换为new DateTime(EndDate.Year, EndDate.Month, 1)

正如Zohar Peled所提到的,DateSerialDateAndTime的成员。ValConversion的成员。所以完整的代码(带using Microsoft.VisualBasic;(是

CrByMonthCr = Conversion.Val(DateAndTime.DateDiff("m", StartDate, DateAndTime.DateSerial(DateAndTime.Year(EndDate), DateAndTime.Month(EndDate), 1)) + 1) * MonthCr;

相关内容

最新更新