Why I have a (error does not implement interface member) silverlight - c#

PagesCollection.ViewModel.PagePictureCommands.cs
namespace PagesCollection.ViewModel
{
public partial class PagePicturesViewModel : IPropertieCommands
{
private ICommand deleteAlbum;
public ICommand _CreateAlbum
{
get
{
if (createAlbum == null)
createAlbum = new Model.DelegateCommand(CreateAlbum, CanAdd);
return createAlbum;
}
}
}
}
PagesCollection.ViewModel.PagePicturesViewModel.cs
namespace PagesCollection.ViewModel
{
public partial class PagePicturesViewModel : IPictureMethods
{
public void CreateAlbum(object param)
{...}
}
}
I have one 2 interfaces and one class which divided on 2.Each one half of class has implemented some of those interfaces.But I have a very strange error.
('PagesCollection.ViewModel.PagePicturesViewModel' does not implement interface member 'PagesCollection.Model.IPropertieCommands._CreateAlbum.set')
Can u help me please?

What is it you don't understand, because the error message seems pretty descriptive:
PagesCollection.ViewModel.PagePicturesViewModel' does not implement
interface member
'PagesCollection.Model.IPropertieCommands._CreateAlbum.set
I suspect that the interface looks like:
public interface IPropertieCommands
{
ICommand _CreateAlbum { get; set; }
}
Which defines that you must have a setter on that property!
So just add a setter in your implementation:
public ICommand _CreateAlbum
{
get
{
if (createAlbum == null)
createAlbum = new Model.DelegateCommand(CreateAlbum, CanAdd);
return createAlbum;
}
set
{
createdAlbum = value; // or something else sensible!
}
}

It looks like your IPropertieCommands interface requires that the _CreateAlbum property has a setter - but you've only implemented a getter.

public interface IPropertieCommands
{
ICommand _CreateAlbum { get;}
}
If you don't want this property to have a setter (readonly), you can have your interface code like above.

Related

Getter and setter coming from different interfaces

I really need to have something like this:
interface IReadableVar
{
object Value { get; }
}
interface IWritableVar
{
object Value { set; }
}
interface IReadableWritableVar : IReadableVar, IWritableVar
{
}
However when I try to use IReadableWritableVar.Value I get compile errors unless I explicitly cast to base interface, like here:
static void Main()
{
IReadableWritableVar var = null;
var t = var.Value; // <-- CS0229: Ambiguity between 'IReadableVar.Value' and 'IWritableVar.Value'
var.Value = null; // <-- CS0229: Ambiguity between 'IReadableVar.Value' and 'IWritableVar.Value'
var v = ((IReadableVar)var).Value; // compiles fine
((IWritableVar)var).Value = null; // compiles fine
}
Why do I get these errors although everything should be clear to the compiler? Is there any way to fix this problem other than casting (hundreds of places in the application)?
Update: it was suggested this is a dupe of Implementing 2 Interfaces with 'Same Name' Properties but this is slightly different as in the other case there's no inheritance in interfaces. Anyway, the problem is solved now - see accepted answer.
A possible workaround can be modify your interface IReadableWritableVar like this:
interface IReadableWritableVar : IReadableVar, IWritableVar
{
new object Value { get; set; }
}
But keep in my that a valid implementation should be:
class ReadableWritableVar : IReadableWritableVar
{
public object Value
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
object IWritableVar.Value
{
set { throw new NotImplementedException(); }
}
object IReadableVar.Value
{
get { throw new NotImplementedException(); }
}
}
A more concrete example:
class ReadableWritableVar : IReadableWritableVar
{
public object Value
{
get { return ((IReadableVar)this).Value; }
set { ((IWritableVar)this).Value = value; }
}
object _val;
object IWritableVar.Value { set { _val = value; } }
object IReadableVar.Value => _val;
}
Or even better:
class ReadableWritableVar : IReadableWritableVar
{
public object Value { get; set; }
object IWritableVar.Value { set { Value = value; } }
object IReadableVar.Value => Value;
}
Interesting question. I think extension methods will help in this case.
public static class Extension
{
public static object GetValue(this IReadableVar v)
{
return v.Value;
}
public static void SetValue(this IWritableVar v, object value)
{
v.Value = value;
}
}
You need to change the code to use it:
IReadableWritableVar variable = null;
var t = variable.GetValue();
variable.SetValue(null);
The extension method does the cast for you.
Well, effectively Getter and Setter are just two methods. When we use IReadableWritableVar interface there are two methods with identical name inherited from base interfaces and compiler doesn't know which of these two should it use hence the ambiguity.
When we cast that to one of these interfaces the other member's gone and there's no error.
If we implement those member there will be no error as compiler will use that implementation:
class ReadableWritableVar : IReadableWritableVar
{
public object Value { get; set; }
}
var #var = new ReadableWritableVar();
var t = #var.Value;
Also you can use an explicit interface members implementation from #Alessandro D'Andria's answer if it is required that you use interface and not class.
using abstract class istead of interface will resolve your problem.
public abstract class ReadableWritableVar : IReadableVar, IWritableVar
{
public object Value { get; set; }
}
One possible alternative is to use explicit (java style) get and set methods instead of a property:
interface IReadableVar
{
object GetValue();
}
interface IWritableVar
{
void SetValue(object value);
}
interface IReadableWritableVar : IReadableVar, IWritableVar
{
}
The usage then becomes:
static void Main(string[] args)
{
IReadableWritableVar aVar = null;
var t = aVar.GetValue();
aVar.SetValue(null);
}

Inject a string property in a class using Ninject

One of my interfaces has a string property that will depend on where the interface is being used. I want to avoid hardcoding the property every time the object is created. I can set the property in constructor, but the object is injected using a factory.
The interface as follows:
public interface IObjectStore
{
string StorageTableName { get; set;}
void UpdateObjectStore(string key, string value);
string ReadObjectStore(string key);
}
Which is used in a service
public class CategoryService<T> : ICategoryService<T> where T : Company
{
private readonly IObjectStore objectStore;
public CategoryService(IObjectStore objStore)
{
this.objectStore = objStore;
objectStore.StorageTableName = "CategoryTable"; // I want to avoid this hard coding
}
...
}
The service is created using service factory (Ninject.Extensions.Factory)
public interface IServiceFactory
{
ICategoryService<T> CreateCategoryService<T>() where T : class;
}
Which is then injected using Ninject at the controller level. Here are my bindings
bool storeInNoSql = true;
kernel.Bind<IServiceFactory>().ToFactory().InSingletonScope();
kernel.Bind<ICategoryService<Article>>().To<CategoryService<Article>>();
kernel.Bind<IObjectStore>().ToMethod(ctx => storeInNoSql ? ctx.Kernel.Get<ObjectStore>() : null);
So the question is: how do i tell Ninject to set the property StorageTableName to "CategoryTable" everytime the object is injected into CategoryService and to "ArticleTable" everytime it is inserted into ArticleService?
I think this is what you are looking for.
It's just a very small sample project I just did, but this should solve your problem.
public class Ninject_34091099
{
public static void Run()
{
using (IKernel kernel = new StandardKernel())
{
kernel.Bind<IInterface<Generic1>>()
.To<Class<Generic1>>()
.WithConstructorArgument("name", "STRING ONE");
kernel.Bind<IInterface<Generic2>>()
.To<Class<Generic2>>()
.WithConstructorArgument("name", "The other string");
kernel.Bind<IServiceFactory>().ToFactory().InSingletonScope();
var factory = kernel.Get<IServiceFactory>();
var c1 = factory.CreateInterface<Generic1>();
var c2 = factory.CreateInterface<Generic2>();
Console.WriteLine(c1.Name);
Console.WriteLine(c2.Name);
}
Console.WriteLine("Done");
Console.ReadLine();
}
}
public interface IInterface<T> where T : class
{
string Name { get; set; }
}
public class Generic1
{
}
public class Generic2
{
}
public class Class<T> : IInterface<T> where T : class
{
public string Name { get; set; }
public Class(string name)
{
Name = name;
}
}
public interface IServiceFactory
{
IInterface<T> CreateInterface<T>() where T : class;
}
Sorry that the names mean nothing :D
Hope it helps

Why do I need to declare the type?

I have the following code:
public interface IMyActionFactory
{
AbstractAction<T> CreateAction<T>(MyActionParamBase paramBase = null)
where T : MyActionParamBase;
}
public sealed class MergeActionParam : MyActionParamBase
{
}
public class MergeTest
{
private readonly IMyActionFactory _actionFactory = new DefaultMyActionFactory();
[Theory]
[PropertyData("MergeWorksData")]
public void MergeWorks(/*params here*/)
{
var param = new MergeActionParam();
// populate param here
var sut = _actionFactory.CreateAction<MergeActionParam>(param);
sut.DoAction();
}
}
I am getting an error
"..Error 10 Using the generic type 'IMyActionFactory' requires 1
type arguments..."
Why does the compiler expect a type to be passed to my IMyActionFactory, since I have declared the interface without a T? As far as the method is concerned its the only one to expect the type. Am I missing something here?
How can I make it work without redefining the interface signature?
EDIT:
Feeling a bit embarassed here, because the quick code I put down and ran seperately in a standalone online c# compiler doesnt give any compilation errors. However, going back to my original solution (tens of projects altogether) the error is still there.. Maybe has something to do with the XUnit ?..not sure
public interface IMyActionFactory
{
AbstractAction<T> CreateAction<T>(MyActionParamBase paramBase = null)
where T : MyActionParamBase;
}
public interface IAction
{
void DoAction();
}
public abstract class AbstractAction<T> : IAction
where T : MyActionParamBase
{
public void DoAction()
{
}
}
public class MyActionParamBase
{
public MyActionParamBase()
{
}
}
public sealed class MergeActionParam : MyActionParamBase
{
}
public class DefaultMyActionFactory : IMyActionFactory
{
public AbstractAction<T> CreateAction<T>(MyActionParamBase paramBase = null) where T : MyActionParamBase
{
return null;
}
}
public class MergeTest
{
private readonly IMyActionFactory _actionFactory = new DefaultMyActionFactory();
public void MergeWorks(/*params here*/)
{
var param = new MergeActionParam();
// populate param here
var sut = _actionFactory.CreateAction<MergeActionParam>(param);
sut.DoAction();
}
}

how to use constructor injection with abstract class?

I have been trying to learn dependency injection but I have 2 errors when I run my code:
DependencyInjection.Message.Sms() must declare a body because it is not marked abstract, extern, or partial.
Cannot implicitly convert type DependencyInjection.IInterface to DependencyInjection.MyClass. An explicit conversion exists (are you missing a cast?)
Is the code below a good design for DI?
namespace DependencyInjection
{
public interface IInterface
{
}
public abstract class Message
{
public virtual void Sms();
}
public class MyClass : Message, IInterface
{
public override void Sms()
{
Console.WriteLine("Sms gönder.");
}
}
public class ClassManager
{
private IInterface _myinterface;
public MyClass Xyz
{
get { return _myinterface; }
set { _myinterface = value; }
}
public ClassManager(IInterface myinterface)
{
_myinterface = myinterface;
}
}
}
1) DependencyInjection.Message.Sms()' must declare a body because it is not marked abstract, extern, or partial
Add abstract keyword (and remove virtual) to method declaration:
public abstract void Sms();
2) Cannot implicitly convert type 'DependencyInjection.IInterface' to 'DependencyInjection.MyClass'. An explicit conversion exists (are you missing a cast?)
private IInterface _myinterface;
public MyClass Xyz
{
get { return _myinterface; }
set { _myinterface = value; }
}
Xyz has return-type of type MyClass but in get you are returning _myinterface which is of type IInterface.
Change to following:
public IInterface Xyz
{
get { return _myinterface; }
set { _myinterface = value; }
}
Change this
public abstract class Message
{
public virtual void Sms();
}
to this
public abstract class Message
{
public abstract void Sms();
}
You either give the virtual method a body, like so
public virtual void Sms() { }
or you mark it as abstract
public abstract void Sms();
EDIT:
Forgot about the second error. Change this
public MyClass Xyz
{
get { return _myinterface; }
set { _myinterface = value; }
}
to this
public IInterface Xyz
{
get { return _myinterface; }
set { _myinterface = value; }
}
You're declaring a virtual method with no body. Virtual methods must declare a body that will act as the default implementation of the method. You can either declare your method as abstract, which means that the derived classes have to provide their own implementation:
public abstract void Sms();
or you can keep the method virtual and provide a default implementation in the base class:
public virtual void Sms()
{
// Default or no implementation goes here.
}
In the following code you're trying to cast IInterface to MyClass, which is probably not what you want.
public MyClass Xyz
{
get { return _myinterface; }
set { _myinterface = value; }
}
you're probably looking to return IInterface instead:
public IInterface Xyz
{
get { return _myinterface; }
set { _myinterface = value; }
}

Return IEnumerable<IMyInterface> from List<MyInterfaceClass>

EDIT: This question would be invalid in .NET 4 since it actually works as desired.
I have a Data class that must implement an interface like this:
public interface IData
{
IEnumberable<IOther> OtherList { get; }
IOther AddOther();
void RemoveOtherData(IOther data);
}
But I am stuck with declaring the actual member in Data
public class Data : IData
{
// desired, always return the same reference
public IEnumberable<IOther> OtherList { get { return _mOtherList } }
// Non persistent reference not desirable.
public IEnumerable<IOther> OtherList { get { return _mOtherList.Select(x => x as IOther); } }
List<IOther> _mOtherList = new List<Other>(); // error, type mismatch
List<Other> _mOtherList = new List<Other>(); // error, property return type mismatch
IEnumerable<IOther> _mOtherList = new List<Other>(); // ok, but cannot use List methods without casting.
}
What would be the best solution in this case?
public class Data : IData
{
public IEnumerable<IOther> OtherList { get; private set; }
List<Other> _mOtherList = new List<Other>();
public Data()
{
OtherList=mOtherList.Cast<IOther>();
}
}
On .net 4 IEnumerable<out T> is co-variant. i.e. a class that implements IEnumerable<Other> automatically implements IEnumerable<IOther> too. So could also simply write:
public class Data : IData
{
public IEnumerable<IOther> OtherList { get{return mOtherList;} }
List<Other> _mOtherList = new List<Other>();
}
But I'd avoid that, since it breaks encapsulation and allows outsiders to modify your list.
((List<Other>)MyData.OtherList).Add(...);
Other class must implement IOther interface and you don't need to cast.
When you declare _mOtherList, it's IEnumerable, so you can't use list methods. Declare it as a list.
public class Data : IData
{
List<IOther> _mOtherList = new List<Other>();
public IEnumberable<IOther> OtherList { get { return _mOtherList } }
IOther AddOther()
{
return null;
}
void RemoveOtherData(IOther data){}
}
Your Other class:
class Other : IOther
{
//some members
}
As IEnumerable is covariant this is fine:
public interface IInterface{}
public class ClassA : IInterface{}
public class ClassB
{
private readonly List<ClassA> _classAs;
public IEnumerable<IInterface> Data{ get { return _classAs; } }
}

Categories

Resources