i would like to merge the two objects and display their sum in the new object
The first i want to achieve is i have two object that have a 100 target sales and then the result must be 200.
The second i want to achieve is the two object has array and the array consist different value.
if the two object is merge the value of the array in the new object is the value of the two objects
simple example of my class
public class IncentivePayout
{
public decimal Sales { get; set; }
public List<IncentivePayoutDetail> IncentivePayoutDetails { get; set; }
}
public class IncentivePayoutDetail
{
public string ProductFamilyName { get; set; }
}
example
totalEthical: {
target: 100,
incentivePayoutDetails: [
{
productFamilyName: X,
},
]
totalConsumer: {
target: 100,
incentivePayoutDetails: [
{
productFamilyName: Y,
},
]
the result i want to achieve is
newObject: {
target: 200,
incentivePayoutDetails: [
{
productFamilyName: X,
},
{
productFamilyName: Y,
},
]
Ok here's my solution. Pretty straightforward. Link to codeshare.
I assumed how your classes might look like. If there are any inconsistencies, try to modify it to your needs.
If codeshare doesn't work. :
class Ethical{
public int SomeValue = {get;private set;}
public List<PayoutDetails> details = {get; private set;}
public Ethical(int SomeValue){
this.SomeValue = SomeValue;
this.details = new List<PayoutDetails>();
}
public Ethical(){
this.SomeValue = 0;
this.details = new List<PayoutDetails>();
}
public void AddPayoutDetails(PayoutDetails details){
this.details.add(details);
}
public void AddPayoutDetails(List<PayoutDetails> details){
details.ForEach(el => this.details.Add(el));
}
public void AddToValue(int anyNumber){
SomeValue += anyNumber;
}
}
//for merging 2 objects
public static Ethical sumEthicalProperties(Ethical obj1, Ethical obj2){
Ethical newObj = new Ethical(obj1.SomeValue + obj2.SomeValue)
newObj.Add(obj1.details);
newObj.Add(obj2.details);
return newObj;
}
//MoreUniversal
public static Ethical sumEthicalProperties(params Ethical[] objs){
int fullSum = 0;
Ethical newObj = new Ethical()
foreach(Ethical singleObj in objs){
fullSum += singleObj.SomeValue;
newObj.AddPayoutDetails = singleObj.details;
}
newObj.AddToValue(fullSum;
return newObj;
}
Related
I am looking for information about that in the internet but with no success. The goal is to realize a sort of dataset of 10 subject (sub_1, sub_2... sub_10), each of them has done 3 kind of activities (walk, run, jump) for three time each (trial_1... trial_3) with relative scores. I would like to access these information like:
variable = dataset.sub_1.jump.trial_2.score;
or, at least:
variable = dataset.sub[0].task[2].trial[1].score;
So, the structure would be a tree structure. Until now I only realized a structure with "parallel fields":
struct dataset
{
public string[] sub; // 1 to 10 subjects
public string[] task; // 1 to 3 tasks
public string[] trial; // 1 to 3 trials
public int score; // the score of the above combination
}
Any idea?
This problem can be solved in many ways.
My solution has one drawback, there is no check if user exceeded Score arrays capacity.
I guess database tag has nothing to do with this question
using System;
using System.Linq;
namespace ConsoleApp
{
public abstract class Task
{
public string Name { get; set; }
public int TotalScore { get { return Score.Sum(); } }
public int[] Score { get; set; } = new int[3];
}
public class Walk : Task { }
public class Run : Task { }
public class Jump : Task { }
public class Subject
{
public Walk Walk { get; set; } = new();
public Run Run { get; set; } = new();
public Jump Jump { get; set; } = new();
public int TotalScore { get { return Walk.TotalScore + Run.TotalScore + Jump.TotalScore; }}
}
class Program
{
static void Main(string[] args)
{
var subject = new Subject();
// Adding score to specific trials
subject.Run.Score[0] = 50;
subject.Run.Score[1] = 40;
subject.Run.Score[2] = 60;
subject.Jump.Score[0] = 40;
subject.Jump.Score[1] = 80;
subject.Jump.Score[2] = 100;
// Output score of 1. trial for Walk task
Console.WriteLine(subject.Walk.Score[0]);
// Output total score as a sum of all trials for Jump task
Console.WriteLine(subject.Jump.TotalScore);
// Output total score as a sum of all trials in all tasks
Console.WriteLine(subject.TotalScore);
// ERROR CASE: this will be exception
subject.Jump.Score[3] = 100;
}
}
}
using System;
using System.Collections.Generic;
namespace ConsoleApp
{
public class Trial
{
public Trial(int score)
{
Score = score;
}
public int Score { get; set; }
}
public class Task
{
public List<Trial> Trials { get; } = new List<Trial>();
}
public class Subject
{
public Dictionary<string, Task> Tasks { get; } = new Dictionary<string, Task>();
public Subject()
{
Tasks.Add("walk", new Task());
Tasks.Add("run", new Task());
Tasks.Add("jump", new Task());
}
}
class Program
{
static void Main(string[] args)
{
Subject player1 = new Subject();
player1.Tasks["run"].Trials.Add(new Trial(score: 3));
Console.WriteLine(player1.Tasks["run"].Trials[0].Score);
}
}
}
Maybe a class for everything is too much, but maybe you want to add a description property for tasks one day or a timestamp for the trial. Then it's ok.
public class Subject
{
private Dictionary<string,Activity> _activities { get; }= new Dictionary<string, Activity>();
public Activity this[string activity]
{
get
{
if (!_activities.Keys.Contains(activity))
_activities[activity] = new Activity();
return _activities[activity];
}
set
{
_activities[activity] = value;
}
}
public int Score => _activities.Values.Sum(x => x.Score);
}
public class Activity
{
private Dictionary<int, Trial> _trials { get; } = new Dictionary<int, Trial>();
public Trial this[int trial]
{
get
{
if (!_trials.Keys.Contains(trial))
_trials[trial] = new Trial();
return _trials[trial];
}
set
{
_trials[trial] = value;
}
}
public int Score => _trials.Values.Sum(x => x.Score);
}
public class Trial
{
public int Score { get; set; }
}
public class Answer
{
public void DoSomething()
{
Subject Mindy = new Subject();
Mindy["curling"][1].Score = 5;
Mindy["bowling"][1].Score = 290;
Console.WriteLine(Mindy.Score);
}
}
This is what I would guess you think you need... but from your question I think you're still new to C# and might want to rethink your concept. It looks like a very database-oriented way of looking at the problem, so maybe you might want to take a look at dapper to more closely match your database.
Also, avoid using the classname Task, this can imo only cause confusion if you ever start using multithreading (System.Threading.Task is a .NET framework component)
I have a list of source classes derived from a base source class and a list with destination classes derived from a base destination class. There is only one destination class corresponding to a source class. I want to make a generic converter which updates an existing destination object, given his corresponding source object.
Updated completely:
I have the following code:
using System;
namespace ConsoleApp1
{
internal class Program
{
private static void Main(string[] args)
{
S1 newItem = new S1()
{
Age = 11,
Name = "John"
};
D1 oldItem = new D1()
{
Age = 10
};
//there is an item in a database which is of D1 type. This convertor receives an object S1 in order to update the D1 item.
// the rule is that Sx updatates Dx (where x is 1,2,3,4,5...)
Convertor<S1, D1> convertor = new Convertor<S1, D1>(newItem, oldItem);
S2 newItem2 = new S2()
{
City = "London",
Name = "Lynda"
};
D2 oldItem2 = new D2()
{
City = "Paris"
};
Convertor<S2, D2> convertor2 = new Convertor<S2, D2>(newItem2, oldItem2);
Console.ReadKey();
}
}
public abstract class SourceDomain
{
public string Name { get; set; }
internal abstract void SetItem<Y>(Y oldItem) where Y : DestinationDomain;
}
public class S1 : SourceDomain
{
public int Age { get; set; }
internal override void SetItem<Y>(Y oldItem)
{
Console.WriteLine("here I want to update the age of oldItem (10) with the new value (11)");
//oldItem.Age = Age;
}
}
public class S2 : SourceDomain
{
public string City { get; set; }
internal override void SetItem<Y>(Y oldItem)
{
Console.WriteLine("here I want to update the city of oldItem Paris with the new value London");
// oldItem.City = City;
}
}
public class DestinationDomain { }
public class D1 : DestinationDomain
{
public int Age { get; set; }
}
public class D2 : DestinationDomain
{
public string City { get; set; }
}
public class Convertor<X, Y> where X : SourceDomain where Y : DestinationDomain
{
protected X item;
protected Y oldItem;
public Convertor(X newObject, Y oldObject)
{
item = newObject;
oldItem = oldObject;
//here I want to call, depending of item type, the proper method, not the base one.
item.SetItem(oldItem);
}
}
}
SourceDomain and DestinationDomain are base classes and there are a lot of derived classes for each of them S1, S2, D1, D2, etc.
This converter receives two classes as types and two objects of that classes and tries to update the destination item of type Y with source item of type X.
In the above example, I want to change update the age of the D1 oldItem variable with 11, and the City oldItem2 with the "London" value, but I cannot access that properties in SetItem method.
This needs to be done with a combination of inheritance and generics, and we'll make the two work together with generic type constraints.
Here are the requirements as I understand them:
You've got some data you want to copy from Source to Destination; one kind of data has an Age, one has a City, maybe another has a Poodle or a SwimmingPool. We'll define an interface that says: "This is a data object which can copy its own properties from another object of the same type", and we'll put all our data in little classes which know how to copy themselves. The "I can copy things like me" interface doesn't know what properties will be copied; it just requires the concrete class to implement a method that knows those details internally. As far as this example goes it could be a base class, but my assumption is the “copyability” isn’t the core indentity of these objects. It’s not what they represent; it’s just one thing we need to be able to do with them along the way.
The other things we need are a "source thing", and a "destination thing". A source thing just needs to provide a data object that can be copied. A destination thing just needs to receive a data object that can be copied.
You confused yourself by trying to mix up the sources and destinations with the data items. If you're going crazy trying to make your class do two contradictory things at once, try breaking it up into two classes that each does one thing.
You never do anything with your SourceDomain Name property, so I won't either. If you need to, I'll leave that as an exercise.
public interface ICopyable<T>
{
void CopyFrom(T other);
}
internal class Program
{
private static void Main(string[] args)
{
var newItem = new SourceDomain<Person>
{
Item = new Person { Age = 11 },
Name = "John"
};
var oldItem = new DestinationDomain<Person>
{
Item = new Person { Age = 10 }
};
//there is an item in a database which is of D1 type. This convertor receives an object S1 in order to update the D1 item.
// the rule is that Sx updatates Dx (where x is 1,2,3,4,5...)
Convertor<Person> convertor = new Convertor<Person>(newItem, oldItem);
var newItem2 = new SourceDomain<Location>()
{
Item = new Location { City = "London" },
Name = "Lynda"
};
var oldItem2 = new DestinationDomain<Location>()
{
Item = new Location { City = "Paris" }
};
Convertor<Location> convertor2 = new Convertor<Location>(newItem2, oldItem2);
Console.ReadKey();
}
}
public class SourceDomain<T>
{
public string Name { get; set; }
public T Item { get; set; }
}
public class DestinationDomain<T> where T : ICopyable<T>, new()
{
public string Name { get; set; }
public T Item { get; set; }
public void CopyItemFrom(T other)
{
if (Item == null)
{
Item = new T();
}
Item.CopyFrom(other);
}
}
// A person is a thing which can turn itself into a copy of another Person.
// You could define a class Wombat : ICopyable<Locomotive>, if you wanted to be
// able to convert Locomotives to Wombats. You'd just add another CopyFrom()
// overload, public void CopyFrom(Locomotive other).
public class Person : ICopyable<Person>
{
public int Age { get; set; }
public void CopyFrom(Person other)
{
Age = other.Age;
}
}
public class Location : ICopyable<Location>
{
public String City { get; set; }
public void CopyFrom(Location other)
{
City = other.City;
}
}
public class Convertor<X> where X : ICopyable<X>, new()
{
protected SourceDomain<X> item;
protected DestinationDomain<X> oldItem;
public Convertor(SourceDomain<X> newObject, DestinationDomain<X> oldObject)
{
item = newObject;
oldItem = oldObject;
//here I want to call, depending of item type, the proper method, not the base one.
//newObject.Data = oldItem.Data;
oldItem.CopyItemFrom(item.Item);
}
}
Homework:
Modify this code so DestinationDomain.CopyItemFrom() receives the source itself, not the source's item.
I think it's better the way I wrote it. Think of reasons why I might have thought that.
Seriously, find out what ref does, and never again use a keyword in your code if you are only guessing about what it might mean. Don't just throw code at the wall, hoping it sticks. You'll get yourself in a lot of frustrating trouble that way.
Finally i have done this:
namespace ConsoleApp1
{
internal class Program
{
private static void Main(string[] args)
{
S1 newItem = new S1()
{
Age = 11,
Name = "John"
};
D1 oldItem = new D1()
{
Age = 10
};
//there is an item in a database which is of D1 type. This convertor receives an object S1 in order to update the D1 item.
// the rule is that Sx updatates Dx (where x is 1,2,3,4,5...)
Convertor<S1, D1> convertor = new Convertor<S1, D1>(newItem, oldItem);
S2 newItem2 = new S2()
{
City = "London",
Name = "Lynda"
};
D2 oldItem2 = new D2()
{
City = "Paris"
};
Convertor<S2, D2> convertor2 = new Convertor<S2, D2>(newItem2, oldItem2);
Console.ReadKey();
}
}
public interface ICopyable<T>
{
void CopyFrom(T other);
}
public abstract class SourceDomain
{
public string Name { get; set; }
}
public class S1 : SourceDomain
{
public int Age { get; set; }
}
public class S2 : SourceDomain
{
public string City { get; set; }
}
public class DestinationDomain { }
public class D1 : DestinationDomain, ICopyable<S1>
{
public int Age { get; set; }
public void CopyFrom(S1 other)
{
Console.WriteLine("oldItem.Age " + Age + " new Age; = " + other.Age);
Age = other.Age;
Console.WriteLine("oldItem.Age " + Age + " new Age; = " + other.Age);
}
}
public class D2 : DestinationDomain, ICopyable<S2>
{
public string City { get; set; }
public void CopyFrom(S2 other)
{
City = other.City;
Console.WriteLine(" oldItem.City = City;");
}
}
public class Convertor<X, Y> where X : SourceDomain where Y : DestinationDomain, ICopyable<X>
{
protected X item;
protected Y oldItem;
public Convertor(X newObject, Y oldObject)
{
item = newObject;
oldItem = oldObject;
//here I want to call, depending of X type, the proper method, not the base one.
oldItem.CopyFrom(item);
Console.WriteLine(item);
}
}
}
I am trying to sort list with clubs which have best result, I must use Sort Method, but it shows error, what I am doing wrong. I know it is a problem of Sort method but can't find a mistake, I made it work with lambda expression but I want to do it with sort method;
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// class with objects
Club barca = new Club("Barcelona", 1900, 100, 20);
Club Real = new Club("Real", 1910, 80, 70);
Club Manchester = new Club("Manchester", 1890, 75, 55);
Club Milan = new Club("Milan", 1880, 45, 65);
//new list of clubs
var myclublist = new List<IClub>();
///add clubs in list
myclublist.Add(barca);
myclublist.Add(Real);
myclublist.Add(Manchester);
myclublist.Add(Milan);
// sort method for list
myclublist.Sort();
//show clubs name with best results
foreach (var item in myclublist)
{
if (item.IsPositiveBallRatio() == true)
{
Console.WriteLine(item.ClubName());
}
}
}
// club class
public class Club : IClub, IComparable<Club>
{
public string Name { get; set; }
public int Year { get; set; }
public int Scoredgoals { get; set; }
public int Lossgoals { get; set; }
public Club(string name, int year, int scoredgoals, int lossgoals)
{
Name = name;
Year = year;
Scoredgoals = scoredgoals;
Lossgoals = lossgoals;
}
public int BallRatio()
{
int ratio;
ratio = Scoredgoals - Lossgoals;
return ratio;
}
public bool IsPositiveBallRatio()
{
if (Scoredgoals > Lossgoals)
{
return true;
}
else
return false;
}
public string ClubName()
{
string n;
n = Name;
return n;
}
public int CompareTo(Club other)
{
return BallRatio().CompareTo(other.BallRatio());
}
}
// inferface for club class
interface IClub
{
int BallRatio();
bool IsPositiveBallRatio();
string ClubName();
}
}
what I am doing wrong?
Why: IClub is not comparable to itself and there is no other information about the type available to the code at run-time for generic method. So it falls back to non-generic version of IComparable which is not implemented by your Club type.
Fixes:
either use list of Club instead of List<IClub> as Club is comparable to itself
implement non-generic IComparable on the Club:
public class Club : IClub, IComparable<Club> , IComparable
{
...
public int CompareTo(object obj)
{
return CompareTo(obj as Club);
}
}
make type you have in the list (IClub) to be comparable to itself - IClub : IComparable<IClub> to fix the issue if you really expect mixed IClub implementations in the array:
public class Club : IClub, IComparable<Club>
{
...
public int CompareTo(IClub other)
{
return CompareTo(other as Club);
}
}
public interface IClub : IComparable<IClub> {...}
See List.Sort for details.
Note: CompareTo in this post are sample-only and you need to add all type/null checks for them to work in real code.
is there a way to put these into either a 1 D array or a 2 D array. ? i have produced code and it looks a bit untidy as well as long can this be shortened?
double worstPrice = 6.47;
double bestPrice = 0.99;
double CivetCatPrice =29.14;
double whenPrice = 10.50;
double everythingPrice = 319.56;
int bestStock = 3238;
int worstStock = 8;
int civetCatstock = 3;
int whenStock = 37;
int everythingStock = 2;
You can make an array for each doubles and ints like this
double[] priceData = new double[]{ 6.47, 0.99, 29.14, 10.50, 319.56 };
int[] stockData = new int[]{ 3238, 8, 3, 37, 2 };
Alternatively you can use a dictionary if you wish for them to keep their names
Dictionary<string, double> priceDict = new Dictionary<string, double>();
priceDict.Add("worstPrice", 6.47);
//And so on for each double
Dictionary<string, int> stockDict = new Dictionary<string, int>();
priceDict.Add("bestStock", 3238);
//And so on for each int
The values in these can be called like so
double worstMinusBestPrices = priceData[0] - priceData[1]; //For arrays
double worstMinusBestPrices = priceDict["worstPrice"] - priceDict["bestPrice"] //For dictionaries
You could implement a custom class which holds these values as proprties with meaningful names. Then your code will be much more readable, maintainable and robust.
For example (you don't need all of these classes, it should just give you an idea):
public abstract class Animal
{
public Animal(string animalName)
{
this.Name = animalName;
}
//insert properties and methods which all aimals share here
public string Name { get; set; }
}
public class CibetCat : Animal
{
public CibetCat() : base("CibetCat")
{
}
//insert properties and methods which all CibetCats share here
}
Now your class that holds the price and stock informations as well as the reference to the animal itself(CibetCat in your example):
public class AnimalStock // or AnimalPrice or whatever
{
public AnimalStock(Animal animal)
{
this.Animal = animal;
}
public AnimalStock(Animal animal, decimal worstPrice, decimal bestPrice, int bestStock, int worstStock)
{
this.Animal = animal;
this.Worstprice = worstPrice;
this.BestPrice = bestPrice;
this.BestStock = bestStock;
this.WorstStock = worstStock;
}
public Animal Animal { get; set; }
public decimal Worstprice { get; set; }
public decimal BestPrice { get; set; }
public int BestStock { get; set; }
public int WorstStock { get; set; }
// ...
}
Lot of code but not complex. Now You can write this simple and readable code:
Animal cibetCat = new CibetCat();
AnimalStock stock = new AnimalStock(cibetCat);
stock.BestPrice = 0.99m;
stock.Worstprice = 6.47m;
stock.BestStock = 3238;
// ...
Later you can access all these properties(or it's methods) from a single instance.
Console.WriteLine("Animal's best-price is: {0}", stock.BestPrice); // etc
As Alfie pointed out, you could use a dictionary - but you're then referencing things by a string identifier, that you have to remember.
Another way would be to use a class or struct. There are of course many ways to do this, but some include:
public class Things
{
public double worstPrice = 6.47;
public double bestPrice = 0.99;
public double CivetCatPrice =29.14;
public double whenPrice = 10.50;
public double everythingPrice = 319.56;
public int bestStock = 3238;
public int worstStock = 8;
public int civetCatstock = 3;
public int whenStock = 37;
public int everythingStock = 2;
}
Another way would be:
public class Things
{
public double WorstPrice { get; readonly set; }
public double BestPrice = { get; readonly set; }
// etc
public Things(double worstPrice, double bestPrice) // etc
{
WorstPrice = worstPrice;
BestPrice = bestPrice;
}
}
There are pros and cons to both approaches. Another potential is to use a collection of a class/struct to group things and aggregate them in meaningful ways.
Like:
public class Thing
{
public string ThingLabel { get; readonly set; }
public double ThingPrice { get; readonly set; }
public int ThingQuantity { get; readonly set; }
// the value of your stock, calculated automatically based on other properties
public double ThingValue { get { ThingPrice * ThingQuantity; } }
public Thing(string thingLabel, double thingPrice, int thingQuantity)
{
ThingLabel = thingLabel;
// etc
}
}
public void DoStuff()
{
List<Thing> list = new List<Thing>();
Thing thing = new Thing("Civet cat", 500, 10);
list.Add(thing);
list.Add(new Thing("Sea flap flap", 100, 5);
list.Add(new Thing("Nope Rope", 25, 4);
Console.WriteLine("The value of {0}'s stock is: {1}", thing.ThingLabel, thing.Value);
}
and yet another way is to use a base class and create sub classes of your different types. The possibilities are nearly endless! You just have to decide which way works best for you now, you later, and your potential team.
I'm trying to create a class that has fields in it that are of an anonymous type. (This is for Json deserialization.)
I can't find a syntax that the compiler will accept. I'm trying:
class Foo {
var Bar = new {
int num;
}
var Baz = new {
int[] values;
}
}
This is supposed to represent this example Json object:
{
"Bar": { "num": 0 }
"Baz": { "values": [0, 1, 2] }
}
Is this even possible, or must I declare each class normally with a full class identifier?
You can declare a field using an anonymous type initializer... you can't use implicit typing (var). So this works:
using System;
class Test
{
static object x = new { Name = "jon" };
public static void Main(string[] args)
{
Console.WriteLine(x);
}
}
... but you can't change the type of x to var.
Yes it is possible, here is EXAMPLE
var Bar = new {num = 0};
var Baz = new {values = new List<int>()};
var Foo = new {Bar, Baz};
Console.WriteLine(JsonConvert.SerializeObject(Foo));
Of Course you can type it in one line
var Foo = {Bar = new {num = 0}, Baz = new {values = new List<int>()}};
Edit updated .Net fiddle with using Foo as class
No, this is not possible. The most straightforward way to do this is to simply create classes like you said. This is what I'd recommend.
void Main()
{
Console.WriteLine(JsonConvert.SerializeObject(new Foo { Bar = new Bar {
num = 0
},
Baz = new Baz { values = new[] { 0, 1, 2 } }
})); // {"Bar":{"num":0},"Baz":{"values":[0,1,2]}}
}
public class Foo {
public Bar Bar { get; set; }
public Baz Baz { get; set; }
}
public class Bar {
public int num { get; set; }
}
public class Baz {
public int[] values { get; set; }
}
Another approach, which loses static type checking, is typing it as object or dynamic:
void Main()
{
JsonConvert.SerializeObject(new Foo { Bar = new {
num = 0
},
Baz = new { values = new[] { 0, 1, 2 } }
}); // {"Bar":{"num":0},"Baz":{"values":[0,1,2]}}
}
class Foo {
public object Bar { get; set; }
public object Baz { get; set; }
}
It would probably be possible to write a custom JsonConverter to serialize a class like this as you wish (since each anonymous type in your example only has one real value inside it; if your real types are more complex, this won't work for those).
[JsonConverter(typeof(MyFooConverter))]
class Foo {
public int Bar { get; set; }
public int[] Baz { get; set; }
}