array indexing problem

2 posts, 0 answers
  1. mdanh2002
    mdanh2002 avatar
    80 posts
    Member since:
    May 2007

    Posted 12 Jun 2007 Link to this post

    Hi

    The following code in C#

        protected string Testing(string st)  
        {  
            string st2 = "";  
            for (int i = st.Length - 1; i >= 0; i--)  
                st2 = st2 + st[i];  
            return st2;  
        } 

    gets converted to VB in such a strange way:

    Protected Function Testing(ByVal st As StringAs String 
        Dim st2 As String = "" 
        Dim i As Integer = st.Length - 1  
        While i >= 0  
            st2 = st2 + st(i)  
            System.Math.Max(System.Threading.Interlocked.Decrement(i),i + 1)  
        End While 
        Return st2  
    End Function 

    Try to convert the converted code back to C#:

    protected string Testing(string st)  
    {  
        string st2 = "";  
        int i = st.Length - 1;  
        while (i >= 0) {  
            st2 = st2 + st(i);  
            System.Math.Max(System.Threading.Interlocked.Decrement(i), i + 1);  
        }  
        return st2;  
    }  
     

    Some questions:

    (1) Why must the for loop be converted to while? VB also has for loop! The logic is still there but the question is, why must the converter use while?
    (2) The increment for i, why can't it be simpler like i = i+1? Why must it use System.Math.Max(System.Threading.Interlocked.Decrement(i),i + 1), which looks mysterious? And this mysterious code remains the same when the converted code is converted back to VB!
    (3) The converter is smart enough to change the array index from [ ] in C# to ( ) in VB, but is unable to change it back, which would of course cause syntax error when compiling the converted code.

    Can any of this be improved?
  2. Todd Anglin
    Todd Anglin avatar
    2040 posts
    Member since:
    Aug 2005

    Posted 19 Jun 2007 Link to this post

    Thanks for the feedback!

    There are indeed a few quirks in the NRefactory conversion engine used to power this converter. I'd encourage you to share your findings with the NRefactory team at:

    http://community.sharpdevelop.net/forums/default.aspx

    That's the best way to get the right people working on a solution.

    Thanks~
    Todd
Back to Top