I have a class with 2 strings and 1 double (amount).
class Donator
string name
string comment
double amount
Now I have a Array of Donators filled.
How I can sort by Amount?
If you implement IComparable<Donator> You can do it like this:
public class Donator :IComparable<Donator>
{
public string name { get; set; }
public string comment { get; set; }
public double amount { get; set; }
public int CompareTo(Donator other)
{
return amount.CompareTo(other.amount);
}
}
You can then call sort on whatever you want, say:
var donors = new List<Donator>();
//add donors
donors.Sort();
The .Sort() calls the CompareTo() method you implemented for sorting.
There's also the lambda alternative without IComparable<T>:
var donors = new List<Donator>();
//add donors
donors.Sort((a, b) => a.amount.CompareTo(b.amount));
You can also use delegates:
class Program
{
static void Main(string[] args)
{
List<Donor> myDonors = new List<Donor>();
// add stuff to your myDonors list...
myDonors.Sort(delegate(Donor x, Donor y) { return x.amount.CompareTo(y.amount); });
}
}
class Donor
{
public string name;
public string comment;
public double amount;
}
By implementing IComparable and then use Array.Sort.
public class Donator : IComparable {
public string name;
public string comment;
public double amount;
public int CompareTo(object obj) {
// throws invalid cast exception if not of type Donator
Donator otherDonator = (Donator) obj;
return this.amount.CompareTo(otherDonator.amount);
}
}
Donator[] donators; // this is your array
Array.Sort(donators); // after this donators is sorted
I always use the list generic, for example
List<Donator> MyList;
then I call MyList.Sort
MyList.Sort(delegate (Donator a, Donator b) {
if (a.Amount < b.Amount) return -1;
else if (a.Amount > b.Amount) return 1;
else return 0; );
You could use MyArray.OrderBy(n => n.Amount)
providing you have included the System.Linq namespace.
Here is a sort without having to implement an Interface. This is using a Generic List
List<Donator> list = new List<Donator>();
Donator don = new Donator("first", "works", 98.0);
list.Add(don);
don = new Donator("first", "works", 100.0);
list.Add(don);
don = new Donator("middle", "Yay", 101.1);
list.Add(don);
don = new Donator("last", "Last one", 99.9);
list.Add(don);
list.Sort(delegate(Donator d1, Donator d2){ return d1.amount.CompareTo(d2.amount); });
Another way is to create a class that implements IComparer, then there is an overload to pass in the Comparer class.
http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx
This way you could have different classes for each specific sort needed. You could create one to sort by name, amount, or others.
Related
I want to index class properties like an array.
Public class Foo
{
propery p1{get;set;}
propery p3{get;set;}
propery p3{get;set;}
.
.
.
.
}
I wan to index every propery like an array
FOO.p1=Value
Foo[0]=Value(index 0 refers to p1)
I don't know much about the database, where there might have been a ready-made solution. But at least you can do it by reflection in this way:
using System.Reflection;
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class IndexedPropertyAttribute : Attribute
{
readonly int index;
public IndexedPropertyAttribute(int index)
{
this.index = index;
}
public int Index
{
get { return index; }
}
}
public abstract class WithIndexedProperties
{
private Lazy<IReadOnlyDictionary<int, PropertyInfo>> properties;
protected WithIndexedProperties()
{
properties = new Lazy<IReadOnlyDictionary<int, PropertyInfo>>(
() => {
var linq = from prop in this.GetType().GetProperties()
let attr = prop.GetCustomAttributes(typeof(IndexedPropertyAttribute), true)
where attr.Length is 1
select (((IndexedPropertyAttribute)attr[0]).Index, prop);
return linq.ToDictionary(p => p.Index, p => p.prop);
});
}
public object? this[int propertyIndex]
{
get
{
return properties.Value[propertyIndex].GetValue(this);
}
set
{
properties.Value[propertyIndex].SetValue(this, value);
}
}
}
And there is an example:
Clss obj = new Clss();
obj[0] = "ABC";
obj[2] = 222;
obj[4] = 444;
// Here obj.A will be "ABC", obj.B will be 444 and obj.C will be 222.
public class Clss : WithIndexedProperties
{
[IndexedProperty(0)]
public string? A { get; init; }
[IndexedProperty(4)]
public int B { get; init; }
[IndexedProperty(2)]
public int C { get; init; }
}
I think you need to do something like this. The code below is very generalized solution to your question and I might need some customization for yourself
using System.Reflection;
public class ReflectionBasedIndexedType
{
public int A1 { get; set; } = 10;
public int A2 { get; set; } = 20;
public string SomeString => "Hello There";
private readonly Dictionary<string, object> _underlyingCollection;
public object this[string name] => _underlyingCollection[name];
public ReflectionBasedIndexedType()
{
_underlyingCollection = GetUnderlyingCollection();
}
private Dictionary<string, object> GetUnderlyingCollection()
{
Dictionary<string, object> container = new();
// get the properties defined in the class, I am filtering
// with constraint that, I want get only public and class level
// Properties, which means I won't get any private/protected or
// static properties if there is defined such in the class
// also where filters out indexer property, which we have defined
// inside this class, without this line, there will be exception
IEnumerable<PropertyInfo> properties = GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Where(x => x.GetIndexParameters().Length == 0);
foreach (PropertyInfo property in properties)
{
container.Add(property.Name, property.GetValue(this)!);
}
return container;
}
}
and than use case will be like this
ReflectionBasedIndexedType rbit = new();
var a1 = rbit[nameof(rbit.A1)];
var a2 = rbit[nameof(rbit.A2)];
var str = rbit[nameof(rbit.SomeString)];
Console.WriteLine(a1);
Console.WriteLine(a2);
Console.WriteLine(str);
and output in the console will be this
10
20
Hello There
I think you have two ways at least.
The first one, is #Swapper mentioned, yo can use reflection. In this way, the class is normal and you have to write your hug code in where you want to use that class.
The second way is easier but a little fuzy. You can use dynamic type. If you know how to use it, that's ok. Otherwise please let me know, then I will create a sample code for you.
I have two classes one called warehouse and one called Warehouselocations.
The wareHouse is currently able to create,store and find boxes in warehouselocation.
But now i also need the warehouse to be able to create a cloned version of wareHouseLocation with all the stored information.
locations = new List<WareHouseLocation>();
This is the list where i store all the information. I want to be able to copy it.
I tried to find the answer my self and even tried some code but so far i had got nothing that works properly.
public WareHouseLocation DeepCopy()
{
foreach (WareHouseLocation wareHouseLocation in locations)
{
if(wareHouseLocation == null)
{
return null;
}
else
{
//Need code here
}
}
return null;
}
The code is currently in the wareHouse class.
I be happy for anything that could help me.
public class WareHouseLocation
{
public int FloorID { get; set; }
public List<I3DStorageObject> storage = new List<I3DStorageObject>();
public double MaxVolume;
public double MaxWeight;
public WareHouseLocation(double height, double width, double depth)
{
MaxVolume = height * width * depth;
MaxWeight = 1000;
}
public bool hasAvailableVolumeForObject(I3DStorageObject s)
{
double currentVolume = 0;
foreach (I3DStorageObject obj in storage)
{
currentVolume += obj.Volume;
}
double available = MaxVolume - currentVolume;
if (s.Volume <= available)
{
return true;
}
else
{
return false;
}
}
}
Here is the code for the WareHouseLocation.
You can achieve it by implementing ICloneable interface:
public class WareHouseLocation : ICloneable
{
public int FloorID { get; set; }
public List<I3DStorageObject> storage = new List<I3DStorageObject>();
public double MaxVolume;
public double MaxWeight;
//rest of code
public object Clone()
{
var copy = (WareHouseLocation)MemberwiseClone();
copy.storage = storage.Select(item => (I3DStorageObject)item.Clone()).ToList();
return copy;
}
}
Since you have a List reference type inside WareHouseLocation, you'll need to properly clone this as well by implementing ICloneable for I3DStorageObject as well, because MemberwiseClone copy the reference only, not the referred object itself
public class I3DStorageObject : ICloneable
{
public double Volume { get; set; }
public object Clone()
{
return MemberwiseClone();
}
}
You can also have a look at MemberwiseClone for details and examples of deep/shallow copy of objects
I think you can use JsonConvert.SerializeObject and JsonConvert.DeserializeObject for copy,
var json = JsonConvert.SerializeObject(put_your_object_here);
var copy = JsonConvert.DeserializeObject<your_object_type>(json);
In a method
count is calculated
a list is also formed
I need to return this count along with the list in c#. Any suggestions on how to return this?
You should avoid using out and ref parameters.
I suggest creating a type that represents the output result.
public DoSomethingResult DoSomething()
{
var result = new DoSomethingResult();
//....
result.Data = GenerateList();
result.Count = CalculateCount();
return result;
}
public class DoSomethingResult
{
public List<YourType> Data { get; set; }
public int Count { get; set; }
}
FxCop rule for out parametes.
You should use the out keyword in C#.
MSDN description. "Declaring an out method is useful when you want a method to return multiple values. This enables methods to return values optionally."
Here is the MSDN documentation.
Here is an example:
public List<YourType> SomeMethodName(out int count)
{
//Your calculation here
}
You could simply return a List and a get a Count in caller method. You don't really require to return both.
Still interested? multiple options.
Using out parameter for count.
public List<something> DoSomething(out int count)
{
....
count = list.Count();
return list;
}
using Tuple as pointed in comments.
public Tuple<List<something>, int> DoSomething()
{
....
return new Tuple<List<something>, int>(list1, count);
}
2 options
using out param for count, with return type as List
using a Tuple or custom Class which will contain both of your Count and List
Sample Code:
public List<Class1> GetData(out int Count)
{
//...
//Your Logic with returning list
}
Option2:
public CustomClass DoSomething()
{
var data = new CustomClass();
//....
data.Data = list;
data.Count = list.Count();
return data;
}
public class CustomClass
{
public List<Class1> Data { get; set; }
public int Count { get; set; }
}
I have a HashSet. Is there a method that can utilize the IEqualityComparer for retrieving items where you pass in an object that will satisfies the equals method defined in the IEqualityComparer?
This might explain it a bit more.
public class Program
{
public static void Main()
{
HashSet<Class1> set = new HashSet<Class1>(new Class1Comparer());
set.Add( new Class1() { MyProperty1PK = 1, MyProperty2 = 1});
set.Add( new Class1() { MyProperty1PK = 2, MyProperty2 = 2});
if (set.Contains(new Class1() { MyProperty1PK = 1 }))
Console.WriteLine("Contains the object");
//is there a better way of doing this, using the comparer?
// it clearly needs to use the comparer to determine if it's in the hash set.
Class1 variable = set.Where(e => e.MyProperty1PK == 1).FirstOrDefault();
if(variable != null)
Console.WriteLine("Contains the object");
}
}
class Class1
{
public int MyProperty1PK { get; set; }
public int MyProperty2 { get; set; }
}
class Class1Comparer : IEqualityComparer<Class1>
{
public bool Equals(Class1 x, Class1 y)
{
return x.MyProperty1PK == y.MyProperty1PK;
}
public int GetHashCode(Class1 obj)
{
return obj.MyProperty1PK;
}
}
If you want to retrieve items based on a single property, you might want to use a Dictionary<T,U> instead of a hashset. You can then place the items within the dictionary, using MyProperty1PK as the key.
Your query then becomes simple:
Class1 variable;
if (!dictionary.TryGetValue(1, out variable)
{
// class wasn't in dictionary
}
Given that you're already storing using a comparer which only uses this value as the uniqueness criteria, there is really no disadvantage to just using that property as the key in a dictionary instead.
I have set of properties as follows:
public string Foo1 {set;get;}
public string Foo2 {set;get;}
public string Foo3 {set;get;}
public string Foo4 {set;get;}
public string Foo5 {set;get;}
public string Foo6 {set;get;}
public string Foo7 {set;get;}
public string Foo8 {set;get;}
public string Foo9 {set;get;}
......
public string Foo50 {set;get;}
then i m iterating through a collection as follows:
foreach(var element in sortedCollection.Keys){
if(element != null)
// in this block I would like to assign the element to the properties above
// ex:
foo?? = sortedCollection[element];
// ?? need to be replaced by index.
}
Is there an easy way to do this?
I think a better design would be:
public List<string> Foos { get; private set; }
If you can't change it, you could probably do something like:
var type = typeof(MyCalss);
int index = 1;
foreach (var key in sortedCollection.Keys)
{
var value = sortedCollection[key];
var prop = type.GetProperty("Foo" + index);
if (prop != null) prop.SetValue(this, value, null);
index++;
}
... of course with some error handling, and where this assumes this is a method within your class. Can you determine an index based on the values in your sortedCollection?
you can use reflection and do it in a loop:
for ( int i = 1 ; i < 51 ; i++)
this.GetType().GetProperty(string.Format("Foo{0}",i)).SetValue(this,desiredValue,null);
but i think i'll recommend using indexers
http://msdn.microsoft.com/en-us/library/2549tw02%28v=vs.80%29.aspx
You can do what you want by:
Using a for loop instead of foreach. This way you can operate with the current index.
Using reflections. You can get a list of properties for your class and access them dynamically. For example, see Type.GetProperties.
But why don't you just use a List<string> Foos instead of lots of properties?
You should use reflection.
this.GetType().GetProperty("Foo" + i).SetValue(this, sortedCollection[element], null);
Two things though:
GetProperty's cost is not null. So if you're doing this a lot of times, you might want to store the result of GetProperty in some field and then use this field in your foreach.
If your properties are really named Something1, Something2, etc..., then you may have a design flaw you may want to correct before doing this (replace all you string members with one List).
You need to use reflection (Type.GetProperty()) to get the property and set it's value.
Assuming that the properties are defined in class called MyClass:
foreach(var element in sortedCollection.Keys){
if(element != null)
// in this block I would like to assign the element to the properties above
// ex:
//foo?? = sortedCollection[element];
// not sure how you are getting the index here, may be you need to use for loop
PropertyInfo pi = typeof(MyClass).GetProperty("Foo" + index);
// ?? need to be replaced by index.
if (pi != null)
{
pi.SetValue(<object of MyClass>, sortedCollection[element], null);
}
}
void Main()
{
var foo = new Foo();
foo[1] = "Foo1";
//foo.Dump();
}
public class Foo
{
public string Foo1 {set;get;}
public string Foo2 {set;get;}
public string Foo3 {set;get;}
public string Foo4 {set;get;}
public string Foo5 {set;get;}
public string Foo6 {set;get;}
public string Foo7 {set;get;}
public string Foo8 {set;get;}
public string Foo9 {set;get;}
public string this[int index]
{
get
{
return getPropertyValue(index);
}
set
{
setPropertyValue(index, value);
}
}
private void setPropertyValue(int i, string value)
{
var propi = this.GetType().GetProperty("Foo" + i);
if (propi != null)
propi.SetValue(this,value,null);
}
private string getPropertyValue(int i)
{
var propi = this.GetType().GetProperty("Foo" + i);
if (propi != null)
return (string)propi.GetValue(this, null);
return null;
}
}
I would actually use reflection, or if this is called a lot, make a dynamic method and ILEmit to do it (much faster at runtime than reflection).
However just to suggest something different, you could change the class containing the Foo* properties to have each getter/setter read from an indexed list:
public class FooOfDoom
{
public string[] Foos = new string[2];
public string Foo1
{
set { Foos[0] = value; }
get { return Foos[0]; }
}
public string Foo2
{
set { Foos[1] = value; }
get { return Foos[1]; }
}
}
Then your class doesn't really change, as far as its contract with all the other code, since the properties are still there, but now you can assign right to Foos instead of through each individual property.
Again, in reality I would actually use a DynamicMethod if I was doing it myself.
Personally, I disagree with most of the other posters here. I think the use of reflection should be limited to those situations where it is really called for (object inspection, certain GUI situations, etc). In this case, with just a little more typing, it is possible to write a strongly-typed program and still do what you want. I'll offer two alternatives. Both alternatives will offer the ability to access your properties by name as well as by index.
In the first alternative, I'll assume that we are allowed to change the definition of your properties. In the second alternative, I'll assume that those definitions must remain unchanged.
The first alternative moves the data to a separate array, adds helper methods to access the data by index, and alters the properties to use the helper methods:
private class Version1 {
private readonly string[] underlyingData=new string[50];
public string Foo1 { get { return ReadFoo(1); } set { SetFoo(1, value); } }
public string Foo2 { get { return ReadFoo(2); } set { SetFoo(2, value); } }
public string Foo3 { get { return ReadFoo(3); } set { SetFoo(3, value); } }
//......
public string Foo50 { get { return ReadFoo(50); } set { SetFoo(50, value); } }
private string ReadFoo(int index) {
return underlyingData[index-1]; //1-based indexing
}
private void SetFoo(int index, string value) {
underlyingData[index-1]=value; //1-based indexing
}
}
The second alternative leaves the property definitions unchanged, and two static arrays of delegates representing the reading and writing function of those properties.
private class Version2 {
private static readonly Func<Version2, string>[] readers=new Func<Version2, string>[] {
c => c.Foo1,
c => c.Foo2,
c => c.Foo3,
//......
c => c.Foo50,
};
private static readonly Action<Version2, string>[] writers=new Action<Version2, string>[] {
(c,v) => c.Foo1=v,
(c,v) => c.Foo2=v,
(c,v) => c.Foo3=v,
//......
(c,v) => c.Foo50=v,
};
public string Foo1 { set; get; }
public string Foo2 { set; get; }
public string Foo3 { set; get; }
//......
public string Foo50 { set; get; }
private string ReadFoo(int index) {
return readers[index-1](this); //1-based indexing
}
private void SetFoo(int index, string value) {
writers[index-1](this, value); //1-based indexing
}
}