Share via


This expression causes side effects and Will Not Be Evaluated

Question

Thursday, May 20, 2010 7:48 PM

Hi,
   
  I have the routine below and it works correctly, but the mew WebForm has a TabControl and when I "Click" on  TabItem routine below error ?!?!?
  
  Would anyone help me enteender because it's goin '?
  
  Routine:
  
   public static void Clear(string [] Names, Grid LayoutRoot)
        (
            foreach (string field in Names)
            (
                if (LayoutRoot.FindName (field). GetType () == typeof (TextBox))
                (
                    ((TextBox) (LayoutRoot.FindName (field))). Text = String.Empty;
                )
                else if (LayoutRoot.FindName (field). GetType () == typeof (AutoCompleteBox))
                (
                    ((AutoCompleteBox) (LayoutRoot.FindName (field))). Text = String.Empty;
                )
                else if (LayoutRoot.FindName (field). GetType () == typeof (CheckBox))
                (
                    ((CheckBox) (LayoutRoot.FindName (field))). IsChecked = false;
                )
            )
        )
  Error:
   Object reference not set to an instance of an Object.
   
  Errors that appear in Watch4
   LayoutRoot.FindName (field) This expression causes side effects and Will Not Be Evaluated
   LayoutRoot.FindName (field). GetType () This expression causes side effects and Will Not Be EvaluatedRogerio

All replies (6)

Thursday, May 20, 2010 8:07 PM ✅Answered

Hi,

Try following instead:

    public static void Clear(string[] Names, Grid LayoutRoot)
    {
      foreach (string field in Names)
      {
        if (null == LayoutRoot.FindName(field))
        {
          MessageBox.Show(string.Format("{0} not found!", field));
          continue;
        }
        if (LayoutRoot.FindName(field).GetType() == typeof(TextBox))
        {
          ((TextBox)(LayoutRoot.FindName(field))).Text = String.Empty;
        }
        else if (LayoutRoot.FindName(field).GetType() == typeof(AutoCompleteBox))
        {
          ((AutoCompleteBox)(LayoutRoot.FindName(field))).Text = String.Empty;
        }
        else if (LayoutRoot.FindName(field).GetType() == typeof(CheckBox))
        {
          ((CheckBox)(LayoutRoot.FindName(field))).IsChecked = false;
        }
      }
    }

Regards,

Yasser.

WHAT'S NEW IN THE .NET FRAMEWORK 4:
Article: Comparison of parallel and sequential computing in .NET Framework


Thursday, May 20, 2010 9:08 PM ✅Answered

Thank you.

So, you will get better and quicker answers at http://social.msdn.microsoft.com/Forums/en/wpf/threads/ (Windows Presentation Foundation (WPF)) from related experts there.

WHAT'S NEW IN THE .NET FRAMEWORK 4:
Article: Comparison of parallel and sequential computing in .NET Framework


Thursday, May 20, 2010 8:42 PM

Hi Yasser,

This is the problem "LayoutRoot.FindName(field)) return NULL, after the click on TabItem.

Can you tell me why this happens? and how to solve?

Tks

Rogério

Rogerio


Thursday, May 20, 2010 8:55 PM

Sure,

What is the LayoutRoot data type?

WHAT'S NEW IN THE .NET FRAMEWORK 4:
Article: Comparison of parallel and sequential computing in .NET Framework


Thursday, May 20, 2010 9:04 PM

...is the type of FrameworkElement

 

Rogerio


Thursday, May 20, 2010 9:14 PM

Yasser,

 thanks for the support.

Rogerio