2019-07-12
How to implement Interface in Mainwindow
stackoverflow
Question

I'm rewriting my project, so it's easier to edit in future, and want to implement interface.

I've implemented the interface, but it doesn't work in MainWindow, I'm not able to call the method.

So I've tried to use the PalindromeChecker as the default implementation PalindromeChecker = new PalindromeChecker(); , so I can call the method but it didn't work.

interface ICheckPalindrome
    {
        bool IsPalindrome(string text);
    }
public class PalindromeChecker : ICheckPalindrome
    {

        /// <summary>
        /// Method for checking if the word/text is a palindrome.
        /// </summary>
        public bool IsPalindrome(string text)
        {
            ......
            //Code
            }
        }
    }
  namespace TextChecker
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            lblInput.Foreground = Brushes.ForestGreen;
            lblResult.Foreground = Brushes.ForestGreen;
            lblTitel.Foreground = Brushes.ForestGreen;

        }

        /// <summary>
        /// User input and checking the input if the word a palindrome is.
        /// </summary>
        private void InputText_TextChanged(object sender, TextChangedEventArgs e)
        {
            string text = InputText.Text;

            bool isPalindrome = PalindromeChecker.IsPalindrome(text);

            OutputText.Text = text + (isPalindrome ? " is a palindrome" : " is NOT a palindrome");

            if (InputText.Text == string.Empty)
            {
                OutputText.Clear();
            }
        }
        private void ButtonClicked(object sender, RoutedEventArgs e)
        {
            SubWindow subWindow = new SubWindow();
            subWindow.Show();
        }
    }
}
 public class PalindromeChecker : ICheckPalindrome
    {

        /// <summary>
        /// Method for checking if the word/text is a palindrome.
        /// </summary>
        public bool IsPalindrome(string text)
        {
            int min = 0;
            int max = text.Length - 1;

            while (true)
            {
                if (min > max)
                {
                    return true;
                }

                char a = text[min];
                char b = text[max];

                if (a != b)
                {
                    return false;
                }

                min++;
                max--;
            }
        }
    }

I'm really stuck here, I would like to thank you in advance.

Answer
1

As far as can see, you don't want interfaces, class instances to check for a palindrome (do you really want to implement several algorithms to choose from?), but a static method:

    // Let's class be partial one: if you want to add a method it it
    // you don't have to modify this code but 
    // add a chunk public partial static class MyStringRoutine {...}
    public partial static class MyStringRoutine {
      public static bool IsPalindrome(string text) {
        //DONE: do not forget about special cases
        if (string.IsNullOrEmpty(text))
          return true;

        for (int i = 0; i < text.Length / 2; ++i)
          if (text[i] != text[text.Length - 1 - i])
            return false;

        return true;
      }
    }

Then you can use it:

    private void InputText_TextChanged(object sender, TextChangedEventArgs e) {
      if (string.IsNullOrEmpty(InputText.Text)) 
        OutputText.Clear();
      else {  
        string suffix = MyStringRoutine.IsPalindrome(InputText.Text) 
          ? "is a palindrome"
          : "is NOT a palindrome";

        OutputText.Text = $"{InputText.Text} {suffix}";  
      }
    }

If you have to implement ICheckPalindrome interface, and thus to work with class instance, you have to create the instance:

  private void InputText_TextChanged(object sender, TextChangedEventArgs e) {
    if (string.IsNullOrEmpty(InputText.Text)) 
      OutputText.Clear();
    else {
      // You have to create the instance (checker)
      ICheckPalindrome checker = new PalindromeChecker(); 

      // IsPalindrome is the instance method; you can't call is as 
      // PalindromeChecker.IsPalindrome
      string suffix = checker.IsPalindrome(InputText.Text) 
        ? "is a palindrome"
        : "is NOT a palindrome";

      OutputText.Text = $"{InputText.Text} {suffix}";  
    }  
  } 
How to implement Interface in Mainwindow
See more ...