enumeration

Get next enumerator constant/property

时光总嘲笑我的痴心妄想 提交于 2019-11-28 07:20:05
问题 Lets's say I have an enumerator, is it possible to get the property that follows? So if I had today=Days.Sunday would I be able to do something like tomorrow=today.next() ? example: class Days(Enum): Sunday = 'S' Monday = 'M' ... Saturday = 'Sa' I know I could use tuples (like below) to do something like tomorrow=today[1] , but I was hoping there was something built in or more elegant. class Days(Enum): Sunday = ('S','Monday') Monday = ('M','Tuesday') ... Saturday = ('Sa','Sunday') 回答1:

Removing from array during enumeration in Swift?

和自甴很熟 提交于 2019-11-28 04:48:30
I want to enumerate through an array in Swift, and remove certain items. I'm wondering if this is safe to do, and if not, how I'm supposed to achieve this. Currently, I'd be doing this: for (index, aString: String) in enumerate(array) { //Some of the strings... array.removeAtIndex(index) } In Swift 2 this is quite easy using enumerate and reverse . var a = [1,2,3,4,5,6] for (i,num) in a.enumerate().reverse() { a.removeAtIndex(i) } print(a) See my swiftstub here: http://swiftstub.com/944024718/?v=beta Matteo Piombo You might consider filter way: var theStrings = ["foo", "bar", "zxy"] // Filter

IEnumerable , IEnumerator vs foreach, when to use what [duplicate]

落花浮王杯 提交于 2019-11-28 04:43:49
This question already has an answer here: Can anyone explain IEnumerable and IEnumerator to me? [closed] 16 answers I was going through IEnumerable and IEnumerator , but could not get one point clearly..if we have foreach, then why do we need this two interfaces? Is there any scenario where we have to use interfaces.If yes, then can somebody explain with an example. Any suggestions and remarks are welcome. Thanks. foreach uses the interfaces in many cases. You need the interfaces if you want to implement a sequence which foreach can then use. (Iterator blocks usually make this implementation

Why do people use enums in C++ as constants while they can use const?

吃可爱长大的小学妹 提交于 2019-11-28 04:40:48
Why do people use enums in C++ as constants when they can use const ? An enumeration implies a set of related constants, so the added information about the relationship must be useful in their model of the problem at hand. Bruce Eckel gives a reason in Thinking in C++ : In older versions of C++, static const was not supported inside classes. This meant that const was useless for constant expressions inside classes. However, people still wanted to do this so a typical solution (usually referred to as the “enum hack”) was to use an untagged enum with no instances. An enumeration must have all

Methods inside enum in C#

北战南征 提交于 2019-11-28 03:43:49
In Java, it's possible to have methods inside an enum. Is there such possibility in C# or is it just a string collection and that's it? I tried to override ToString() but it does not compile. Does someone have a simple code sample? You can write extension methods for enum types: enum Stuff { Thing1, Thing2 } static class StuffMethods { public static String GetString(this Stuff s1) { switch (s1) { case Stuff.Thing1: return "Yeah!"; case Stuff.Thing2: return "Okay!"; default: return "What?!"; } } } class Program { static void Main(string[] args) { Stuff thing = Stuff.Thing1; String str = thing

Are booleans as method arguments unacceptable? [closed]

余生颓废 提交于 2019-11-28 02:58:24
A colleague of mine states that booleans as method arguments are not acceptable . They shall be replaced by enumerations. At first I did not see any benefit, but he gave me an example. What's easier to understand? file.writeData( data, true ); Or enum WriteMode { Append, Overwrite }; file.writeData( data, Append ); Now I got it! ;-) This is definitely an example where an enumeration as second parameter makes the code much more readable. So, what's your opinion on this topic? Boolean's represent "yes/no" choices. If you want to represent a "yes/no", then use a boolean, it should be self

Lookup Tables Best Practices: DB Tables… or Enumerations

白昼怎懂夜的黑 提交于 2019-11-28 02:57:10
If we have to store the available positions at a company (i.e. Manager, Team Lead, ... etc). What are the best practices for storing it? I have two opinions with comments... "sure, welcoming yours" Storing it as DB table with columns ID and Name, and deal with it using queries and joins. Storing it as Enum and forget about the DB table. In my opinion, I will choose the first solution if I have changing items. So that I won't hard code these options as Enum. I may choose the Enum solution, if I have no doubt that data won't change (for example, Gender: Male, Female). NOTE: I code in English,

Distinction between iterator and enumerator

白昼怎懂夜的黑 提交于 2019-11-28 02:52:32
An interview question for a .NET 3.5 job is "What is the difference between an iterator and an enumerator"? This is a core distinction to make, what with LINQ, etc. Anyway, what is the difference? I can't seem to find a solid definition on the net. Make no mistake, I can find the meaning of the two terms but I get slightly different answers. What would be the best answer for an interview? IMO an iterator "iterates" over a collection, and an enumerator provides the functionality to iterate, but this has to be called. Also, using the yield keyword is said to save state. What exactly is this

Entity Framework enumerating SqlQuery result

我怕爱的太早我们不能终老 提交于 2019-11-28 02:35:28
问题 I have strange error while I am trying to view results of SqlQuery: var sql = "SELECT @someParam"; var someParamSqlParameter = new SqlParameter("someParam", "Some Value"); var result = _dbContext.SqlQuery<string>(sql, someParamSqlParameter); var containsAnyElements = result.Any(); So when debugger is at last line and when I try to expand Results View of result it shows me expected result("Some Value") but on invoking last line I got an exception "The SqlParameter is already contained by

What is the best way to convert an IEnumerator to a generic IEnumerator?

你说的曾经没有我的故事 提交于 2019-11-27 23:26:09
I am writing a custom ConfigurationElementCollection for a custom ConfigurationHandler in C#.NET 3.5 and I am wanting to expose the IEnumerator as a generic IEnumerator. What would be the best way to achieve this? I am currently using the code: public new IEnumerator<GenericObject> GetEnumerator() { var list = new List(); var baseEnum = base.GetEnumerator(); while(baseEnum.MoveNext()) { var obj = baseEnum.Current as GenericObject; if (obj != null) list.Add(obj); } return list.GetEnumerator(); } Cheers I don't believe there's anything in the framework, but you could easily write one: