このコードは正常に動作しています
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));
そして、キャスト構文の問題があります。
どうすれば解決できますか?
long bc = (long)Math.Pow(36, ((IBase36.Length - 1) -i));
() はC#のキャスト演算子です。
おそらくVBでは、Option Strict Offがあるか、まったく宣言されていません(デフォルトオフ)
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)
Math.Pow
には戻り値の型double
があり、これは C# では暗黙的に に変換できないlong
ため、型キャストを介して明示的に行う必要があります。私は VB.NET に精通していませんが、変換規則はそれほど厳密ではないかもしれません。
C# では、結果が必要な場合、コンパイラに Math.Pow が long または double 型であることを伝える必要があります。
コンソール アプリで確認してください。
int value = 2;
string power = "6";
Console.WriteLine("" + (long)Math.Pow(value, (Convert.ToInt16(power) - 1)));
Console.ReadKey();