This is a migrated thread and some comments may be shown as answers.

array indexing problem

1 Answer 79 Views
Code Converter
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
mdanh2002
Top achievements
Rank 1
mdanh2002 asked on 12 Jun 2007, 09:34 AM
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?

1 Answer, 1 is accepted

Sort by
0
Todd Anglin
Top achievements
Rank 2
answered on 19 Jun 2007, 09:42 PM
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
Tags
Code Converter
Asked by
mdanh2002
Top achievements
Rank 1
Answers by
Todd Anglin
Top achievements
Rank 2
Share this question
or