Indexing an `unsigned long` variable and printing the result

As a preface, this program will not necessarily run exactly like how it does in the question as it exhibits implementation-defined behavior. In addition to this, tweaking the program slightly can cause undefined behavior as well. More information on this at the end. The first line of the main function defines an unsigned long foo … Read more

Can I avoid casting an enum value when I try to use or return it?

enums are supposed to be type safe. I think they didn’t make them implicitly castable to discourage other uses. Although the framework allows you to assign a constant value to them, you should reconsider your intent. If you primarily use the enum for storing constant values, consider using a static class: public static class ReturnValue … Read more

.NET: Unable to cast object to interface it implements

I hat the same problems with a library of mine providing “plugin”-functionality… I got it finally working… Here was my problem: I had one main assembly using plugins, one assembly with the plugin (Plugin.dll) AND (important) another assembly providing the plugin-functionality (Library.dll). The Plugin.dll referenced the main assembly (in order to be able to extend … Read more

Rails Migrations: tried to change the type of column from string to integer

I quote the manual about ALTER TABLE: A USING clause must be provided if there is no implicit or assignment cast from old to new type. What you need is: ALTER TABLE listings ALTER longitude TYPE integer USING longitude::int; ALTER TABLE listings ALTER latitude TYPE integer USING latitude::int; Or shorter and faster (for big tables) … Read more

Safe casting in python

Think not, but you may implement your own: def safe_cast(val, to_type, default=None): try: return to_type(val) except (ValueError, TypeError): return default safe_cast(‘tst’, int) # will return None safe_cast(‘tst’, int, 0) # will return 0

Casting an array of Objects into an array of my intended class

Because toArray() creates an array of Object, and you can’t make Object[] into DataObject[] just by casting it. toArray(DataObject[]) creates an array of DataObject. And yes, it is a shortcoming of the Collections class and the way Generics were shoehorned into Java. You’d expect that Collection<E>.toArray() could return an array of E, but it doesn’t. … Read more

C# type conversion inconsistent?

UPDATE: This question was the subject of my blog in July of 2013. Thanks for the great question! Why does this foreach not create the same error as the variable assignment does? “Why” questions are difficult to answer because I don’t know the “real” question you’re asking. So instead of answering that question I’ll answer … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)