Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VB -> C#: Exit Try is converted to break - this causes problems when a loop is inside a try-catch block #779

Closed
philipkaare opened this issue Oct 22, 2021 · 4 comments
Labels
compilation error A bug where the converted output won't compile VB -> C# Specific to VB -> C# conversion

Comments

@philipkaare
Copy link

philipkaare commented Oct 22, 2021

VB.Net input code

        Try
            If Not To_Show_Cost() Then
                Close()
            End If

            Button_Exit.Visible = False
            The_Old_Index = The_Cost_Center
            SQLUtil.ReadyComboBox(ComboBox_CostCenter, " Index_ID,Cost_Center_Name", " Cost_Center ", " WHERE Hidden_Center ='False' ORDER BY Cost_Center_Name ", "Please Choose")

            If The_Cost_Center = 0 Then
                ComboBox_CostCenter.Enabled = True
                Button_Change_Cost.Visible = False
                Button_NoCost.Visible = True
                Is_Loaded = True
                ComboBox_CostCenter.DroppedDown = True
                Exit Try
            End If

            ComboBox_CostCenter.SelectedIndex = -1
            For i = 0 To ComboBox_CostCenter.Items.Count - 1
                If ComboBox_CostCenter.Items(i).item(0) = The_Cost_Center Then
                    ComboBox_CostCenter.SelectedIndex = i
                    ComboBox_CostCenter.Enabled = False
                    Call Do_Cost_Item()
                    Is_Loaded = True
                    Exit Try
                End If
            Next

Erroneous output

   try
            {
                if (!To_Show_Cost())
                {
                    Close();
                }

                Button_Exit.Visible = false;
                The_Old_Index = The_Cost_Center;
                SQLUtil.ReadyComboBox(ComboBox_CostCenter, " Index_ID,Cost_Center_Name", " Cost_Center ",
                    " WHERE Hidden_Center ='False' ORDER BY Cost_Center_Name ", "Please Choose");
                if (The_Cost_Center == 0)
                {
                    ComboBox_CostCenter.Enabled = true;
                    Button_Change_Cost.Visible = false;
                    Button_NoCost.Visible = true;
                    Is_Loaded = true;
                    ComboBox_CostCenter.DroppedDown = true;
                    break;
                }

                ComboBox_CostCenter.SelectedIndex = -1;
                for (int i = 0, loopTo = ComboBox_CostCenter.Items.Count - 1; i <= loopTo; i++)
                {
                    if (Conversions.ToBoolean(
                        Operators.ConditionalCompareObjectEqual(((DataRowView)ComboBox_CostCenter.Items[i]).Row.ItemArray[0],
                            The_Cost_Center, false)))
                    {
                        ComboBox_CostCenter.SelectedIndex = i;
                        ComboBox_CostCenter.Enabled = false;
                        Do_Cost_Item();
                        Is_Loaded = true;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Issue ..." + Constants.vbCrLf + ex.Message, "Load");
            }
         

Expected output

   try
            {
                if (!To_Show_Cost())
                {
                    Close();
                }

                Button_Exit.Visible = false;
                The_Old_Index = The_Cost_Center;
                SQLUtil.ReadyComboBox(ComboBox_CostCenter, " Index_ID,Cost_Center_Name", " Cost_Center ",
                    " WHERE Hidden_Center ='False' ORDER BY Cost_Center_Name ", "Please Choose");
                if (The_Cost_Center == 0)
                {
                    ComboBox_CostCenter.Enabled = true;
                    Button_Change_Cost.Visible = false;
                    Button_NoCost.Visible = true;
                    Is_Loaded = true;
                    ComboBox_CostCenter.DroppedDown = true;
                    #error cannot convert exit try as this does not have an equivalent in C#
                }

                ComboBox_CostCenter.SelectedIndex = -1;
                for (int i = 0, loopTo = ComboBox_CostCenter.Items.Count - 1; i <= loopTo; i++)
                {
                    if (Conversions.ToBoolean(
                        Operators.ConditionalCompareObjectEqual(((DataRowView)ComboBox_CostCenter.Items[i]).Row.ItemArray[0],
                            The_Cost_Center, false)))
                    {
                        ComboBox_CostCenter.SelectedIndex = i;
                        ComboBox_CostCenter.Enabled = false;
                        Do_Cost_Item();
                        Is_Loaded = true;
                        #error cannot convert exit try as this does not have an equivalent in C#
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Issue ..." + Constants.vbCrLf + ex.Message, "Load");
            }
         

Details

  • Product in use: VS extension
  • Version in use: 8.4.1.0

This is mainly a problem when inside a loop since the break, instead of causing a compile error, causes the code to behave differently. I may be able to contribute a fix, but I have no experience with the code base. Also, thanks for a wonderful program!

@philipkaare philipkaare added the VB -> C# Specific to VB -> C# conversion label Oct 22, 2021
@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented Oct 22, 2021

Thanks for the report. It's closely related to #690. I like the suggestion of just outputting an error initially so at least it's obvious. I'll try to post some hints on where to make this change when I have chance to look, but it's probably a method called something like VisitExitStatemement

@philipkaare philipkaare changed the title VB -> C#: _Add a short description_ VB -> C#: Exit Try is converted to break - this causes problems when a loop is inside a try-catch block Oct 23, 2021
@philipkaare
Copy link
Author

Yes, it is possible to automatically convert it, but it gets rather complicated and the resulting code is going to look a bit different from the source. I'll try to take a look when I have time at the VisitExitStatement.

@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented Oct 26, 2021

In terms of a logically identical conversion, one option is a bunch of booleans and if statements (annoying for very nested code). Other options (I might edit these slightly if I come back to them and notice further issues):

Break from loop

do
{
    try
    {
       //...
       break;
       //...
    }
    catch
    {
       //...
    }
} while (false)

This would need a special case of #690 (treat the try block as an enclosing breakable block) if the "try" contains other breakable blocks (that are broken from)

Return from local function

try
{
    void InnerTry()
    {
       //...
       return;
       //...
    }
    InnerTry();
} catch
{
    //...
}

This would need a special case of #690 (pull if (exit_something) break; statement below the local function call) if it's for a breakable block (that's broken from) surrounding the try block. If the try block already contains a return it'd have issues and need a return type on the local function, which starts to get messier

@GrahamTheCoder GrahamTheCoder added output logic error A bug where the converted output behaves differently to the input code compilation error A bug where the converted output won't compile and removed output logic error A bug where the converted output behaves differently to the input code labels Dec 30, 2021
@GrahamTheCoder
Copy link
Member

In df6354b I turned this into a compilation error so that it at least can't surprise anyone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compilation error A bug where the converted output won't compile VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

2 participants