Need Help C# - Expected catch or finally + cannot be applied to operands

2.8k views Asked by At
           MyProject.MyForms.m_FormBeingCreated.Add(typeof(T), null);
                try
                {
                    try
                    {
                        result = Activator.CreateInstance<T>();
                        return result;
                    }
                    object arg_96_0;
                    TargetInvocationException expr_9B = arg_96_0 as TargetInvocationException;
                    int arg_B8_0;
                    if (expr_9B == null)
                    {
                        arg_B8_0 = 0;
                    }
                    else
                    {
                        TargetInvocationException ex = expr_9B;
                        ProjectData.SetProjectError(expr_9B);
                        arg_B8_0 = (((ex.InnerException != null) > false) ? 1 : 0);
                    } 
                        endfilter(arg_B8_0);
                }
                finally
                {
                    MyProject.MyForms.m_FormBeingCreated.Remove(typeof(T));
                }
            }
            result = Instance;
            return result;
        }

/// What have i done wrong?

Keep getting errors about: Expected catch or finally @object arg_96_0;

Operator '>' cannot be applied to operands of type 'bool' and 'bool' @ex.InnerException !=null)

The name 'endfilter' does not exist in the current context. @endfilter(arg_B8_0);

1

There are 1 answers

3
Leon Newswanger On

Your first problem is with this line of code

arg_B8_0 = (((ex.InnerException != null) > false) ? 1 : 0);

In C# a boolean cannot be greater than or less than, so try changing that to

arg_B8_0 = (((ex.InnerException != null) != false) ? 1 : 0);

Also, as pointed out by Jeroen and others in comments, this piece of code is not very clean and is doing some evaluations it doesn't need to do.

arg_BB_0 = ex.InnerException != null ? 1 : 0;

This is a much better way to write the expression and achieve the same goal.

Your next issue is that all try statements must be accompanied by a catch or finally statement. Try reading this article on MSDN about proper use of try-catch blocks.

As far as getting an error that says endfilter doesn't exist in the current context, this means that you need to check the scope of where endfilter is declared. endfilter is probably not declared where it accessible in the current scope. MSDN can again be helpful here, This is a good place to get started understanding scope.