I have created custom button in winforms in which i've added property with a list of custom class: List<Zasoby> and a method to add item to this list only when there is already item in that list that meets specific criteria (the lambda .where expression).
The class Zasob is serializable.
And in designer i add first Zasob to the list in this button like this:
bt01008xxx.Zasoby.Add(new Zasob { Lokalizacja = new Lokalizacja("01", "008", "000") });
..
public class ZasobSzczegolowoButton: Button, IAddZasoby
{
private List<Zasob> _zasoby = new List<Zasob>(); //{ new Zasob { Lokalizacja = new Lokalizacja("01", "001", "000") } };
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
public List<Zasob> Zasoby
{
get { return _zasoby; }
set
{
_zasoby = value;
if (_zasoby.Any()) BackColor = _zasoby.Sum(x => x.Ilosc) > 0 ? Color.Coral : Color.White;
}
}
public void AddZasoby(List<Zasob> zasoby)
{
var buton = Name;
if (_zasoby != null && _zasoby.Count != 0)
{
var szukaneZasoby =
zasoby?.Where(
x =>
x.Lokalizacja.ObszarKod == _zasoby[0].Lokalizacja.ObszarKod &&
x.Lokalizacja.Segment1 == _zasoby[0].Lokalizacja.Segment1);
if (szukaneZasoby == null) return;
Zasoby.Clear();
Zasoby.AddRange(szukaneZasoby);
}
}
}
...
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Delegate,Inherited = false)]
[ComVisible(true)]
public class Zasob : Attribute
{
public Towar Towar { get; set; }
public Magazyn Magazyn { get; set; }
public Lokalizacja Lokalizacja { get; set; }
public decimal Ilosc { get; set; }
}
Now whenever i try to use method AddZasoby i get System.ArgumentOutOfRangeException. I check the list for null and count items and in debug mode and it shoud exit method but still somehow ends up in the body of the method with the error. (please see the screenshot below)
Any idea what am i doing wrong?
You check the list correctly for the count but with the code Zasoby.Clear(); you clear those elements from the member variable _zasoby again.
Don't let it trick yourself: You're defining the Where()-clause before you clear the list but it will be executed afterwards! That's the trick with lambdas, the predicates you wrote in your Where()-lambda will just be executed as soon as it is evaluated.
This feature is called Deferred Execution, see the first example here.
To fix that, you can enforce the lambda to be executed immediately by calling ToArray() or ToList() before you clear your list like:
public void AddZasoby(List<Zasob> zasoby)
{
var buton = Name;
if (_zasoby != null && _zasoby.Count != 0)
{
var szukaneZasoby =
zasoby?.Where(
x =>
x.Lokalizacja.ObszarKod == _zasoby[0].Lokalizacja.ObszarKod &&
x.Lokalizacja.Segment1 == _zasoby[0].Lokalizacja.Segment1
).ToList(); // *** NOTE ME HERE ***
if (szukaneZasoby == null) return;
Zasoby.Clear();
Zasoby.AddRange(szukaneZasoby);
}
}
That should do the trick.
Related
I have a class that contains a list of another class which has a property that I want to check if it has more than one distinct value.
e.g
public class BasketModel
{
public BasketModel()
{
BasketOrderLines = new List<BasketOrderLine>();
}
.
.
.
public class BasketOrderLine
{
public int OrderLineId { get; set; }
public string ImageUrl { get; set; }
public string ProductType { get; set; }
.
.
Given a basket model object I want to find out if there are more than one distinct value in the ProductType.
e.g If all Product Types are "A" then that would be false, if 3 products are of type "A" and one is of type "B" then this would be true.
Cheers
Macca
Your title: "more than two distinct", your question body: "more than one distinct"
If the title is a typo:
bool notDistinctTypes = theBasket.BasketOrderLine
.Select(o => o.ProductType)
.Distinct()
.Skip(1)
.Any();
This doesn't need to enumerate all items to find out if there is more than one ProductType.
// Does this basket contains three or more types
public bool HasSeveralTypes(BasketModel basket)
{
if (basket == null)
return false;
int differentTypes = basket.BasketOrderLines
.Select(l => l.ProductType)
.Distinct()
.Count();
return (differentTypes > 2);
}
Something like this :
Public bool CheckDistinct (){
var res = basketOrderLines.Select(o => o.ProductType).Distinct ().Count ();
return res > 1;
}
There are a few ways to do this, here's one:
public class BasketModel
{
public BasketModel()
{
BasketOrderLines = new List<BasketOrderLine>();
}
public bool HasMulitpleDistinctProducts
{
get
{
if (!BasketOrderLines.Any())
{
return true; // or false?
}
return BasketOrderLines.Select(b => b.ProductType).Distinct().Count() > 1;
}
}
}
Here is a type extension you can call directly from your list. The pros of this code is to be adaptable to any type implementing IEquals and not only string + kick to use from your code.
The code :
public static class Tools
{
public static bool fDistinctProductType(this List<BasketOrderLine> lstToAnalyse)
{
BasketOrderLine ProductTypeA = lstToAnalyse.FirstOrDefault();
if (ProductTypeA == null) // It's null when lstToAnalyse is empty
return false;
BasketOrderLine ProductTypeB = lstToAnalyse.Where(b => b.ProductType.Equals(ProductTypeA.ProductType)).FirstOrDefault();
if (ProductTypeB == null) // It's null when it doesn't exists a distinct ProductType
return false;
return true;
}
}
How to call:
List<BasketOrderLine> lst = new List<BasketOrderLine>();
// Add element to list
if (lst.fDistinctProductType())
{
// DO Something
}
In my current app I have 6 players and everyone has 1 boolean variable. Which under certain circumstances are set to true (originally they are false).. The problem is that I want to check which 5 variables are set to true and which one is set to false but I can't come up with any good idea.. only some if statements checking every single combination
if(a && b && c && d && e && !f)
{
//f is false in this case and I will do some operations here
}
However this is the ugliest and not well written code ever. What would be more general way of doing it?
You're going to have a hard time doing this with just booleans. But if you wrap the boolean in a class with some other data, it becomes easier.
class Item
{
public bool IsCondition {get; set;}
public string Name {get; set;}
}
var itemsToCheck = new List<Item>()
{
new Item { IsCondition = true; Name = "A",
new Item { IsCondition = true; Name = "B",
new Item { IsCondition = false; Name = "C",
new Item { IsCondition = true; Name = "D",
}
foreach(var item in itemsToCheck)
{
if(!Item.IsCondition)
{
Console.WriteLine($"Item {item.Name} is false");
}
}
You can also get a list of all those that are false with Linq
var items = itemsToCheck.Where(i => !i.IsCondition);
Or if you know there will only ever be one that is false, you can get that single item.
var item = itemsToCheck.Where(i => !i.IsCondition).Single();
So there's two takeaways from this:
You should store sets of similar data in a collection, such as a List
Use a class when you want to group some information together.
You can assign them boolean list and then work with them.
List<bool> bools = new List<bool> {a,b,c,d,e,f};
if (bools.Count(x => x) == 5) // if there are 5 true items
{
int index = bools.IndexOf(false); // index of false item
// do your things here.
}
Remember that indexes are 0 based. means that index 0 refers to first item.
Normally you'd use array/list and just count false values:
var onlyOneFromListIsFalse = players.Select(p => !p.SomeProperty).Count() == 1;
You can use similar approach with individual variables
var onlyOneVariableIsFalse = ((a ? 0 : 1) + (b ? 0 : 1) ... (f ? 0 : 1)) == 1;
Using LINQ and List/Array will greatly reduce your code.
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var players = new List<Player>
{
new Player("Orel", true),
new Player("Zeus"),
new Player("Hercules", true),
new Player("Nepton"),
};
var playingPlayers = players.Where(p => p.IsPlaying);
foreach (var player in playingPlayers)
{
Console.WriteLine(player.Name);
}
}
}
public class Player
{
public string Name { get; set; }
public bool IsPlaying { get; set; }
public Player(string name, bool isPlaying = false)
{
Name = name;
IsPlaying = isPlaying;
}
}
This is a refactoring question mainly.
I am creating some methods to go back/forward through an actions history depending on its Id/PreviousId relationship (see basic class below):
public class Action
{
public int Id { get; set; }
public int PreviousId { get; set; }
public string Title { get; set; }
}
Background Info:
I start off by getting a single action from the database. If the user selects 'GoBack', I need to get the previous action from the database and store it in a LinkedList. This means users can potentially revisit that same action (i.e. by going back then forward again) but by calling it from the LinkedList version rather than getting it from the database again. I don't want to initially retrieve all actions first either from the database. I have this functionality working but my GoBack() and GoForward() methods are pretty much identical.
I was hoping to see if there is a good way of refactoring this into a more generic method set rather than duplicating code? (Note - my code doesn't include the database calls to reduce reading so instead I've put dummy data into a List to act as my database).
Class level variables I'm referencing in the methods:
//The list I'm using to pretend to be my database containing actions
private List<Action> _actions { get; set; }
private Action _currentAction { get; set; }
private LinkedList<Action> _actionLinks { get; set; }
Here is my GoBack() method:
private void GoBack()
{
var current = _actionLinks.Find(_currentAction);
if (current == null)
return;
//If we've already stored the previous action. Just point to it
if (current.Previous != null)
{
_currentAction = current.Previous.Value;
return;
}
//We don't have this action stored so go get it from the database and cache it in the list
var previousAction = _actions.FirstOrDefault(i => i.Id == _currentAction.PreviousId);
//There are no previous actions
if(previousAction == null)
return;
_actionLinks.AddBefore(current, previousAction);
//Now reset the current action
_currentAction = previousAction;
}
Here is my GoForward() method:
private void GoForward()
{
var current = _actionLinks.Find(_currentAction);
if (current == null)
return;
//If we've already stored the next action. Just point to it
if (current.Next != null)
{
_currentAction = current.Next.Value;
return;
}
//We don't have this action stored so go get it from the database and cache it in the list
var nextAction = _actions.FirstOrDefault(i => i.PreviousId == _currentAction.Id);
//There are no further actions
if (nextAction == null)
return;
_actionLinks.AddAfter(current, nextAction);
//Now reset the current action
_currentAction = nextAction;
}
If you want to compile the code. I've added in my Constructor and BuildData method I'm using to test this:
Constructor:
public LinkListTest()
{
_actionLinks = new LinkedList<Action>();
_actions = new List<Action>();
BuildData();
//Just set current to the latest action id
_currentAction = _actions.First(i => i.Id == 6);
//Add it to the linkedlist
_actionLinks.AddFirst(_currentAction);
//Start navigating as a user would
GoBack();
GoBack();
GoForward();
GoBack();
GoForward();
GoBack();
GoBack();
}
BuildData method:
private void BuildData()
{
for (int i = 6; i >= 0; i--)
{
var action = new Action();
action.Id = i;
if (i != 0)
action.PreviousId = i - 1;
else
action.PreviousId = -1;
action.Title = string.Format("Action {0}", i);
_actions.Add(action);
}
}
Thanks in advance!
One way to de-duplicate some of the logic here is to use the visitor pattern.
using ActionListAction = System.Action<System.Collections.Generic.LinkedList<Package.Action>, System.Collections.Generic.LinkedListNode<Package.Action> ,Package.Action>;
...
private void GoBack()
{
Move(new BackwordVisitor());
}
private void GoForward()
{
Move(new ForwardVisitor());
}
private void Move(DirectionVisitor direction)
{
var current = _actionLinks.Find(_currentAction);
if (current == null)
return;
var node = direction.Pointer(current);
if (node != null)
{
_currentAction = node.Value;
return;
}
var action = _actions.FirstOrDefault(i => direction.NextSelector(i, _currentAction));
//There are no further actions
if (action == null)
return;
direction.Add(_actionLinks, current, action);
_currentAction = action;
}
private abstract class DirectionVisitor
{
public Func<LinkedListNode<Action>, LinkedListNode<Action>> Pointer { protected set; get; }
public Func<Action, Action, bool> NextSelector { protected set; get; }
public ActionListAction Add { protected set; get; }
}
private class ForwardVisitor : DirectionVisitor
{
public Forward()
{
Pointer = n => n.Next;
NextSelector = (action, current) => action.PreviousId == current.Id;
Add = (list, current, node) => list.AddAfter(current, node);
}
}
private class BackwordVisitor : DirectionVisitor
{
public Backword()
{
Pointer = n => n.Previous;
NextSelector = (action, current) => action.Id == current.PreviousId;
Add = (list, current, node) => list.AddBefore(current, node);
}
}
Since there are only two options for moving through the list, this may be overkill for this particular scenario. Passing an enum into the Move method with the direction and using conditionals may read better.
I have a base class called BaseStatus which looks like this:
public class BaseStatus
{
public int UnitId { get; protected set; }
public UInt16 StatusValue { get; protected set; }
public string StatusCode { get; protected set; }
public string StatusDescription { get; protected set; }
public BaseStatus()
{
this.UnitId = -1;
this.StatusValue = 0;
this.StatusCode = null;
this.StatusDescription = null;
}
}
Furthermore i have two or more other base classes which derive from BaseStatus and define a other unit id. For example the two classes
public class BaseGlobalStatus : BaseStatus
{
public BaseGlobalStatus()
{
base.UnitId = -1;
}
}
public class BaseGcmGdmStatus : BaseStatus
{
public BaseGcmGdmStatus()
{
base.UnitId = 2;
}
}
public class BaseCcuStatus : BaseStatus
{
public BaseCcuStatus()
{
base.UnitId = 1;
}
}
The Background is that i want to derive from for example BaseCcuStatus and have the correct UnitId in the derived class.
Now i define my correct status classes for example:
public class StatStErrDefinition : BaseGlobalStatus
{
public StatStErrDefinition()
: base()
{
base.StatusDescription = "Kommando nicht zulässig, unit im state ERROR";
base.StatusCode = "STAT_ST_ERR";
base.StatusValue = 3;
}
}
public class GcmStErrDefinition : BaseGcmGdmStatus
{
public GcmStErrDefinition()
: base()
{
base.StatusDescription = "Kommando nicht zulässig, unit im state ERROR";
base.StatusCode = "STAT_ST_ERR";
base.StatusValue = 3;
}
}
public class CcuStErrDefinition : BaseCcuStatus
{
public CcuStErrDefinition()
: base()
{
base.StatusDescription = "Kommando nicht zulässig, unit im state ERROR";
base.StatusCode = "STAT_ST_ERR";
base.StatusValue = 3;
}
}
For my understading, the three classes StatStErrDefinition, GcmStErrDefinition and CcuStErrDefinition should have the UnitId which is set in the derived BaseClass?
Now that i have defined my three Status Classes i want to get them into a registry. Currently im using this piece of code to try get them. Problem is that the result has no items.
registry = new StatusDictionary<UInt16, BaseStatus>();
var unitStatus = typeof(BaseStatus)
.Assembly.GetTypes()
.Where(x => x.BaseType == typeof(BaseStatus))
.Select(x => new
{
StatusType = x,
UnitId = x.GetProperty("UnitId", BindingFlags.Public)
StatVal = x.GetProperty("StatusValue", BindingFlags.Public)
}
)
.Where(x => x.StatVal != null && x.UnitId != null)
.Select(x => new
{
UnitId = (int)x.UnitId.GetValue(null, null),
StatusValue = (UInt16)x.StatVal.GetValue(null, null),
Factory = (Func<BaseStatus>)(() => ((BaseStatus)Activator.CreateInstance(x.StatusType)))
});
try
{
foreach (var status in unitStatus)
{
if (status.UnitId == unitId
|| status.UnitId < 0)
registry.Register(status.StatusValue, status.Factory);
}
}
catch (Exception ex)
{
string temp = ex.Message;
}
After the LINQ expression the var unitStatus is empty...
Later, the registry call looks like that to get the specific class but that is unimportant at this point:
stat = StatusContainer.GetRegistry(this.unitTypeId).GetInstance(this.StatusValue);
For information:
I want to get the status class which should be in the registry by the unittypeid and the specific status value.
Currently my registry method does not work because he is not able to find any class. So there has to be a mistake somewhere. Thanks in advance
#Update 1
I changed my functionality a little bit:
registry = new StatusDictionary<UInt16, BaseStatus>();
//get all types of cucrent assembly
var allAssemblyTypes = Assembly.GetCallingAssembly().GetTypes();
//get all types from base status
var baseStatusTypes = allAssemblyTypes.Where(x => x.BaseType == typeof(BaseStatus));
//Place all concrete types in the foundtypes
List<Type> foundTypes = new List<Type>();
foreach (Type item in baseStatusTypes)
{
var temp = allAssemblyTypes.Where(x => x.BaseType == item)
.Select(x => new
{
StatusType = x,
UnitId = x.GetProperty("UnitId", BindingFlags.Public),
StatVal = x.GetProperty("StatusValue", BindingFlags.Public),
}
);
}
Temp contains now the correct type.
Problem is that if temp is type of StatStErrDefinition the StatusValue and UnitId Property is null.
The fact is that these members are instance members. Is there a way to get the values out of them?
First thing first : your LINQ query is pretty long.
divide it in different step and store them in different variables (or make properties out of them, whatever you prefer)
This is
easy to read / maintain
easy to debug
With this given I think you are able to solve your problem :)
To check if the class is of a certain type you could use the method .OfType
Use this method to get the value. Notice that you must make an instance in your case because the value change in your constructor.
public static object GetPropValue(Type src, string propName)
{
var prop = src.GetProperty(propName);
var instance = Activator.CreateInstance(src);
var value = prop.GetValue(instance);
return value;
}
Instead of
UnitId = x.GetProperty("UnitId", BindingFlags.Public),
use
UnitId = GetPropValue(x,"UnitId"),
How do I find and replace a property using Linq in this specific scenario below:
public interface IPropertyBag { }
public class PropertyBag : IPropertyBag
{
public Property[] Properties { get; set; }
public Property this[string name]
{
get { return Properties.Where((e) => e.Name == name).Single(); }
//TODO: Just copying values... Find out how to find the index and replace the value
set { Properties.Where((e) => e.Name == name).Single().Value = value.Value; }
}
}
Thanks for helping out in advance.
Do not use LINQ because it will not improve the code because LINQ is designed to query collection and not to modify them. I suggest the following.
// Just realized that Array.IndexOf() is a static method unlike
// List.IndexOf() that is an instance method.
Int32 index = Array.IndexOf(this.Properties, name);
if (index != -1)
{
this.Properties[index] = value;
}
else
{
throw new ArgumentOutOfRangeException();
}
Why are Array.Sort() and Array.IndexOf() methods static?
Further I suggest not to use an array. Consider using IDictionary<String, Property>. This simplifies the code to the following.
this.Properties[name] = value;
Note that neither solution is thread safe.
An ad hoc LINQ solution - you see, you should not use it because the whole array will be replaced with a new one.
this.Properties = Enumerable.Union(
this.Properties.Where(p => p.Name != name),
Enumerable.Repeat(value, 1)).
ToArray();
[note: this answer was due to a misunderstanding of the question - see the comments on this answer. Apparently, I'm a little dense :(]
Is your 'Property' a class or a struct?
This test passes for me:
public class Property
{
public string Name { get; set; }
public string Value { get; set; }
}
public interface IPropertyBag { }
public class PropertyBag : IPropertyBag
{
public Property[] Properties { get; set; }
public Property this[string name]
{
get { return Properties.Where((e) => e.Name == name).Single(); }
set { Properties.Where((e) => e.Name == name).Single().Value = value.Value; }
}
}
[TestMethod]
public void TestMethod1()
{
var pb = new PropertyBag() { Properties = new Property[] { new Property { Name = "X", Value = "Y" } } };
Assert.AreEqual("Y", pb["X"].Value);
pb["X"] = new Property { Name = "X", Value = "Z" };
Assert.AreEqual("Z", pb["X"].Value);
}
I have to wonder why the getter returns a 'Property' instead of whatever datatype .Value, but I'm still curious why you're seeing a different result than what I am.