enumeration

How to use std::foreach with parameters/modification

旧城冷巷雨未停 提交于 2019-11-28 16:20:55
问题 I've found myself writing for(int i=0;i<myvec.size();i++) myvec[i]->DoWhatever(param); a lot, and I'd like to compress this into a foreach statement, but I'm not sure how to get param in there without going super-verbose. I've also got things like for(int i=0;i<myvec.size();i++) if(myvec[i]->IsOK()) myvec[i]->DoWhatever(param); and I'd like to rewrite that guy too. Any thoughts? Oh, also, for various reasons, I don't want to use boost. 回答1: #include <vector> #include <algorithm> #include

What is the best way to handle constants in Ruby when using Rails?

岁酱吖の 提交于 2019-11-28 15:17:28
问题 I have some constants that represent the valid options in one of my model's fields. What's the best way to handle these constants in Ruby? 回答1: You can use an array or hash for this purpose (in your environment.rb): OPTIONS = ['one', 'two', 'three'] OPTIONS = {:one => 1, :two => 2, :three => 3} or alternatively an enumeration class, which allows you to enumerate over your constants as well as the keys used to associate them: class Enumeration def Enumeration.add_item(key,value) @hash ||= {}

Get Path To “Links” (AKA Favorites) Folder

折月煮酒 提交于 2019-11-28 13:15:49
问题 UPDATE: So I just found out in doing all this leg work that it seems the "Links" folder in your user folder can be named ANYTHING and you can still access it by going to "C:\Users(username)\Links" Ex: Rename "C:\Users\(username)\Links" to "C:\Users\(username)\MyNewLinksFolder" Then try to browse to... "C:\Users\(username)\Links" (forehead to palm) There is a "Favorites" node in the Windows 7 file explorer tree: The path to those favorites (by default) is here: I want to be able to get this

Looping through a list of Actions

情到浓时终转凉″ 提交于 2019-11-28 12:16:11
I can't understand how to loop through an Action list. When I try it, I end up with the values being the same as the previous iteration. Here's the code (simplified example): string[] strings = { "abc", "def", "ghi" }; var actions = new List<Action>(); foreach (string str in strings) actions.Add(new Action(() => { Trace.WriteLine(str); })); foreach (var action in actions) action(); Output: ghi ghi ghi Why is it always selecting the final element in strings when it performs the action? And how can I achieve the desired output which would be: abc def ghi Your action is a closure, therefore it

Does GetCustomAttributes() preserve the attribute order in .NET?

只愿长相守 提交于 2019-11-28 12:00:58
The title pretty much says it all. When I'm doing some reflection through my classes, will the MemberInfo.GetCustomAttributes() method preserve the order of attributes on a member, or not? The official documentation does not say anything one way or the other. In case you're wondering why I would need this, here's the full explanation. It's lengthy and not needed for the question as it is posed now, but perhaps someone can come up with an alternative solution to the greater problem which does not involve relying on attribute enumeration order. I'm trying to make a flexible framework for an (ASP

Possibility of mapping enum values to string type instead of integer

寵の児 提交于 2019-11-28 10:46:04
Enum attributes are great and I want to use them. But mapping enum values to integer would make it hard to maintain both code and database. Also my database would be highly coupled with my code which I think I should consider that a bad thing. I know I can use a hash to organize an enum attribute with key/value pairs, but still it would be a lot better to be able to use an array and map to string values in database. Is there any way to map enum to strings by default? As far as I know it's not possible with Active Record's built-in enum functionality. However, there are a few popular 3rd party

List all bit names from a flag Enum

早过忘川 提交于 2019-11-28 10:00:37
I'm trying to make a helper method for listing the names of all bits set in an Enum value (for logging purposes). I want have a method that would return the list of all the Enum values set in some variables. In my example [Flag] Enum HWResponse { None = 0x0, Ready = 0x1, Working = 0x2, Error = 0x80, } I feed it 0x81, and it should provide me with a IEnumerable<HWResponse> containing {Ready, Error} . As I didn't find a simpler way, I tried to write the code below, but I can't make it compile. public static IEnumerable<T> MaskToList<T>(Enum mask) { if (typeof(T).IsSubclassOf(typeof(Enum)) ==

How to recursively Iterate over properties of an Entity

末鹿安然 提交于 2019-11-28 09:32:13
问题 Suppose this is what I have in my database table Ancestor ( idAncestor int not null, name varchar(20) not null, ) table Descendant ( idDescendant int not null, name varchar(20) not null, Ancestor_idAncestor int not null ) When ADO.NET generates the entity object for the above 2 tables, I can access Descendant of Ancestor through Ancestors.First().Descendants . If I were to recursively iterate over an ancestor's descendant(s) or descendant's descendant(s) and print out its id and name , the

Java: use Enumeration multiple times

依然范特西╮ 提交于 2019-11-28 08:57:23
问题 I work with DefaultMutableTreeNode , and it has methods depthFirstEnumeration() , breadthFirstEnumeration() and children() that return Enumeration of tree nodes. I need to use the returned Enumeration multiple times, but I can't find any method like reset() in Enumeration . It seems like I only can get all the elements just once, and then call depthFirstEnumeration() again to get new Enumeration , it seems not to be a good solution. Of course, I can get all the elements from Enumeration and

foreach control c# skipping controls

对着背影说爱祢 提交于 2019-11-28 07:47:15
问题 I have the following loop to remove the buttons in my C# Windows Forms application. The only problem is that it skips every other button. How do I go about removing all the button controls from my form? foreach (Control cntrl in Controls) { if(cntrl.GetType() == typeof(Button)) { Controls.Remove(cntrl); cntrl.Dispose(); } } 回答1: I think this way is a bit more readable: var controlsToRemove = Controls.OfType<Button>().ToArray(); foreach (var control in controlsToRemove) { Controls.Remove