Kil Gore

Kil Gore

  • NA
  • 9
  • 635

How to implement Interface in Mainwindow

Jul 8 2019 5:43 AM
Hi,
 
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.
  1. interface ICheckPalindrome    
  2.     {    
  3.         bool IsPalindrome(string text);    
  4.     }    
  5.     
  6. public class PalindromeChecker : ICheckPalindrome    
  7.     {    
  8.     
  9.         ///     
  10.         /// Method for checking if the word/text is a palindrome.    
  11.         ///     
  12.         public bool IsPalindrome(string text)    
  13.         {    
  14.             ......    
  15.             //Code    
  16.             }    
  17.         }    
  18.     }    
  19.  namespace TextChecker    
  20. {    
  21.     ///     
  22.     /// Interaction logic for MainWindow.xaml    
  23.     ///     
  24.     public partial class MainWindow : Window    
  25.     {    
  26.         public MainWindow()    
  27.         {    
  28.             InitializeComponent();    
  29.     
  30.             lblInput.Foreground = Brushes.ForestGreen;    
  31.             lblResult.Foreground = Brushes.ForestGreen;    
  32.             lblTitel.Foreground = Brushes.ForestGreen;    
  33.     
  34.         }    
  35.     
  36.         ///     
  37.         /// User input and checking the input if the word a palindrome is.    
  38.         ///     
  39.         private void InputText_TextChanged(object sender, TextChangedEventArgs e)    
  40.         {    
  41.             string text = InputText.Text;    
  42.     
  43.             bool isPalindrome = PalindromeChecker.IsPalindrome(text);    
  44.     
  45.             OutputText.Text = text + (isPalindrome ? " is a palindrome" : " is NOT a palindrome");    
  46.     
  47.             if (InputText.Text == string.Empty)    
  48.             {    
  49.                 OutputText.Clear();    
  50.             }    
  51.         }    
  52.         private void ButtonClicked(object sender, RoutedEventArgs e)    
  53.         {    
  54.             SubWindow subWindow = new SubWindow();    
  55.             subWindow.Show();    
  56.         }    
  57.     }    
  58. }    
  59.     
  60.  public class PalindromeChecker : ICheckPalindrome    
  61.     {    
  62.     
  63.         ///     
  64.         /// Method for checking if the word/text is a palindrome.    
  65.         ///     
  66.         public bool IsPalindrome(string text)    
  67.         {    
  68.             int min = 0;    
  69.             int max = text.Length - 1;    
  70.     
  71.             while (true)    
  72.             {    
  73.                 if (min > max)    
  74.                 {    
  75.                     return true;    
  76.                 }    
  77.     
  78.                 char a = text[min];    
  79.                 char b = text[max];    
  80.     
  81.                 if (a != b)    
  82.                 {    
  83.                     return false;    
  84.                 }    
  85.     
  86.                 min++;    
  87.                 max--;    
  88.             }    
  89.         }    
  90.     }    
 

Answers (4)