Contents tagged with C#

  • Where to Declare Variables in C# and JavaScript

    Both JavaScript and C# belong to the C family of languages.  They share curly braces and semi-colons, and in fact there are many cases where the exact same code will execute (correctly, in most cases) as either language.  However, there are certain best practices that are unique to each language, and where variables should be declared is one of them.

    Declaring Variables in C#

    In C#, it’s generally best to declare variables just before they’re used.  Like any convention in programming, there’s some debate about this, but this rule is supported by well-respected books like Clean Code and top-voted answers to questions like this one: Where do you declare … more

  • Microsoft Version Number Integer Type

    Microsoft made some headlines a few years ago when they decided to skip Office 13 and jump straight from Office 12 to Office 14.  Recently, they’ve announced Visual Studio “14” CTP, which appears to be doing the same thing: Current Version (Visual Studio 2013): Next Version: I actually wrote about Microsoft’s apparent triskaidekaphobia back in 2008 with the Office naming, and suggested that they consider updating their software frameworks to follow suit (since if 13 is unlucky, it should be worthwhile to keep it out of our computer programs, right?).  They haven’t taken this logical next step yet, so today I’m announcing an open source project you can use to implement Microsoft … more

  • Refactor Request

    Not sure if it's already there, but the folks at DevExpress or JetBrains (or Microsoft, but I don't want to wait for another VS) should have a refactoring for CodeRush/Refactor! or Resharper that will convert verbose properties into C# 3.5 short properties, like so: Make this: protected bool isSponsored = false; public bool IsSponsored { get { return isSponsored; } set { isSponsored = value; } } Into this: public bool IsSponsored { get; set; }   Ideally, it should work in two forms: 1) Right click on the property (field name let's say) and offer it as an option in that context. 2) Apply to all properties in a class. And it should only do it if it is safe, of … more

  • Tweak web.config To Set Compilation Debug False

    ASP.NET applications should never run with <compilation debug="true"> in production.  It can have drastic performance implications (of the negative kind).  Obviously, in a perfect world, developers would always remember to verify this setting whenever they upload changes to production, but unfortunately many organizations utilize fallible humans in their deployment process, and this is something that is easily missed. As part of an automated build process, this problem can be eliminated fairly easily.  Most sections within web.config can be extracted to separate files (using the configSource="{path}" attribute), and separate files can be pulled in for TEST, STAGE, and … more

  • Trends in Programming Languages

    Al Pascual wrote about some trends he noticed using Google Trend, with regard to different programming languages.  It's an interesting tool, since it can be used to gauge general interest in particular keywords and search terms, as well as news, going back about 4 years.  For instance, here's a comparison of Visual Basic and C# using common search terms for each (VB, VB.NET vs. C#, CSharp): Given just these search terms, it's clear that VB/VB.NET is on a downward trend, with C#/CSharp passing it in mid-2006.  Expand this a bit further to include "visual basic" and "c #" and "c sharp" and you get the following: Visual Basic is still a huge search term in its own right … more

  • ASP.NET Provider Source Code Install Path

    Some months ago, ScottGu announced the availability of source code for the ASP.NET providers.  This was great news and a great many comments followed.  In preparation for a talk I'm giving this week at DevConnections, I went out to grab the source again to be able to show it off during my talk.  But no matter where I told it to install, nothing was installed.  By default it wanted to go to C:\Program Files\Microsoft\ASP.NET Provider Toolkit SQL Samples\ However, nothing showed up there.  Nothing in the Start menu.  Nothing.  So I tried some other paths, but they also failed to work.  Finally, I trolled through the comments and found one that explained … more

  • Check if Unique Constraint will be violated before Insert with LLBLGen

    I needed to determine if a unique constraint would be violated so that I could programmatically update the Name of a business object to make it unique today.  I use LLBLGen for this project's database layer, and I have a unique constraint (actually a unique index) set on the database.  In this case it covers two columns, an integer ID and a string Name, but the nice thing about LLBLGen is that it provides helper methods that generate the predicate needed.  The code required looked like this: public static bool CampaignNameInUse(int advertiserId, string campaignName){  using (DataAccessAdapter adapter = new DataAccessAdapter())  {    CampaignEntity … more

  • Limit Rows In DataTable or DataSet

    I wrote some quick and dirty ADO.NET code to go against an RSS feed instead of a flat XML file today.  In the process I had to figure a way to limit the number of rows returned by the function, which returns a DataTable.  The simplest method I found was this one, which uses the DataTable.Select() method.  Using this technique, you could also pass in a sort parameter (second parameter to Select()) which would let you grab the top N rows from the DataTable after sorting it on whichever column you wished (syntax for sortexpression is "column" which defaults to ascending or "column DESC" for descending).  And of course you can also do a filter, etc., but I didn't … more

  • Render User Control as String Template

    Scott Guthrie has a great example of how to use an ASP.NET user control as a template which one can dynamically bind to data and then pull the results out as a string.  One place this is useful is in AJAX scenarios in which you want to replace the contents of a region of the page with the rendered output of a user control.  I'm using this very successfully in Lake Quincy Media AdSignia for our dashboard pages, to enable me to load the page instantly and then dynamically fetch the individual charts and dashboard controls asynchronously via ASP.NET AJAX.  I'll be writing up a full article on this whole process soon, but for now I just want to show my version of ScottGu's … more

  • Nice Products for Developers

    We have a new intern starting with us at Lake Quincy Media today, and in the course of getting him set up, I thought I would mention of a few products we're using that work quite well for us here.  The first one relates to training new interns with limited real world programming experience (Kent State University doesn't do us any favors here - no Microsoft technology, no SQL technology, and no web technology has been covered for most of their computer science graduates, much less students).  For training, we use and have been very happy with AppDev.  (sidenote: neither AppDev nor KSU's web sites works without a www. prefix, making both of them unnecessarily difficult to access … more

  • Real World ASP.NET Performance Tuning Experience

    I'm in the midst of wrapping up a multi-year long project to replace the advertising engine used to host sponsored ads on a few dozen .NET web sites.  This system traces its "lineage" back to an old ASP application written in 2001 (it served its first 207 impressions on 31 March 2001), and has since been upgraded to ASP.NET 1.0 and 2.0 both as incremental ports, not full revisions (for instance, the database remained constant between these ports, and there was never any decent UI for managing the ads (basically direct table access).  Anyway, the system has grown and grown and now serves about 100M impressions per month, which if you do the math works out to an … more

  • Fixed: CCNET + MSBUILD Logger Error

    Ran into a snag this weekend with my build server - it started throwing errors related to the path to a folder, like this: .MSBUILD : error MSB4015: The build was aborted because the "MsBuildToCCNetLogger" logger failed unexpectedly during shutdown. System.ArgumentException: The path is not of a legal form. at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck) at System.IO.Path.GetDirectoryName(String path) at Rodemeyer.MsBuildToCCNet.MsBuildToCCNetLogger.WriteErrorsOrWarnings(XmlWriter w, String type, IEnumerable list) at Rodemeyer.MsBuildToCCNet.MsBuildToCCNetLogger.WriteProject(XmlWriter w, Project p) at Rodemeyer.MsBuildToCCNet.MsBuildToCCNetLogger.WriteLog(XmlWriter w) at … more