I am trying to save a collection of object that are a type of my class. I get an error stating:
The collection data contract type 'System.Collections.Generic.List cannot be deserialized because it does not have a public parameterless constructor.
Adding a public parameterless constructor will fix this error.
Here is my class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MojProjekt
{
class Lekarna
{
public string Ime { get; set; }
public Lekarna()
{
}
}
}
And here is how I save to the IsolatedStorage:
List<Lekarna> lekarneList = new List<Lekarna>();
// here I then fill the list ...
IsolatedStorageSettings localStorage = IsolatedStorageSettings.ApplicationSettings;
localStorage.Add("lekarneList", lekarneList;
localStorage.Save();
make the class public
public class Lekarna
Related
I writed this code, but, when I try to build, the compiler returns:
1>code.cs(16,16,16,44): error CS0542: 'DataGridViewPercentageColumn': member names cannot be the same as their enclosing type
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;
namespace TestSample
{
internal static class DataGridViewPercentageColumn
{
public class DataGridViewPercentageColumn : DataGridViewColumn
{
public DataGridViewPercentageColumn() : base(new DataGridViewPercentageCell())
{
}
}
public class DataGridViewPercentageCell : DataGridViewTextBoxCell
{
public DataGridViewPercentageCell()
{
this.Style.Format = "0%";
}
}
}
}
How I can solve this, and, why this error happens??
Thanks
You cannot nest two classes with the same names. Your inner class DataGridViewPercentageColumn has the same name like the outer class. You have to rename one of these classes like:
internal class DataGridViewClasses{
public class DataGridViewPercentageColumn : DataGridViewColumn
{
public DataGridViewPercentageColumn() : base(new DataGridViewPercentageCell())
{
}
}
public class DataGridViewPercentageCell : DataGridViewTextBoxCell
{
public DataGridViewPercentageCell()
{
this.Style.Format = "0%";
}
}
}
Btw: You cannot have any other members or properties inside a class named like the class itself.
Your wrapper class has the same name as one of the internal classes.
The issue is DataGridViewPercentageColumn.
The problem here is that you've defined a class within another class, both of which have the same name. Give the internal static class a different name.
Looking to cast an Object to a known type at runtime. I have a class (call it Item for ease) which is the base class for Box. Box has it's own properties as well as the ones from Item (obviously).
Basically I create an instance of Box using the CreateInstance method, this creates an Object of type object but the true type (as witnessed when doing 'typeof') is of type Box. I need to cast this Object back to Box without hard coding any switch / if etc. The code I have to test this is below and I'm running out of ideas.
//Base Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace Test11
{
public class Item
{
public int property1 { get; set; }
public int property2 { get; set; }
public int property3 { get; set; }
public Item()
{
property1 = 1;
property2 = 2;
property3 = 3;
}
}
//Box Class - Inherits from Item
namespace Test11
{
public class Box : Item
{
public int property4 { get; set; }
public Box()
{
property4 = 4;
}
}
}
//Application Class
namespace Test11
{
class Program
{
static void Main(string[] args)
{
List<Item> BaseList = new List<Item>();
object obj = Assembly.GetExecutingAssembly().CreateInstance("Test11.Box");
Type t = Type.GetType("Test11.Box");
//The following line does not work, need to make it work :)
//BaseList.Add(obj as t);
Console.WriteLine(t.ToString());
Console.ReadLine();
}
}
}
I've tried many different ways now, the one featured above is one of many. Any ideas or help out there?
Your BaseList is expecting Item objects. You have to cast:
if (obj is Item)
BaseList.Add((Item)obj);
Or:
if (typeof(Item).IsAssignableFrom(t))
BaseList.Add((Item)obj);
Are you working with dynamically loaded assemblies? If you know for sure that it will be Box, could you declare Box as a partial class near Item and fill in the implementation details for it in your dynamic assembly?
Not sure if it'll work, I've not tried that particular problem.
I have created an interface as shown below. The DTO object is a complex value object with 3 parameters.
public interface IOperation
{
DTO Operate(DTO ArchiveAndPurgeDTO);
}
I need people that impliment this interface to be able to inherit from the original Value object and extend it where required.
My assumption was that they could simply inherit the DTO object, add (for example) another property and use it in the same class that impliments this interface.
When I try to use the extended value object, Visual Studio complains that I am no longer implimenting the interface.
How can I impliment this functionality.
Thanks in advance for any ideas, and/or suggestions.
Gineer
Edit:
DTO Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Company.ArchiveAndPurge
{
public class DTO
{
public DTO(String FriendlyID)
{
friendlyId = FriendlyID;
}
private String friendlyId = String.Empty;
public String FriendlyId
{
get { return friendlyId; }
set { friendlyId = value; }
}
private String internalId = String.Empty;
public String InternalyId
{
get { return internalId; }
set { internalId = value; }
}
private Boolean archivedSuccessfully = false;
public Boolean ArchivedSuccessfully
{
get { return archivedSuccessfully; }
set { archivedSuccessfully = value; }
}
}
}
Extended DTO:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Company.MSO.ArchiveAndPurge
{
public class DTO: Company.ArchiveAndPurge.DTO
{
private Boolean requiresArchiving = true;
public Boolean RequiresArchiving
{
get { return requiresArchiving; }
set { requiresArchiving = value; }
}
}
}
Interface Implementation where VS Complains:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Company.ArchiveAndPurge.Contracts;
using Company.ArchiveAndPurge;
namespace Company.MSO.ArchiveAndPurge
{
public class ResolveFriendlyId: IOperation
{
#region IOperation Members
public DTO Operate(DTO ArchiveAndPurgeDTO)
{
ArchiveAndPurgeDTO.InternalyId = ArchiveAndPurgeDTO.FriendlyId;
return ArchiveAndPurgeDTO;
}
#endregion
}
}
As I understand it, you probably had something like:
public class ExtendedOperation : IOperation
{
public ExtendedDTO Operate(ExtendedDTO dto)
{
...
}
}
That doesn't work in two ways:
You can't change the return type when implementing an interface method
You can't change the parameter list when implementing an interface
In particular, you wouldn't be implementing IOperation in a way which would be compatible with code like this:
IOperation operation = new ExtendedOperation();
operation.Operate(new DTO());
I suspect you might want to make the interface generic:
public interface IOperation<T> where T : DTO
{
T Operate(T dto);
}
Use Generics:
public interface IOperation<T> where T : DTO
{
T Operate(T ArchiveAndPurgeDTO);
}
I have some strange problem. I have a solution with the following structure
http://i33.tinypic.com/10fbzbq.jpg
As you can see when i wonna import the VDB.Common.RequestAndResponses it gives an error.
The namespace of dat Class Library is VDB.Common.RequestAndResponses.
I am new in c# , so it could be that i forgot something stupid.
I strongly suspect that Base.cs (the only C# file shown in the VDB.Common.RequestAndResponses project) doesn't actually declare a type in the VDB.Common.RequestAndResponses namespace - or that it only declares an internal (rather than public) type.
For example, note that the code you're creating is under the VDB.Client.Infrastructure project, but is only declaring a class in the Agatha namespace - not VDB.Client.Infrastructure.Agatha, which may be what you were intending. Do you have the same kind of thing in Base.cs, perhaps?
Without seeing the code in Base.cs, we can't see what's wrong though. If you could just post a snippet of that - just the namespace and class declaration - that would be helpful.
Note that although a class library has a default namespace, this isn't prepended to whatever the source file actually declares. In other words, in a library of Acme.Widgets, if you had a declaration of:
namespace Web
{
public class Button {}
}
that would only declare the type Web.Button, not Acme.Widgets.Web.Button.
EDIT: The OP's "answer" confirms what I thought... basically it's not declaring a namespace at all. It should look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agatha.Common;
namespace VDB.Common.RequestAndResponses
{
public abstract class BaseRequest :Request
{
// Code
}
public abstract class BaseResponse : Response
{
// Code
}
}
I would also strongly advise that these classes should be put in two separate files, BaseRequest.cs and BaseResponse.cs. I'm also pretty surprised to see a reference to Agatha.Common - shouldn't that be VDB.Common.Agatha or something like that?
Right click on the "VDB.Common.RequestAndResponses" reference in solution explorer and choose "Show in object browser", make sure the namespace is found there with the exact spelling and capitalization.
Try to use the Base class in the client code and hover over it and allow the Visual Studio IDE to prompt you to add the appropriate namespace. The namespace defined in the Base class could be different to what you think.
EDIT
As Jon as demonstrated in the 2nd part of his answer - the name of the code file does not automatically correspond to the namespace.
The Base.cs file looks like this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agatha.Common;
public abstract class BaseRequest :Request
{
public string UserName { get; set; }
public string UserDomainName { get; set; }
public string ClientLanguageCode { get; set; }
public DateTime ClientCreated { get; set; }
public DateTime ClientSent { get; set; }
public DateTime ServerReceived { get; set; }
public DateTime ServerProcessed { get; set; }
public void BeforeSend(IUserContext context)
{
ClientSent = DateTime.UtcNow;
UserName = context.UserName;
UserDomainName = context.UserDomainName;
ClientLanguageCode = context.LanguageCode;
}
}
public abstract class BaseResponse : Response
{
public DateTime ServerCreated { get; set; }
public DateTime ServerProcessed { get; set; }
public string[] ValidationErrors { get; set; }
public bool IsValid
{
get { return Exception == null & !ValidationErrors.Any(); }
}
}
While dividing my C# application in layers, I have solved the problem of circular dependency among layers in the following way:
using System;
using System.Collections.Generic;
using System.Text;
using SolvingCircularDependency.Common;
using SolvingCircularDependency.DA;
namespace SolvingCircularDependency.BO
{
public class MyClass : IPersistent
{
private string _message;
public string Message
{
get { return _message; }
set { _message = value; }
}
public bool Save()
{
return MyClassDA.Save(this);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace SolvingCircularDependency.Common
{
public interface IPersistent
{
bool Save();
string Message { get;}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using SolvingCircularDependency.Common;
namespace SolvingCircularDependency.DA
{
public class MyClassDA
{
public static bool Save(IPersistent obj)
{
Console.WriteLine(obj.Message);
return true;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using SolvingCircularDependency.BO;
namespace SolvingCircularDependency.UI
{
class Program
{
static void Main(string[] args)
{
MyClass myobj = new MyClass();
myobj.Message = "Goodbye Circular Dependency!";
myobj.Save();
Console.ReadLine();
}
}
}
Please take a look at the class MyClassDA in the DA layer and the assembly itself.
How can a MyDA.Get() method return objects of type MyClass when the Data Access layer doesn't know about the MyClass type.
If this design is not efficient, How can I change/modify it?
As far as I can understand you have a bidirectional relationship between your DA and Business layer.
To solve this problem I suggest that you should have 3 layers instead of two. I mean you should have a Model layer that simply model the DB objects ,then you can derive from model classes in your Business layer and add other behaviors like Save method.
Here's what I mean:
//Model Layer
public class UserModel
{
public virtual string Firstname{get;set;}
}
//DataAccess Layer
public class UserDao
{
List<UserModel> GetAll();
}
//BusinessLayer
public class UserDomainModel:UserModel
{
public UserDomainModel(UserModel user,UserDao dao)
{
_user=user;
_dao=dao;
}
public override string FirstName
{
get
{
return _user.FirstName;
}
set
{
_user.FirstName=value;
}
public void Save()
{
_dao.Save(_user);
}
}
}
I'm using a decorator to combine User and UserDao as a domain model object.
One of the reasons people do Persistance Ignorant objects (POCO) is to avoid such a scenario. There is simply no way for the data access layer to have a reference to a class that it doesn't know about - it is much better to have the class not know about the data access.
The only way you can really do this is to implement Get() on User instead of on UserDA. You can do something like this:
public class User {
IGetFromPresistance<User> _userFetcher;
public static IList<User> GetMatching(Specification<User> spec) {
var values = _userFetcher.Find(spec); //Returns a DataRow or IDictionary<string, object>
return new User() {
PhoneNumber = new PhoneNumber(values["phone"].ToString()),
Name = values["name"].ToString(),
};
}
}