enumeration

Delphi RTTI SetValue for Enumerations

若如初见. 提交于 2019-12-06 05:47:36
问题 How do I use RTTI to set an enumerated field's value ? I.e. type TCPIFileStatus= (fsUnknown, fsProcessed); TTest = class FStatus: TCPIFileStatus; end; ... var Data: TTest; Ctx: TRttiContext; Status : TCPIFileStatus; begin Data := TTest.Create; Status := fsProcessed; Ctx.GetType(Data.ClassType).GetField('FStatus').SetValue(Data, Status); end; I get "Invalid class typecast." NB:I need to use RTTI because I will not always know the object type or field name at design time. 回答1: you must pass a

Differences Between PowerShell and C# when Enumerating a Collection

隐身守侯 提交于 2019-12-06 05:23:19
问题 Here is a simple scenario in C#: var intList = new List<int>(); intList.Add(4); intList.Add(7); intList.Add(2); intList.Add(9); intList.Add(6); foreach (var num in intList) { if (num == 9) { intList.Remove(num); Console.WriteLine("Removed item: " + num); } Console.WriteLine("Number is: " + num); } This throws an InvalidOperationException because I am modifying the collection while enumerating it. Now consider similar PowerShell code: $intList = 4, 7, 2, 9, 6 foreach ($num in $intList) { if (

Different behaviour when collection modified between Dictionary and ConcurrentDictionary

☆樱花仙子☆ 提交于 2019-12-06 05:06:32
With a normal Dictionary code as list below, I get exception that Collection was modified; enumeration operation may not execute. Dictionary<int, int> dict2 = new Dictionary<int, int>(); dict2.Add(1, 10); dict2.Add(2, 20); dict2.Add(3, 30); dict2.Add(4, 40); foreach (var d in dict2) { if (dict2.ContainsKey(2)) dict2.Remove(2); if (dict2.ContainsKey(3)) dict2.Remove(3); } However with ConcurrentDictionary, this works fine. ConcurrentDictionary<int, int> dict1 = new ConcurrentDictionary<int, int>(); dict1.AddOrUpdate(1, 10, (k,v)=> 10); dict1.AddOrUpdate(2, 20, (k, v) => 20); dict1.AddOrUpdate(3

Rich enumeration in Scala

允我心安 提交于 2019-12-06 03:52:57
I am looking for a mechanism to implement rich enumerations in Scala, the same way you can in Java add abstract methods to enums and implement them in the instances of of the enum. Please note that using a sealed trait and case objects is not a solution, because I would not be able to iterate over the existing case objects, unless I mantain a list of them, which is very fragile to changes (especially by other people who don't understand what's going on there) Just extend Val . object items extends Enumeration { val pen = Item("writing") val brush = Item("painting") final case class Item(key:

Does this linq query run on every iteration of the for-each loop?

自古美人都是妖i 提交于 2019-12-06 01:27:06
问题 In another question on SO I answered with code like the one below and got a comment that the LINQ-query probably was evaluated in every iteration of the for/each. Is that true? I know that LINQ-querys does not executes before its items is evaluated so it seems possible that this way to iterate the result can make it run on every iteration? Dim d = New Dictionary(Of String, String)() d.Add("Teststring", "Hello") d.Add("1TestString1", "World") d.Add("2TestString2", "Test") For Each i As String

.NET databinding a combobox to a string enum with Description attributes

亡梦爱人 提交于 2019-12-06 01:07:58
问题 I have an enum like this: public enum Cities { [Description("New York City")] NewYork, [Description("Los Angeles")] LosAngeles, Washington, [Description("San Antonio")] SanAntonio, Chicago } I want to bind this to a combobox and I've tried this: comboBox.DataSource = Enum.GetNames(typeof(Cities)); But that displays the values in the combobox rather than the String description. So I switched to this: public static string GetEnumDescription(Enum value) { FieldInfo fi = value.GetType().GetField

Is there a LINQ extension or (a sensible/efficient set of LINQ entensions) that determine whether a collection has at least 'x' elements?

戏子无情 提交于 2019-12-05 20:52:41
问题 I have code that needs to know that a collection should not be empty or contain only one item. In general, I want an extension of the form: bool collectionHasAtLeast2Items = collection.AtLeast(2); I can write an extension easily, enumerating over the collection and incrementing an indexer until I hit the requested size, or run out of elements, but is there something already in the LINQ framework that would do this? My thoughts (in order of what came to me) are:: bool

WPF How to bind an enum with descriptions to a combobox

拥有回忆 提交于 2019-12-05 18:31:04
问题 Hi I want to bind an enum with descriptions to a combobox: I got next enum: public enum ReportTemplate { [Description("Top view")] 1, [Description("Section view")] 2 } I tried this: <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type System:Enum}" x:Key="ReportTemplateEnum"> <ObjectDataProvider.MethodParameters> <x:Type TypeName="Helpers:ReportTemplate" /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> <Style x:Key="ReportTemplateCombobox" TargetType="dxe

Mock all instances of a class

99封情书 提交于 2019-12-05 18:15:24
问题 I know this is typically a bad practice but in my case it is necessary. I have a case where an Enum holds a class to gain some information. So that Enum creates an instance of that calss in its Constructor. public enum MyEnum { CONSTANT(new MyImpl()); private final MyImpl myImpl; private MyEnum(final MyImpl impl) { this.myImpl = impl; } public void sayHello() { System.out.println(this.myImpl.getSomethingToSay()); } } MyImpl.java is just a class with a single method that returns a String.

Should I store my Enums at database level or in the application logic (.NET)?

柔情痞子 提交于 2019-12-05 17:06:19
I have a table, lets call it Objects. It has a predefined set of records looking like this: ObjectId ObjectName 1 Salmon 2 Trout 3 Seabass 4 Shark Etc.. So, this I would like to implement somehow as an enum. But what's the best way, implement it in the database creating tables for it or in the application logic in an CommonEnums codebehind file etc? We add them in both places, we use code generator to keep in sync the values. The main values are in the Enum but the template automatically generates a script to update the DB We also add a property to our entities to expose an enum property