The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,862 other subscribers

Archive for the ‘C# 3.0’ Category

Recommended reading: Greg Beechs Blog | Greg Beechs Website

Posted by jpluimers on 2012/05/10

To bad that Greg Beechs Blog only has “42 Entries” as those entries (from the start of 2006 till the end of 2009) are well worth reading on various .NET related topics.

–jeroen

via Greg Beechs Blog | Greg Beechs Website.

Posted in .NET, C#, C# 2.0, C# 3.0, Development, Software Development | Leave a Comment »

Dodgy Coder: Coding tricks of game developers

Posted by jpluimers on 2012/04/26

Some very interesting tips from game development that apply juts as well to general software development.

On code health:

Now I always try to dig right down to the root cause of a bug, even if a simple, and seemingly safe, patch is available. I want my code to be healthy. If you go to the doctor and tell him “it hurts when I do this,” then you expect him to find out why it hurts, and to fix that.

Though tools like SourceMonitor can help you track your code health, the best tool is between your ears.

–jeroen

via: Dodgy Coder: Coding tricks of game developers.

Posted in .NET, Batch-Files, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Delphi, Delphi x64, Delphi XE2, Development, JavaScript/ECMAScript, PHP, PowerShell, Scripting, Software Development | 1 Comment »

SQL Server 2000 (and probably later) other reason for System.Data.SqlClient.SqlException: A severe error occurred on the current command. (via SQL Server Forums)

Posted by jpluimers on 2012/04/24

While transitioning from SQL Server 2000 to 2008, I recently had the “A severe error occurred on the current command. The results, if any, should be discarded.”  occurring on SQL Server 2000 in the form as shown at the bottom of this message.

Many of the search results point you into the area of atabase corruption, or in using NVARCAR parameters with SQL Server 2000 or SQL Server 2005 (the app didn’t use NVARCAR, nor did it use large VARCHAR parameters).

The cool thing on the SQL Server Forums – System.Data.SqlClient.SqlException: A severe error occurred on the current command post was that it summed up causes, and asked for more:

Posted – 06/17/2004 :  15:05:20

Rashid writes “Hi: Gurus I am getting these errors when I try to execute my application. According to MS knowledge base (http://support.microsoft.com/default.aspx?scid=kb;en-us;827366) these errors happen due to following resons

  1. You use a SqlClient class in a Finalize method or in a C# destructor.
  2. You do not specify an explicit SQLDbType enumeration when you create a SqlParameter object. When you do not specify an explicit SQLDbType, the Microsoft .NET Framework Data Provider for SQL Server (SqlClient) tries to select the correct SQLDbType based on the data that is passed. SqlClient is not successful.
  3. The size of the parameter that you explicitly specify in the .NET Framework code is more than the maximum size that you can use for the data type in Microsoft SQL Server.

None of these are true in my case. Are there any other reasons that can cause these problems..

There is one more: sending huge SQL Statements to your SQL Server is always a bad idea and gives this error too. Read the rest of this entry »

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Database Development, Development, Encoding, Software Development, SQL Server, SQL Server 2000, SQL Server 2008 R2, Unicode | Leave a Comment »

.NET/C#: setting private fields and properties through reflection (via: .net – Is it possible to set private property via reflection – Stack Overflow)

Posted by jpluimers on 2012/04/19

The Stack Overflow question “Can I set a private property via reflection?” was answered “Yes, it is” with some good examples on how to set private fields and properties through reflection.

Recommended reading!

–jeroen

via .net – Is it possible to set private property via reflection – Stack Overflow.

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Development, Software Development | Leave a Comment »

.NET/C#: Way overdue post – the syntax inside the Obsolete attribute; Looking for the new ConfigurationManager? (via: @ScottCate Blog Moved ….)

Posted by jpluimers on 2012/04/18

Everytime I get a warning like

Warning 1 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by ConfigurationManager.AppSettings'

it reminds me I should have written a blog post about it, as the solution is a tiny bit more than just replacing System.Configuration.ConfigurationSettings.AppSettings by System.Configuration.ConfigurationManager.AppSettings.

Scott Gate wrote a nice post on his old blog about this (his new blog is awesome, he really should import his old posts into his new blog) that explains how to solve this well, so below is an elaboration on the how of the change, a tiny trick and a short series of steps to resolve this warning.

First of all, the above message means you are touching code that has been written in the .NET 1.x era, and the maintaining people (you! <g>) have been too lazy to solve the warning. That is bad, as your code should compile without warnings, and preferably without hints too. Read the rest of this entry »

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Development, Software Development | Leave a Comment »

Getting the public static readonly strings and public const strings (and their values) from a class

Posted by jpluimers on 2012/04/03

Quite a few projects have one or more classes with with a bunch of public const string or public static readonly string values. Use const when things are really constant (like registry configuration keys), use static readonly when – upon change – I do not want to recompile dependent assemblies. Many people recommend static readonly over const.

Having members in stead of string literals scattered all over the place allows you to do compile timing checking, which I’m a big fan of as in the age of things getting more and more dynamic, you need to have as many sanity checks as possible.

One of the checks is to verify these const members and their values. Sometimes you need the list of members, sometimes the list of values, and sometimes each member value should be the same as the member name.

The listing below shows the code I came up with.

It works with code like this, and those can have more code and other fields (non string, non public) in them as well:

namespace bo.Literals
{
    public class FooBarPublicStringConstants
    {
         public const string Foo = "Foo";
         public const string Bar = "Bar";
    }
    public class FooBarPublicStaticReadOnlyStringFields
    {
         public static readonly string Foo = "Foo";
         public static readonly string Bar = "Bar";
    }
}

I started out with this code, but that is limited to classes only having public const fields. Not flexible enough. Read the rest of this entry »

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Development, Software Development | Leave a Comment »

naming – What’s the use/meaning of the @ character in variable names in C#? – Stack Overflow

Posted by jpluimers on 2012/03/28

Duh, I always thought @ could only be used for strings.

Not so: just like with the & in Delphi is used to escape keywords, the additional use of @ in C# is to escape identifiers:

The prefix “@” enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier.

–jeroen

via: naming – What’s the use/meaning of the @ character in variable names in C#? – Stack Overflow.

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Delphi, Development, Software Development | Leave a Comment »

reflection – C# – Resolving a parameter name at runtime – Stack Overflow

Posted by jpluimers on 2012/03/27

So I won’t forget: a GetName method returning the name of a parameter, local or field.

Tags: C#, reflection, IL parsing, argument name, anonymous type, generic type cachegeneric type caching.

–jeroen

via: reflection – C# – Resolving a parameter name at runtime – Stack Overflow.

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Development, Software Development | 4 Comments »

WTF C# code of the month

Posted by jpluimers on 2012/03/14

When I come across code like this, I’m always astonished:

			catch(Exception ex)
			{
				string strMess = ex.Message;
			}

What was the person thinking when he wrote this? Did he get distracted and nobody else notice this before checking it into their version control system?

In the same module:

		private string ToString(object myVal)
		{
			try
			{
				if (myVal != System.DBNull.Value)
					return myVal.ToString().Trim();
			}
			catch{}

			return "";
		}

–jeroen

Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

Eric Lippert’s comment and answer explaining nullable operator lifting (c# – Why does the == operator work for Nullable when == is not defined? – Stack Overflow)

Posted by jpluimers on 2012/03/07

It seems I’m not the ony one who watches what Eric Lippert writes closely.

Eric works at the C# team at Microsoft (since 1996, which is about the time Anders Hejlsberg joined Microsoft).

Unlike Anders, Eric is much more visible. I regularly read his blog, and watch his StackOverflow.com contributions (RSS feed) on a regular base.

Recently, he posted a awesome comment “Nullable is nothing but magic” on a the question “C# – Why does the == operator work for Nullable when == is not defined?“, together with a very concise answer explaining that in C# most operators are ‘lifted to nullable’.

Note his tiny – but important – mention that for == VB.net behaves different than C#.

Note that Eric is very productive, he usually contributes to StackOverflow.com multiple times a day, sometimes with material that (at least for me <g>) need a while before I really get the point.

Recommended reading :)

–jeroen

via: c# – Why does the == operator work for Nullable when == is not defined? – Stack Overflow.

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Development, Software Development, VB.NET | Leave a Comment »