This is my set up,
class CostPeriodDto : IPeriodCalculation
{
public decimal? a { get; set; }
public decimal? b { get; set; }
public decimal? c { get; set; }
public decimal? d { get; set; }
}
interface IPeriodCalculation
{
decimal? a { get; set; }
decimal? b { get; set; }
}
class myDto
{
public List<CostPeriodDto> costPeriodList{ get; set; }
public List<IPeriodCalculation> periodCalcList
{
get
{
return this.costPeriodList; // compile error
}
}
}
What would be the best way of doing this?
Use Cast<IPeriodCalculation>() :
public class CostPeriodDto : IPeriodCalculation
{
public decimal? a { get; set; }
public decimal? b { get; set; }
public decimal? c { get; set; }
public decimal? d { get; set; }
}
public interface IPeriodCalculation
{
decimal? a { get; set; }
decimal? b { get; set; }
}
public class myDto
{
public List<CostPeriodDto> costPeriodList { get; set; }
public List<IPeriodCalculation> periodCalcList
{
get
{
return this.costPeriodList.Cast<IPeriodCalculation>().ToList();
}
}
}
I believe in C#4, if you were using something implementing IEnumerable<out T>, you could simply do it the way you wrote it, and it would be resolved using Covariance.
class myDto
{
public IEnumerable<CostPeriodDto> costPeriodList{ get; set; }
public IEnumerable<IPeriodCalculation> periodCalcList
{
get
{
return this.costPeriodList; // wont give a compilation error
}
}
}
Try return this.costPeriodList.Cast<IPeriodCalculation>().ToList().
The LINQ methods to cast from one sequence to another will not be equal. That is to say that the following test would fail if you used Cast()/ToList().
Assert.AreSame(myDto.costPeriodList, myDto.periodCalcList);
Furthermore, using those methods means that if you tried to add an item to one collection, they would not be reflected in the other. And every time you called periodCalcList, it would be creating an entirely new collection which could be disastrous depending on how many items, how frequently it's called, etc.
A better solution, in my opinion, is to not use List<T> for holding the CostPeriodDto and instead use a collection derived from Collection<T> and explicitly implement IEnumerable<IPeriodCalculation>. Optionally you could implement IList<IPeriodCalculation> if you needed to.
class CostPeriodDtoCollection :
Collection<CostPeriodDto>,
IEnumerable<IPeriodCalculation>
{
IEnumerable<IPeriodCalculation>.GetEnumerator() {
foreach (IPeriodCalculation item in this) {
yield return item;
}
}
}
class MyDto {
public CostPeriodDtoCollection CostPeriods { get; set; }
public IEnumerable<IPeriodCalculation> PeriodCalcList {
get { return CostPeriods; }
}
}
Related
I'm trying to create some interfaces. The IReportSection object will have one string and a collection of items, which could be different depending on what we're working with. Do I need to make it generic?
The IReport will have one string and a collection of IReportSection.
Here's how I'm trying to define it now.
public interface IReport
{
string ReportName { get; set; }
ICollection<IReportSection> ReportSections { get; }
}
public interface IReportSection
{
string ReportSectionName { get; set; }
ICollection ReportItems { get; }
}
public abstract class ReportSectionBase : IReportSection
{
public string ReportSectionName { get; set; }
public ICollection ReportItems { get; set; }
}
And my models:
pulic class ProjectSubmissionViewModel
{
public int ProjectSubmissionId { get; set; }
public string SubmissionTitle { get; set; }
}
pulic class AffiliateViewModel
{
public int AffiliateId { get; set; }
public string AffiliateName { get; set; }
}
This is how I'm trying to use it in code:
public class ChapterAffiliates : ReportSectionBase
{
public string ReportSectionName { get { return "Chapter Affiliates"; } }
public ICollection<AffiliateViewModel> ReportItems { get; set; }
}
public class ChapterTitles : ReportSectionBase
{
public string ReportSectionName { get { return "Chapter Titles"; } }
public ICollection<ProjectSubmissionViewModel> ReportItems { get; set; }
}
public class SubmissionListViewModel : IReport
{
public ICollection<ProjectSubmissionViewModel> Submissions { get; set; }
public ICollection<AffiliateViewModel> Affiliates{ get; set; }
public string ReportName { get; set; }
public ICollection<IReportSection> ReportSections
{
get
{
var affiliateSection = new ChapterAffiliates
{
ReportItems = Affiliates
};
var titleSection = new ChapterTitles
{
ReportItems = Submissions.Where(s => s.SubmissionTitle.Contains("SomePhrase")).ToList()
};
var sections = new List<IReportSection> { {subSection}, {titleSection} };
return sections;
}
}
}
I'm not sure how to best define this. I'm pretty sure I've done it before, but it's not coming to me.
Are the type parameters for TRType all the same within a certain report? E.g. will you have report sections with different report types in them?
If all types within a report are the same, the solution is relatively simple:
public interface IReport<T> { ... }
If this is not the case - you'll have to do something different, e.g:
public interface IReportSection
{
string ReportSectionName { get; }
ICollection ReportItems { get; }
}
public abstract class ReportSectionBase<TRType> : IReportSection {
...
}
This allows you to put different underlying types in the ReportSections collection related to the report. You'll have to do some more work to get the exact information that you need out of each report section.
Just trying to get my head around this one. Consider the following code.
public class Setting<T> where T: struct
{
public string Name { get; set; }
public T Value { get; set; }
}
public class SettingsDto
{
public List<Setting<>> Settings{ get; set; }
}
The issue is in the SettingsDto class. My Settings property wants me to specify a type for List>. All I know right now is that T must be a struct. How can I create a list of Settings such that I can add to it later - perhaps at runtime. Is there a way around this ?
Many thanks for all replies.
You can carry over the generic parameter over to SettingsDto:
public class SettingsDto<T> where T : struct
{
public List<Setting<T>> Settings { get; set; }
}
You must to pass T generic type to SettingsDto.
Try this:
public class Setting<T> where T: struct
{
public string Name { get; set; }
public T Value { get; set; }
}
public class SettingsDto<T> where T: struct
{
public List<Setting<T>> Settings { get; set; }
}
You can only do it if all settings have something in common. Consider using an interface like this:
public interface ISetting
{
string Name { get; set; }
}
public class Setting<T> : ISetting
where T : struct
{
public string Name { get; set; }
public T Value { get; set; }
}
public class SettingsDto
{
public List<ISetting> Settings { get; set; }
}
class Program
{
static void Main(string[] args)
{
var set=new SettingsDto();
set.Settings=new List<ISetting>();
set.Settings.Add(new Setting<int>() { Name="Setting1", Value=100 });
set.Settings.Add(new Setting<double>() { Name="Setting2", Value=Math.PI });
set.Settings.Add(new Setting<DateTime>() { Name="Setting3", Value=DateTime.Now });
set.Settings.Add(new Setting<int>() { Name="Setting4", Value=200 });
foreach(var setting in set.Settings.OfType<Setting<int>>())
{
Console.WriteLine("{0}={1}", setting.Name, setting.Value);
}
// Setting1=100
// Setting4=200
}
}
Does it has to be struct? Otherwise You could use
public List<Setting<object>> Settings { get; set; }
and cast it when needed.
If it is not for performance reasons, you could put the struct inside a class.
I have a class with many related properties that need I need to run some simple calculations on. The general structure is:
public class SpecialItem
{
public decimal Value { get; set; }
}
public class Processor
{
public decimal CalcA { get; set; }
public decimal CalcB { get; set; }
public decimal CalcC { get; set; }
public SpecialItem ItemA { get; set; }
public SpecialItem ItemB { get; set; }
public SpecialItem ItemC { get; set; }
public decimal PrevCalcA { get; set; }
public decimal PrevCalcB { get; set; }
public decimal PrevCalcC { get; set; }
public decimal DifferenceA { get; set; }
public decimal DifferenceB { get; set; }
public decimal DifferenceC { get; set; }
private void Subtract()
{//TODO}
}
I need to calculate DifferenceA = PrevCalcA - ItemA.Value - CalcA, then DifferenceB = PrevCalcB - ItemB.Value - CalcB , and so on. What is the best way to do this, ideally having a simple subtractor method (ex: private void Subtract(decimal a, decimal b, decimal c) that can sequentially calculate the Difference properties as you feed it a "list" containing groups of 3 other properties?
The reason I have all these properties is that they each indvidually bind to a ViewModel.
Thanks!
It seems to me that the simplest thing to do is redefine the DifferenceX properties like this:
public decimal DifferenceA
{
get
{
return this.PrevCalcA - this.ItemA.Value - this.CalcA;
}
}
public decimal DifferenceB
{
get
{
return this.PrevCalcB - this.ItemB.Value - this.CalcB;
}
}
public decimal DifferenceC
{
get
{
return this.PrevCalcC - this.ItemC.Value - this.CalcC;
}
}
Or am I missing something in your question?
Can anybody help me to solve this problem?
I have a base class:
public class BaseShowFilter {
public int TotalCount { get; set; }
public int FromNo { get; set; }
public int ShowCount { get; set; }
public string SortFieldName { get; set; }
public bool SortAsc { get; set; }
}
and a couple of ChildClasses from this base class. Then I have a few of other classes that store in (for example)
IEnumerable<OtherClassXXX> = ....
And I want to apply some filter to all of them using same method implemented in BaseShowFilter:
For example I need
dstList = srcList.Skip(this.FromNo-1).Take(this.ShowCount);
So I need implement in BaseShowFilter one function that will be accept in parameter IEnumerable and will return also IEnumerable
How can I write it? In pure C++ it will be simple as 1,2,3... but here I don't know how can it be done. Result may be something like this:
public class BaseShowFilter {
public int TotalCount { get; set; }
public int FromNo { get; set; }
public int ShowCount { get; set; }
public string SortFieldName { get; set; }
public bool SortAsc { get; set; }
public T FilterList<T>(T SrcList) where T :IEnumerable<> {
return srcList.Skip(this.FromNo-1).Take(this.ShowCount);
}
}
This is the usual way to do it:
public IEnumerable<T> FilterList<T>(IEnumerable<T> source)
{
return source.Skip(this.FromNo - 1).Take(this.ShowCount);
}
How can I have a property which can accept object of any type (of class)... something like this?
public class HttpConnectorRequest
{
public int Id { get; set; }
public T RequestObject { get; set; } where T: class
public string ResponseData { get; set; }
public Exception Exception { get; set; }
}
I am trying to acheive an alternative for something like this:
public class HttpConnectorRequest
{
public int Id { get; set; }
public ClassA ClassARequestObject { get; set; }
public ClassB ClassBRequestObject { get; set; }
public ClassC ClassCRequestObject { get; set; }
public string ResponseData { get; set; }
public Exception Exception { get; set; }
}
That's fine - you just need to make it generic:
public class HttpConnectorRequest<T> where T: class
{
public int Id { get; set; }
public T RequestObject { get; set; }
public string ResponseData { get; set; }
public Exception Exception { get; set; }
}
Then you'd write something like:
var request = new HttpConnectorRequest<string>();
request.RequestObject = "Hello!";
Generics is a big topic - MSDN is probably a reasonable starting point, although I suspect you'll want to read about it in a tutorial or book at some point. (While my own book, C# in Depth, obviously covers generics, plenty of others do too :)
Note that this makes the whole type generic. If you want to make just a single property generic, you're out of luck... although you could make a method generic:
public class HttpConnectorRequest
{
// Other members elided
public void SetRequestObject<T>(T value) where T : class
{
...
}
public T GetRequestObject<T>() where T : class
{
...
}
}
Quite what this would do is up to you - bear in mind that someone could write:
var request = new HttpConnectorRequest();
request.SetRequestObject<string>("Hello");
var button = request.GetRequestObject<Button>();