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

Archive for 2012

.NET/C# duh moment of the day: “A char can be implicitly converted to ushort, int, uint, long, ulong, float, double, or decimal (not the other way around; implicit != implicit)”

Posted by jpluimers on 2012/11/20

A while ago I had a “duh” moment while calling a method that had many overloads, and one of the overloads was using int, not the char I’d expect.

The result was that a default value for that char was used, and my parameter was interpreted as a (very small) buffer size. I only found out something went wrong when writing unit tests around my code.

The culprit is this C# char feature (other implicit type conversions nicely summarized by Muhammad Javed):

A char can be implicitly converted to ushort, int, uint, long, ulong, float, double, or decimal. However, there are no implicit conversions from other types to the char type.

Switching between various development environments, I totally forgot this is the case in languages based on C and Java ancestry. But not in VB and Delphi ancestry  (C/C++ do numeric promotions of char to int and Java widens 2-byte char to 4-byte int; Delphi and VB.net don’t).

I’m not the only one who was confused, so Eric Lippert wrote a nice blog post on it in 2009: Why does char convert implicitly to ushort but not vice versa? – Fabulous Adventures In Coding – Site Home – MSDN Blogs.

Basically, it is the C ancestry: a char is an integral type always known to contain an integer value representing a Unicode character. The opposite is not true: an integer type is not always representing a Unicode character.

Lesson learned: if you have a large number of overloads (either writing them or using them) watch for mixing char and int parameters.

Note that overload resolution can be diffucult enough (C# 3 had breaking changes and C# 4 had breaking changes too, and those are only for C#), so don’t make it more difficult than it should be (:

Below a few examples in C# and VB and their IL disassemblies to illustrate their differnces based on asterisk (*) and space ( ) that also show that not all implicits are created equal: Decimal is done at run-time, the rest at compile time.

Note that the order of the methods is alphabetic, but the calls are in order of the type and size of the numeric types (integral types, then floating point types, then decimal).

A few interesting observations:

  • The C# compiler implicitly converts char with all calls except for decimal, where an implicit conversion at run time is used:
    L_004c: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Implicit(char)
    L_0051: call void CharIntCompatibilityCSharp.Program::writeLineDecimal(valuetype [mscorlib]System.Decimal)
  • Same for implicit conversion of byte to the other types, though here the C# and VB.NET compilers generate slightly different code for run-time conversion.
    C# uses an implicit conversion:
    L_00af: ldloc.1
    L_00b0: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Implicit(uint8)
    L_00b5: call void CharIntCompatibilityCSharp.Program::writeLineDecimal(valuetype [mscorlib]System.Decimal)
    VB.NET calls a constructor:
    L_006e: ldloc.1
    L_006f: newobj instance void [mscorlib]System.Decimal::.ctor(int32)
    L_0075: call void CharIntCompatibilityVB.Program::writeLineDecimal(valuetype [mscorlib]System.Decimal)

Here is the example code: Read the rest of this entry »

Posted in .NET, Agile, Algorithms, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, C++, Delphi, Development, Encoding, Floating point handling, Java, Software Development, Unicode, Unit Testing, VB.NET | 1 Comment »

Gear Keeper – Retractable Gear Attachment Systems

Posted by jpluimers on 2012/11/19

For my birthday wish list :)

–jeroen

via: gear keeper – Google Search.

Posted in About, Opinions, Personal | 1 Comment »

Interesting: site “Your Online Choices | IAB” seems to help in disabling tracking cookies for advertisers

Posted by jpluimers on 2012/11/16

Is there anyone that has experience with the Your Online Choices | IAB site?

They seem to be able to turn off cookie tracking for selected advertisers.

I’m anxious to hear if this scam or not.

–jeroen

Posted in LifeHacker, Opinions | Leave a Comment »

Some notes on finding the cause of a .NET app generating a “application has generated an exception that could not be handled”

Posted by jpluimers on 2012/11/15

A while ago, one of the users at a client got an error in a .NET 1.1 app of which the sources were not readily available:

“application has generated an exception that could not be handled”

I think it is a e0434f4d  exception.

This particular site has very strict rules about what you can and cannot do as a developer. Which means that on a production system, you basically cannot do anything.

A few links that should help me finding the solution, and escalate far enough upstream to get someone with local admin rights to assist me:

If WinDbg is allows to be ran, these should help me:

–jeroen

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

Interesting: Visual Studio Project Renamer | ComVisible

Posted by jpluimers on 2012/11/14

Over the course of development time, each suite of projects is bound to get some renames.

Doing that from the Visual Studio IDE is a pain, so I was glad to find Visual Studio Project Renamer by ComVisible.

Though it only supports C# and VB.NET projects (so no solution rename or rename of F#, Database or Reporting Service projects, nor stuff outside of the Microsoft realm like Prism).

These Just geeks: Renaming a Visual Studio Project link led me to the project.

Renaming solutions still is largely a manual operation as it involves renaming directories. You have to re-add some (sometimes all) projects later where this tool can come in handy: CoolCommands by SharpToolbox.

–jeroen

via:

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, F#, Prism, Software Development, VB.NET, Visual Studio and tools | Leave a Comment »

.NET/C#: Cleaning up path names with invalid characters (via: validation – C# Sanitize File Name – Stack Overflow)

Posted by jpluimers on 2012/11/13

Thanks Andre for this cleanup code:

To clean up a file name you could do this

private static string MakeValidFileName(string name)
{
string invalidChars = Regex.Escape( new string( Path.GetInvalidFileNameChars() ) );
string invalidReStr = string.Format( @"[{0}]+", invalidChars );
return Regex.Replace( name, invalidReStr, "_" );
}

Next to GetInvalidFileNameChars, you have GetInvalidPathChars.

–jeroen

via: validation – C# Sanitize File Name – Stack Overflow.

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

IIS on Windows 7: installing and opening the firewall

Posted by jpluimers on 2012/11/12

Usually, I run IIS only on server systems, but for the occasional time when I need it on a (development) workstation (as Cassinni only listens to localhost), these links come in handy:

  1. Installing IIS 7 on Windows Vista and Windows 7 : Installing IIS 7 : Installing and Configuring IIS : The Official Microsoft IIS Site.
    This helps you setup IIS 7, and make sure ASP.NET works on it
  2. Allowing Remote Connection in IIS on Windows 7 – Super User.
    This helps you open up the firewall to access IIS over http port 80.
  3. Make sure if you add virtual directories or applications, that they are based on Physical Paths. When not (for instance with a subst path), you will get an error like this:
    <<
    —————————
    Add Virtual Directory
    —————————
    The specified directory does not exist on the server.
    —————————
    OK
    —————————
    >>
  4. For ASP.NET applications, when creating a virtual directory or application (especially outside the C:\inetpub\wwwroot realm), make sure the rights are set correctly.
    The IIS configuration will warn you when testing a new virtual directory:
    <<
    The server is configured to use pass-through authentication with a built-in account to access the specified physical path. However, IIS Manager cannot verify whether the built-in account has access. Make sure that the application pool identity has Read access to the physical path. If this server is joined to a domain, and the application pool identity is NetworkService or LocalSystem, verify that <domain>\<computer_name>$ has Read access to the physical path. Then test these settings again.
    >>
    When in doubt, check out the rights set to C:\inetpub\wwwroot and use that as a point to get started.
    Usually the user is IIS_USRS, and the minimum rights looks like read+execute, but in fact is excute+read-data+read-attributes+read-extended-attributes.

–jeroen

Posted in Development, IIS, Software Development, Web Development | 2 Comments »

Some interesting USB devices to add more than 2 monitors to your PC or Mac (via: USB Graphics – Graphic solutions GeForce & Radeon)

Posted by jpluimers on 2012/11/09

Thanks to Matthijs ter Woord who pointed me to these.

They require a single Intel®, Nvidia®, or AMD® primary WDDM driver. That driver does the actual rendering, the USB device then gets the rendered parts over USB to the monitor.

The really cool thing is: they work on a PC with Windows XP and higher, and on  Mac with OS X Tiger or better.

The chipsets are based on DisplayLink technology; they have their own USB devices as well.

USB2.0 to DVI-I Graphics

CSV-2000D – SenseVision USB Graphics – USB2.0 to DVI-I

USB2.0 to DVI-I graphics devices let you easily add an additional monitor to your notebook PC, desktop and MacBook®. The Club 3D SenseVision USB2.0 to DVI-I Graphics allows you to extend your desktop display beyond 1080p HD resolution displays. … View Details

USB2.0 to HDMI Graphics

CSV-2000H – SenseVision USB Graphics – USB2.0 to HDMI

USB2.0 to HDMI graphics devices let you easily add an additional monitor to your notebook PC, desktop and MacBook®. The Club 3D SenseVision USB2.0 to HDMI Graphics allows you to extend your desktop display beyond 1080p HD resolution displays. … View Details

–jeroen

via: USB Graphics – Graphic solutions GeForce & Radeon.

Posted in Apple, Mac, Mac OS X / OS X / MacOS, Mac OS X 10.4 Tiger, Mac OS X 10.5 Leopard, Mac OS X 10.6 Snow Leopard, Mac OS X 10.7 Lion, MacBook, MacBook-Air, MacBook-Pro, Power User, Windows, Windows 7, Windows 8 | Leave a Comment »

.NET/C#: Drives, Directories, Paths and Filenames in Windows is hard, let alone cross platform (: via: .net – Why Path.Combine doesn’t add the Path.DirectorySeparatorChar after the drive designator? – Stack Overflow)

Posted by jpluimers on 2012/11/08

Handling names of drives and paths (directories, filenames) is hard in Windows, as both C:myfile.ext and C:\myfile.ext are valid – but potentially different – filenames, C is a valid driveletter, C: and C:\ are valid – but also potentially different – directory names.

This leads into confusion as how Path.Combine behaves.

Part of the confusion comes from the meaning of the absence or presence of the leading DirectorySeparatorChar as explained by user Peter van der Heijden:

C:filename is a valid path and is different from C:\filename. C:filename is the file filename in the current directory on the C: drive whereas C:\filename is the file filename in the root of that drive. Apparently they wanted to keep the functionality of refering to the current directory on some drive.

This behaviour is described here in MSDN

Another oddity is that Path.Combine will only use the drive portion of the left argument when the right argument contains an absolute path.

If you understand the above, then dealing with cross platform directory and path separators, spaces in filenames and UNC path names are peanuts (:

–jeroen

via: .net – Why Path.Combine doesn’t add the Path.DirectorySeparatorChar after the drive designator? – Stack Overflow.

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

Wally and testing the new 3-D printer – via: Dilbert comic strip for 11/05/2012

Posted by jpluimers on 2012/11/07

Once every while a Dilbert strip is so great, you *have* to re-publish it.

So here it goes: Wally and testing the new 3-DD printer.

Wally and testing the new 3-D printer (Dilbert comic strip for 11/05/2012 from the official Dilbert comic strips archive.)

–jeroen

via: Dilbert comic strip for 11/05/2012 from the official Dilbert comic strips archive..

Posted in Comics | Leave a Comment »