在Math.Pow中用VB进行奇怪的转换

时间:2012-07-30 18:35:42

标签: c# .net pow

此代码工作正常

Dim bc As Long = Math.Pow(36, ((IBase36.Length - 1) - i))

!在VB Math.Pow下返回DOUBLE数据类型。

当我将其转换为C#时,我有

long bc = Math.Pow(36, ((IBase36.Length - 1) - i));

我有一个强制语法问题。

如何解决?

4 个答案:

答案 0 :(得分:2)

long bc = (long)Math.Pow(36, ((IBase36.Length - 1) -i));

()是C#中的cast operator

答案 1 :(得分:2)

可能在VB中你有Option Strict 关闭或根本没有声明(默认关闭)

来自MSDN

Visual Basic allows conversions of many data types to other data types. 
Data loss can occur when the value of one data type is converted to a data type with 
less precision or smaller capacity. A run-time error occurs if such a narrowing 
conversion fails. Option Strict ensures compile-time notification of these narrowing 
conversions so they can be avoided.

所以我将更改VB代码

Option Strict On

Dim bc As Long = CType(Math.Pow(36, ((IBase36.Length - 1) - i)), Long)

答案 2 :(得分:1)

Math.Pow具有返回类型double,它在C#中无法隐式转换为long,因此必须通过类型转换显式完成。我不熟悉VB.NET,但转换规则可能不那么严格。

答案 3 :(得分:0)

在C#中你必须告诉编译器Math.Pow是long或double类型,如果你想要一个结果。

在控制台应用中查看此内容。

int value = 2;
string power = "6";
Console.WriteLine("" + (long)Math.Pow(value, (Convert.ToInt16(power) - 1)));
Console.ReadKey();