instruction
stringlengths
0
30k
If you're using ASP.Net there are a number of validation controls you can use. These controls are written in a very generic way, such that most of them automatically duplicate your validation logic between the client and server, even though you only set options for the control in one place. You are also free to i...
I've taken to hand-coding all of my DDL (creates/alter/delete) statements, adding them to my .sln as text files, and using normal versioning (using subversion, but any revision control should work). This way, I not only get the benefit of versioning, but updating live from dev/stage is the same process for code and dat...
I've taken to hand-coding all of my DDL (creates/alter/delete) statements, adding them to my .sln as text files, and using normal versioning (using subversion, but any revision control should work). This way, I not only get the benefit of versioning, but updating live from dev/stage is the same process for code and dat...
There are three ways to iterate over a result set. The best way in terms of both readability and performance is usually to use the built-in cursor iterator. <pre> curs.execute('select * from people') for row in curs: print row </pre> You can fetch all the rows into a list, but this can have some bad si...
I agree that scripting everything is the best way to go and is what I advocate at work. You should script everything from DB and object creation to populating your lookup tables. Anything you do in UI only won't translate (especially for changes... not so much for first deployments) and will end up requiring a tool...
Confirm that you have the correct email address before sending out emails. If someone gives the wrong email address on sign-up, beat them over the head about it ASAP. Always include clear "how to unsubscribe" information in EVERY email. Do not require the user to login to unsubscribe, it should be a unique url for 1...
The typical solution is to dump the database as necessary and backup those files. Depending on your development platform, there may be opensource plugins available. Rolling your own code to do it is usually fairly trivial. Note: You may want to backup the database dump instead of putting it into version control. ...
The best way to do this without any compromises would be to implement your own ControllerFactory by inheriting off of IControllerFactory. The CreateController method that you will implement handles creating the controller instance to handle the request by the RouteHandler and the ControllerActionInvoker. The conventi...
The code you posted is missing one important ingredient: the function stateChanged. If you don't quite understand the code you posted yourself, then what happens is when the call to getchats.php is complete, a function "stateChanged" is called and that function will be responsible for handling the response. Since th...
To everyone just starting using version control: **Please do not use git (or hg or bzr) because of the hype** Use git (or hg or bzr) because they are **better tools** for managing source code than SVN. I used SVN for a few years at work, and switched over to git 6 months ago. Without learning SVN first I would...
To everyone just starting using version control: **Please do not use git (or hg or bzr) because of the hype** Use git (or hg or bzr) because they are **better tools** for managing source code than SVN. I used SVN for a few years at work, and switched over to git 6 months ago. Without learning SVN first I would...
Is this a good way to determine OS Architecture?
|c#|windows|registry|
Since the WMI class Win32_OperatingSystem only includes OSArchitecture, I quickly wrote up a method using the registry to try and determine whether or not the current system is a 32 or 64bit system. private Boolean is64BitOperatingSystem() { RegistryKey localEnvironment = Registry.LocalMachine.OpenSubKe...
Since the WMI class Win32_OperatingSystem only includes OSArchitecture in Windows Vista, I quickly wrote up a method using the registry to try and determine whether or not the current system is a 32 or 64bit system. private Boolean is64BitOperatingSystem() { RegistryKey localEnvironment = Registry.L...
How about an object oriented approach? You could deserialise the XML to an object, set the location value on the object, then serialise back to XML. [XStream](http://xstream.codehaus.org/tutorial.html) makes this really easy. For example, you would define the main object, which in your case is CustomData (I'm us...
- The memory windows, very useful if you're doing low level stuff. - Control + K , Control + F - Format selection - great for quickly making code neat - Regions, some love them, some hate them, most don't even know they exist - Changing variables in debug windows during execution - Tracepoints - Conditional break ...
Split the big system to smaller pieces. And don't think that it's so complex, because it usually isn't. By thinking too complex it just ruins your thoughts and eventually the design. Some point you just realize that you could do the same thing easier, and then you redesign it. Atleast this has been my major mistake...
Here's some code I wrote in C# using .NET 1.1 a few years ago. Not sure if this would be exactly what you need (and may not be my best code :)). using System; using System.Data; using System.Data.OleDb; namespace ExportExcelToAccess { /// <summary> /// Summary description fo...
You didn't state what web server you were using, but Apache has a nice little module called [Mime Magic](http://httpd.apache.org/docs/1.3/mod/mod_mime_magic.html) which it uses to determine the type of a file when told to do so. It reads some of the file's content and tries to figure out what type it is based on the c...
You didn't state what web server you were using, but Apache has a nice little module called [Mime Magic](http://httpd.apache.org/docs/1.3/mod/mod_mime_magic.html) which it uses to determine the type of a file when told to do so. It reads some of the file's content and tries to figure out what type it is based on the c...
You didn't state what web server you were using, but Apache has a nice little module called [Mime Magic](http://httpd.apache.org/docs/1.3/mod/mod_mime_magic.html) which it uses to determine the type of a file when told to do so. It reads some of the file's content and tries to figure out what type it is based on the c...
I cant think of a way to have just the Timeout property changed automatically via the webconfig. Manually configure the value or use DI to read the value in for you. It maybe possible also to change the value globally on the machine config.
By the way @[ZombieSheep](#43618) solution date ('Y-m-d', mktime(0,0,0,$MM + 1,-1,$YYYY)); does not work it should be date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1 Of course @Michał Słaby's accepted solution is the simplest.
Perl lets you make part of your regular expression case-insensitive by using the (?i:) pattern modifier. Modern regex flavors allow you to apply modifiers to only part of the regular expression. If you insert the modifier (?ism) in the middle of the regex, the modifier only applies to the part of the regex to the ri...
You can take the C code almost without changes: uint sdbm( string str ) { uint hash = 0; foreach( char ch in str ) { hash = ch + (hash << 6) + (hash << 16) - hash; } return hash; } Or did you think of something more sophisticated?
I don't have a C compiler set up so I can't test to see if it performs the same, but I *think* the following is correct: private static ulong SBDM(string str) { ulong hash = 0; foreach (char c in str) { hash = c + (hash << 6) + (hash << 16) - hash; } ...
WildcardQuery error in Solr
|search|lucene|solr|
I use solr to search for documents and when trying to search for documents using this query "<code>id:*</code>", I get this query parser exception telling that it cannot parse the query with * or ? as the first character. HTTP Status 400 - org.apache.lucene.queryParser.ParseException: Cannot parse 'id:*': '*' o...
@[Sadie](http://stackoverflow.com/questions/15851/refactoring-away-labeled-loops#15946): > They all look less readable than the original, in that they involve spending more code on the mechanism of the code rather than on the algorithm itself Externalizing the second loop outside the algorithm is not necessarily ...
I got into this once, even tried using PSP Dashboard. It's just too hard to keep up with. Who wants to use a stop watch for all their activities? Follow Joel's advice on Painless Scheduling and Evidence Based Scheduling. +1 this question, -1 to PSP.
In the occasions when this has been relevant to me (which, truth be told, is not often) we have tended to do a combination of hosting a dev copy of the solution in-house and mocking what we can't host. I personally think that the more you can host on individual dev boxes the better-- if your dev's PCs are powerful e...
Cast int to Enum in C#
|c#|enums|casting|
What's a quick and easy way to cast an int to an enum in c#?
You could spend literally thousands of dollars on the dev kit or you could be a bad person and go look at the [homebrew stuff][1]. It is technically hacking though, so I only hypothetically recommend it. [1]: http://hbc.hackmii.com/
From a string: YourEnum foo = (YourEnum) Enum.Parse(typeof(yourEnum), yourString); From an int: YourEnum foo = (YourEnum)yourInt;
int one = 1; MyEnum e = (MyEnum)one;
Just cast it: MyEnum e = (MyEnum)3; You can check if it's in range using [Enum.IsDefined][1]: if (Enum.IsDefined(typeof(MyEnum), 3)) { ... } [1]: http://msdn.microsoft.com/en-us/library/system.enum.isdefined.aspx
Ok, here's a funky helper method to give you a flexible way to activate a type given an array of parameters: static object GetInstanceFromParameters(string typeName, params object[] pars) { var t = Assembly.GetExecutingAssembly().GetType(typeName); var c = t.GetConstructor(pars.Select(p...
Ok, here's a funky helper method to give you a flexible way to activate a type given an array of parameters: static object GetInstanceFromParameters(Assembly a, string typeName, params object[] pars) { var t = a.GetType(typeName); var c = t.GetConstructor(pars.Select(p => p.GetType()).T...
Yes the SDKs (and dev hardware) are expensive, and you must be an actual company with an actual office to get one.
Robust Random Number Generation in C++/C#
|c#|c++|random|
I'm looking for a performant, reasonably robust RNG using no special hardware. It can use mathematical methods (Mersenne Twister, etc), it can "collect entropy" from the machine, whatever. On Linux/etc we have a drand48() which generates 48 random bits. I'd like a similar function/class for C++ or C# which can generate...
For C++, [Boost.Random](http://www.boost.org/libs/random/) is probably what you're looking for. It has support for MT (among many other algorithms), and can collect entropy via the `nondet_random` class. Check it out! :-)
Hmm, ok. I'd suggest code like this, then: typedef struct { int type; int result; } DialogData; static gboolean display_dialog(gpointer user_data) { DialogData *dialog_data = user_data; GtkWidget *dialog; if (dialog_data->type & MB_YES...
The Gnu Scientific Library (GSL) <http://www.gnu.org/software/gsl> has a pretty extensive set of RN generators, test harness, etc. If you're on linux, it's probably already available on your system.
[The Gnu Scientific Library][1] (GSL) has a pretty extensive set of RN generators, test harness, etc. If you're on linux, it's probably already available on your system. [1]: http://www.gnu.org/software/gsl
Interesting question! I would do this by picking combinations, something like the following in python. The hardest part is probably first pass verification, i.e. if f(1,2,3) returns true, is that a correct result? Once you have verified that, then this is a good basis for regression testing. Probably it's a goo...
Since the WMI class Win32_OperatingSystem only includes OSArchitecture in Windows Vista, I quickly wrote up a method using the registry to try and determine whether or not the current system is a 32 or 64bit system. private Boolean is64BitOperatingSystem() { RegistryKey localEnvironment = Registry....
Since the WMI class Win32_OperatingSystem only includes OSArchitecture in Windows Vista, I quickly wrote up a method using the registry to try and determine whether or not the current system is a 32 or 64bit system. private Boolean is64BitOperatingSystem() { RegistryKey localEnvironment = Registry.Loca...
Since the WMI class Win32_OperatingSystem only includes OSArchitecture in Windows Vista, I quickly wrote up a method using the registry to try and determine whether or not the current system is a 32 or 64bit system. private Boolean is64BitOperatingSystem() { RegistryKey localEnvironment = Registry....
It's kind of pointless to talk about "database performance", "query performance" is a better term here. And the answer is: it depends on the query, data that it operates on, indexes, hardware, etc. You can get an idea of how many rows are going to be scanned and what indexes are going to be used with EXPLAIN syntax. ...
[OmniAudit][1] might be a good solution for you need. I've never used it before because I'm quite happy writing my own audit routines, but it sounds good. [1]: http://www.krell-software.com/omniaudit/
Try this: StackTraceElement[] stack = Thread.currentThread ().getStackTrace (); StackTraceElement main = stack[stack.length - 1]; String mainClass = main.getClassName (); Of course, this only works if you're running from the main thread. Unfortunately I don't think there's a system prop...
Try this: StackTraceElement[] stack = Thread.currentThread ().getStackTrace (); StackTraceElement main = stack[stack.length - 1]; String mainClass = main.getClassName (); Of course, this only works if you're running from the main thread. Unfortunately I don't think there's a system prop...
You could use (?:F|f)(?:O|o)(?:O|o) The ?: in the brackets in .Net means it's non-capturing, and just used to group the terms of the | (or) statement.
Can I update/select from a table in one query?
|mysql|
I need to select data when a page is viewed and update the 'views' column is there a way to do this in one query, or do I have to use to distinct queries?
"I switched to a local version of prototypejs and added some debugging in the offending method (createWraper) but the debugging never appears before the error..." Actually the offending function being called when the error occurs is "wrapper" which is created inside createWrapper (but not called there). Basically wh...
You would have to do this in two statements in one transaction Begin Tran Update Pages Set Views = Views + 1 Where ID = @ID Select Columns From Pages Where ID = @ID Commit Tran
What language are you using? A standard way to do this would be something like /([Ff][Oo]{2}|BAR)/ with case sensitivity on, but in Java, for example, there is a case sensitivity modifier (?i) which makes all characters to the right of it case insensitive and (?-i) which forces sensitivity. An example of that Java re...
Unfortunately syntax for case-insensitive matching is not common. In .NET you can use RegexOptions.IgnoreCase flag or **?i** modifier
OK, first is dead easy. date ('Y-m-d', mktime(0,0,0,MM,01,YYYY)); Last is a little trickier, but not much. date ('Y-m-d', mktime(0,0,0,MM + 1,-1,YYYY)); If I remember my PHP date stuff correctly... **edit - Gah! Beaten to it about a million times...
Yeah, if you were being efficient you'd throw everything into the same vertex buffer, but I don't think drawing 100 cubes will push any GPU produced in the past 5 years, so you should be fine following the suggestions above. Write a basic pass through vertex shader, shade however you desire in the pixel shader. Eith...
Better yet, a site domain dictionary, containing a definition (over time) for every programming term on Stackoverflow, with the definition itself modded according to the Wiki-like aspects Atwood and others have been discussing. There are coding dictionaries out there but they're all either a) crap or b) not extensib...
Perhaps the most common usees of #define in C# is to differentiate between debug/release and different platforms (for example Windows and X-Box 360 in the XNA framework).
I've had a look at the Video from Molley Rocket and Looked through Jari Komppa's cached tutorials. An IM-GUI seems the best way to go, I think it will be a lot more streamlined, and lot quicker to build than the system I originally had in mind. Now a new issue, I can only except one Answer. :( Thanks again to ...
It has been my experience that Java applications (.NET also) that consider the database last are highly likely to perform poorly when placed into a corporate environment. You need to really think about your audience. You didn't say if it was a web app or not. Either way the infrastructure that you are implementing o...
ASP.NET LocationProvider
|asp.net|security|sitemap|provider|
We need to replace the menu system in our main ASP.NET application. So naturally we're looking at the ASP.NET SiteMapProvider and Menu controls. However we also need enough security to prevent users from directly entering URLs that they shouldn't have access to. We can do this by putting <location> entries in web.confi...
The first general purpose use for the CLR in SQL 2005 I created was a SQL 2005 assembly that has a variety of functions that perform string operations and pattern matches using regular expressions. The native string functions in SQL 2005 can be augmented so that you can validate common formats like phone numbers or cre...
Keeping validation logic in sync between server and client sides
|database|architecture|
In my [previous question][1] most commenters agreed that having validation logic both at client & server sides is a good thing. However there is a problem - you need to keep your validation rules in sync between database and client code. So the question is how can we deal with it ? One approach is to use...
|database|architecture|
In my [previous question][1] most commenters agreed that having validation logic both at client & server sides is a good thing. However there is a problem - you need to keep your validation rules in sync between database and client code. So the question is how can we deal with it ? One approach is to use...
I doubt you can find a regex that matches all (and only) the function calls in some source code. But maybe you could use a tool like [Understand](http://www.scitools.com/products/understand/), or your IDE, to browse your code.
Probably the single most important thing is to use a framework, such as [jQuery][1], or [prototype][2], to iron out the differences between browsers, and also make things easier in general. [1]: http://jquery.com/ [2]: http://www.prototypejs.org/
If you're using C#, take a look at [StyleCop](http://code.msdn.microsoft.com/sourceanalysis).
Visual Studio uses the settings in Tools > Options > Text Editor > [your language] > Formatting to set how it auto-formats code. You can set it up how you like and then use Tools > Import and Export settings to create a .settings file for your team to import and use. It won't enforce rules, but it will make the def...
We use and like [Mercurial][1]. It follows a distributed model - it eliminates some of the sense of having to "check in" work. Mozilla has [moved to Mercurial][2], which is a good sign that it's not going to go away any time soon. One con, in my opinion, is that there isn't a very good GUI for it. If you're comfort...
The easiest way to test for 64-bit under .NET is to check the value of IntPtr.Size. EDIT: Doh! This will tell you whether or not the current process is 64-bit, not the OS as a whole. Sorry!
Try creating a div document.createElement( 'div' ); And then set the tag soup HTML to the innerHTML of the div. The browser should process that into XML, which then you can parse.
Try creating a div document.createElement( 'div' ); And then set the tag soup HTML to the innerHTML of the div. The browser should process that into XML, which then you can parse. > The innerHTML property takes a string > that specifies a valid combination of > text and elements. When the innerHTML > pr...
Looking into the registry is perfectly valid, so long as you can be sure that the user of the application will always have access to what you need. That said, add 4 spaces before each code line to get this: private Boolean is64BitOperatingSystem() { RegistryKey localEnvironment = Registry.Lo...
Looking into the registry is perfectly valid, so long as you can be sure that the user of the application will always have access to what you need.
> The easiest way to test for 64-bit under .NET is to check the value of IntPtr.Size. I believe the value of IntPtr.Size is 4 for a 32bit app that's running under WOW, isn't it? **Edit**: @Edit: Yeah. :)
Take a look at Raymond Chens solution: [http://blogs.msdn.com/oldnewthing/archive/2005/02/01/364563.aspx][1] and here's the PINVOKE for .NET: [http://www.pinvoke.net/default.aspx/kernel32/IsWow64Process.html?diff=y][2] [1]: http://blogs.msdn.com/oldnewthing/archive/2005/02/01/364563.aspx [2]: http://...
Take a look at Raymond Chens solution: [http://blogs.msdn.com/oldnewthing/archive/2005/02/01/364563.aspx][1] and here's the PINVOKE for .NET: [http://www.pinvoke.net/default.aspx/kernel32/IsWow64Process.html?diff=y][2] **Update:** I'd take issue with checking for 'x86'. Who's to say what intel's or AMD's ne...
|apache-flex|data-binding|actionscript|mxml|
|apache-flex|actionscript-3|data-binding|mxml|
Your question is a little ambiguous - are you looking to keep track of the first instances of every letter? If so, an array of length 26 might be the best option. Whenever you insert text into a string at a position lower than the index you have, just compute the offset based on the length of the inserted string.
I don't really understand the question - you throw an exception as you normally would. If `MyThing` implements `IDisposable`, then: using ( MyThing thing = new MyThing() ) { ... throw new ApplicationException("oops"); } And `thing.Dispose` will be called as you leave the block, a...
I don't really understand the question - you throw an exception as you normally would. If `MyThing` implements `IDisposable`, then: using ( MyThing thing = new MyThing() ) { ... throw new ApplicationException("oops"); } And `thing.Dispose` will be called as you leave the block, a...
I don't really understand the question - you throw an exception as you normally would. If `MyThing` implements `IDisposable`, then: using ( MyThing thing = new MyThing() ) { ... throw new ApplicationException("oops"); } And `thing.Dispose` will be called as you leave the block, a...