Home > Software design >  Why is C# interface not implementing with apparently correct namespace?
Why is C# interface not implementing with apparently correct namespace?

Time:02-25

I am a total C# (and OOP) novice, so perhaps this is a very simple question, but I am stumped.

I am trying to write a method that implements the IValueConverter interface, bound to a simple XAML GUI. It won't compile, and VS tells me:

Error   XLS0414 The type 'local:YesNoToBooleanConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.       
Error   XDG0008 The name "YesNoToBooleanConverter" does not exist in the namespace "clr-namespace:ValueConverter".
Error   CS0535  'MainWindow.YesNoToBooleanConverter' does not implement interface member 'IValueConverter.ConvertBack(object, Type, object, CultureInfo)'
Error   CS0535  'MainWindow.YesNoToBooleanConverter' does not implement interface member 'IValueConverter.ConvertBack(object, Type, object, CultureInfo)'   

What I don't understand are the CS0535 errors. The Convert() and ConvertBack() arguments match the MS docs exactly, I have the proper namespaces added as far as I can tell, and I've cleaned and rebuilt the project. I have not seen this problem addressed directly, so either my understanding is fundamentally flawed or my machine is acting weird.

Does anyone have any guidance?

My C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ValueConverter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public class YesNoToBooleanConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                switch (value.ToString().ToLower())
                {
                    case "yes":
                    case "oui":
                        return true;
                    case "no":
                    case "non":
                        return false;
                }
                return false;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                if ((bool)value == true)
                {
                    return "yes";
                }
                else
                {
                    return "no";
                }
            }
            return "no";
        }
    }
}

My XAML:

<Window x:Class="ValueConverter.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ValueConverter"
    mc:Ignorable="d"
    Title="Converter Sample" Height="450" Width="800">
<Window.Resources>
    <local:YesNoToBooleanConverter x:Key="YesNoToBooleanConverter" />
</Window.Resources>
<StackPanel Margin="10">
    <TextBox Name="txtValue" />
    <WrapPanel Margin="0,10">
        <TextBlock Text="Current value is: "/>
        <TextBlock Text="{Binding Path=Text, ElementName=txtValue, Converter={StaticResource YesNoToBooleanConverter}}" />
    </WrapPanel>
    <CheckBox IsChecked="{Binding Path=Text, ElementName=txtValue, Converter={StaticResource YesNoToBooleanConverter}}" Content="Yes" />
</StackPanel>

CodePudding user response:

Your ConvertBack method is nested under MainWindow, instead of under YesToNoBooleanConverter.

Also because YesToNoBooleanConverter is nested inside MainWindow, you would have to reference local:MainWindow.YesToNoBooleanConverter. Probably makes more sense to move it out into being its own class instead of a nested class

  • Related