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,854 other subscribers

Archive for the ‘C#’ Category

“Try to avoid foreach/for loops”–Over my Dead Body! | Visual Studio Feeds

Posted by jpluimers on 2011/03/16

Zack Owens wrote a nice article comparing various loop constructs.

Summary:
If the semantics are the same, it does not matter if you use foreach, for, while or do while: they all  have equal speed.

So: choose the loop construct that best fits the problem you are trying to solve.

–jeroen

via: “Try to avoid foreach/for loops”–Over my Dead Body! | Visual Studio Feeds.

Posted in .NET, C#, Delphi, Prism, Software Development | 12 Comments »

C# generics: Constraints on Type Parameters

Posted by jpluimers on 2011/03/10

I usually forget the exact details on C# constraints when using generics.

One of the especially irritating things is that you cannot apply all the constraints you want.

Some built-in language features are covered by special types in the .NET framework class library, for instance enums.

Which means that code like this will not compile:

        // Error	1	Constraint cannot be special class 'System.Enum'
        public static T StringToEnum(string name) where T : System.Enum
        {
            return (T)Enum.Parse(typeof(T), name);
        }

You need to replace it with the code below, which uses the fact that an enum is a ValueType (hence the struct constraint) implementing the interfaces IComparable, IFormattable and IConvertible constraints: Read the rest of this entry »

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

Why is the result of RoundTo(87.285, -2) => 87.28 – Stack Overflow

Posted by jpluimers on 2011/03/08

Programmers on all sorts of platforms get this wrong all the time (I admit having done this in bad ways myself too).

In short: Don’t expect floating point values in a computer to be represented as decimals.

Rob Kennedy wrote a very nice answer on this:

The exact value 87.285 is not representable as a floating-point value in Delphi. A page on my Web site shows what that value really is, as Extended, Double, and Single:

87.285 = + 87.28500 00000 00000 00333 06690 73875 46962 12708 95004 27246 09375

87.285 = + 87.28499 99999 99996 58939 48683 51519 10781 86035 15625

87.285 = + 87.28500 36621 09375

And David Heffernan points to the best link you can get on this topic:

The classic reference on floating point is What Every Computer Scientist Should Know About Floating-Point Arithmetic.

For currency based calculations, if indeed this is, you should use a base 10 number type rather than base 2 floating point. In Delphi that means `Currency`.

–jeroen

via delphi – Why is the result of RoundTo(87.285, -2) => 87.28 – Stack Overflow.

Posted in .NET, Algorithms, C#, Delphi, Development, Floating point handling, Software Development | 14 Comments »

Ask yourself: “are you writing a framework now?” – How (not) to write Factorial

Posted by jpluimers on 2011/03/01

On the Development Choas Theory blog, I bumped into a brilliant blog post titled How (not) to write Factorial in Java.

It is not about Java, or any other programming language, platform or library for that matter.

It equally applies to your favourite programming language and framework (be it shell scripts, assembler, .NET, Delphi, haskell, PHP, ruby, Eiffel, Java or any other).

It is about the question that Neal Ford taught me quite a while ago that you should ask yourself several times a day:
“Am I writing a framework now?”

Developing software is about only developing the things you really need to.

Framework development is in a totally different league than developing ‘just’ software.
It requires even more thought, attention, testing, etc.
And after that, you need to support it on a different level too.

Frameworks are far more costly than you would guess at first sight…

So only develop a framework  for something (*) if you observe that it is actually used in a lot of places and requires a framework.

(*) insert what you are currently working on here.

–jeroen

Via:  Development Chaos Theory » Blog Archive » How (not) to write Factorial in Java

Posted in .NET, C#, Delphi, Development, Software Development | 6 Comments »

Exception Filter – VB.NET supports this, but not C#, so write a VB.NET wrapper to expose it to C#

Posted by jpluimers on 2011/02/24

Often I explain to people that there are a lot of .NET languages, because the .NET IL is rich, so the individual languages can focus on the IL pieces they do best.

This also means, that most languages have some support for specific pieces of IL that other languages do not have support for.

The IL Exception Filter feature is one such thing. It is supported by for instance VB.NET but not by C# as Junfeng Zhang explains:

C# does not support exception filter. However, VB and IL support it. To add exception filter to C#, we can build a function in VB or IL, then call it in C#.

Read his full article for the complete code.

–jeroen

via: Exception Filter – Junfeng Zhang’s Windows Programming Notes – Site Home – MSDN Blogs.

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

Duh: projects of non-installed identities won’t load in VS. Learned: always install your VM yourself

Posted by jpluimers on 2011/02/23

For a project, I got a preconfigured VM for a client.

Certain projects they used gave this message:

—————————
Microsoft Visual Studio
—————————
The selected file cannot be opened as a solution or project. Please select a solution file or a project file.
—————————
OK
—————————

It appeared the didn’t do a full Visual Studio installation, so certain project types didn’t load.

This case it was Visual Studio 2008 and VBPROJ types, as Visual Basic .NET is way easier to do Office automation than C#.

In C#, you need truckloads of Type.Missing, which you do not need in VB.NET; Visual Studio 2010 made this easier, but you still often need them.

Lesson learned: always build all projects when you get a development VM, and prefer to install the VM yourself.

–jeroen

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

Supporting Office 2003 from .NET: getting the Office 2003 Primary Interop Assemblies

Posted by jpluimers on 2011/02/22

Often you work with projects not having the latest stuff.
Sometimes that is a good thing: latest stuff is not always best :-)

In this case, the client had Office 2003, and needed to do some Excel automation from .NET.
The development systems however had Office 2007 on it, so importing Excel defaults to the Office 2007 Primary Interop Assembly: Office version 12 in stead of 11. Read the rest of this entry »

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Delphi, Development, Prism, Software Development, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools | Leave a Comment »

Verifying if two hosts are on the same IPv4 network

Posted by jpluimers on 2011/02/17

Recently, I wrote a bunch of code both in C# and Delphi to verify if two hosts are on the same IPv4 network.

Even though the public IPv4 address space is exhausted (IANA recently allocated the final two IPv4 blocks of their primary pool to APNIC, then allocated the five remaning /8 IPv4 blocks to the RIRs, thereby depleting the IPv4 pool), they are still used a lot, especially in private networks.

Over the next couple of weeks, you will see a couple of blog posts explaining:

  • how to get the IPv4 addresses and network masks of network interfaces in your computer
  • how to convert those to and from strings
  • how to convert a host-name to a list of IP addresses
  • how to lookup the current host-name
  • how network masks are related to counting leading/trailing ones and zeros
  • how to compare two IPv4 addresses given they have the same network masks

Those blog posts will have sample code in both Delphi and C#. And there will be unit tests to verify the underlying code.

–jeroen

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

telerik/razor-converter: Tool for converting WebForms Views to Razor (C# Only) – GitHub

Posted by jpluimers on 2011/02/16

Does this finally end the ASP.NET WebForms days?

Tool for converting WebForms Views to Razor C# Only

–jeroen

via: telerik/razor-converter – GitHub.

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

some good tips on C# Excel Interop Use from Sam Allen

Posted by jpluimers on 2011/02/16

Each time I need to do Office COM interop, I wish they had overloaded methods: all those Type.Missing entries, and speed issues.

I know C# 4.0 makes missing parameters easier, but a lot of projects are not yet in Visual Studio 2010.

So this C# Excel Interop Use page by Sam Allen comes in handy.

–jeroen

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