423,335 Members | 1,149 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,335 IT Pros & Developers. It's quick & easy.

# Problems with Converting VB.NET Code to C#.NET

 P: 5 Hi experts, I'm Currently working on a project converting VB codes to C#. Now I'm facing a problem and not understand the VB Code to convert below codes into C# synthax. Please help to explain the below codes and provide me some hint on how to convert it. Expand|Select|Wrap|Line Numbers Private Function Power2(ByVal exponent As Long) As Long         Static result(0 To 31) As Long, i As Integer          ' Evaluate all powers of 2 only once.         If result(0) = 0 Then             result(0) = 1             For i = 1 To 30                 result(i) = result(i - 1) * 2             Next             result(31) = &H80000000        ' This is a special value.         End If         Power2 = result(exponent) End Function   Feb 26 '18 #1
 Expert Mod 5K+ P: 9,731 Notes: you cannot have a static variable declared inside a method in C# so the static definition of the array of `long`s was moved outside of the method &H80000000 is a hex number in VB.NET but in C# the hex notation is 0x80000000 this `Power2 = result(exponent)` is an old school vb way of returning a value...therefore the `return` keyword was used instead Expand|Select|Wrap|Line Numbers static long[] result = new long[32]; private long Power2(long exponent){         // Evaluate all powers of 2 only once.         if(result[0] == 0){             result[0] = 1;             for(int i = 1;i < 30; i++){                 result[i] = result[i - 1] * 2;             }             result[31] = 0x80000000;       // This is a special value. (assuming its hex)         }//end if         return result[exponent]; }//end of function   I would recommend doing some validation before accessing the array index at the exponent... you will get an exception if you pass something larger than 31 or less than 0 as the exponent parameter... Feb 28 '18 #2