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

Archive for the ‘.NET 1.x’ Category

VB.NET: If you want to cast use DirectCast or TryCast; if you want to convert, use CType

Posted by jpluimers on 2014/07/03

I’ve done quite a bit of VB.NET maintenance lately.

Most of that code was riddled with CType, both for conversions and casts. Quite a bit code had Option Explicit and Option Strict Off. A lot of those CType constructions had empty Try / Catch / End Try blocks around them.

Those empty catch blocks are a code smell. They pretend to be able to survive any exceptional disaster, but in practice you can’t. You have to indicate what kinds of disasters you can handle, for instance if a meteorite hits your data center (thanks George Stocker).

Turning off Option Strict can be OK under many circumstances (the default is off), but having Option Explicit off is usually a code smell as well, just like On Error Resume Next (which was also in plenty of the source code).

I do understand a lot of VB.NET source comes from people having programmed in VB 6, VBScript or VBA for a long time where those constructs were more common. But writing code in the 21st century is much more about writing code that you can prove to be right. Having proper error handling and compiler type checking is a big part of that.

It pays to go with the idiom, for example read the good and bad ways of vb.net – Safest way to check for integer.

Back to CType: basically you have do distinguish between conversions and casts. The reason is that when you know it will be a form of cast, CType is way to expensive. And if you know you will be doing conversions, than casting is not what you want.

Casting Read the rest of this entry »

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Development, Software Development, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 7.0, VB.NET 7.1, VB.NET 8.0, VB.NET 9.0 | Leave a Comment »

Some notes/links on Windows Debugging CLR applications

Posted by jpluimers on 2014/07/02

I only need it every once in a while, so finding the right links and tips to help me usually takes too much time.

So here is a small list to get started:

Keywords: CLR, SOS.DLL, WinDbg, mscordacwks.dll, PSSCOR4

Some tips: Read the rest of this entry »

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, Debugging, Development, Software Development | Leave a Comment »

.NET remnants of the past: Visual Studio .NET 2003 cannot start debugging as the assembly is missing

Posted by jpluimers on 2014/05/15

Boy, was this Visual Studio .NET 2003 stuff a long time ago.
Bumped into this one:


---------------------------
Microsoft Development Environment
---------------------------
Visual Studio cannot start debugging because the debug target 'C:\develop\VS-2003\VS-2003-BASE\Source\AppTest\bin\Debug\AppTest.exe' is missing. Please build the project and retry.
---------------------------
OK
---------------------------

Indeed, Visual Studio was right: it didn’t understand the Release build existed like Visual Studio 2005 and up does:

C:\develop\VS-2003\VS-2003-BASE\Source\AppTest\obj\Release\AppTest.exe

–jeroen

Posted in .NET, .NET 1.x, C#, C# 1.0, Development, Software Development, Visual Studio 2003, Visual Studio and tools | Leave a Comment »

.NET/C#: Ctrl+R+R toggles wrap in Visual Studio.NET 2003 (via: Stack Overflow)

Posted by jpluimers on 2014/05/14

A while ago, I had to do some emergency fixing in Visual Studio .NET 2003 as – despite it is unsupported – a client was still using it.

By habit, I tried to rename a variable using the Ctrl+R+R shortcut for Refactor Rename. It didn’t work, and I was not surprised: this refactoring was introduced in Visual Studio 2005.

A while later, I started to notice that WordWrap was turned on in the code editor.

It took me a few minutes and a quick search to find out the two are related:

Visual studio 2003: ctrl+R+R is the shortcut to toggle WordWrap in the code editor.

Thanks Micah for answering that!

–jeroen

via How can I toggle word wrap in Visual Studio.NET? – Stack Overflow.

Posted in .NET, .NET 1.x, C#, C# 1.0, Development, Software Development, Visual Studio 2003, Visual Studio and tools | Leave a Comment »

.NET uses banker’s rounding as default as it follows IEEE 754 (via: Stack Overflow)

Posted by jpluimers on 2014/05/08

It is almost 3 years that Ostemar wrote an interesting answer on Stack Overflow to the question

Why does .NET use banker’s rounding as default? – Stack Overflow.

Few people (even many programmers don’t!) know about rounding and how it can influence software, let alone what bankers rounding does so lets set a few things straight first.

Rounding matters. Depending on the kinds of software you write, it matters a little, or a lot.

For instance, in these categories, it can matter an awful lot:

  • Financial applications
  • Statistical applications

Bankers rounding means rounding half even. Which means that #.5 will round to the even number closest to #.

In bankers rounding, 1.5 rounds to 2, 3.5 to 4 as does 4.5, -1.5 rounds to -2, -3.5 to -4 as does -4.5.

This is called “unbiased” because for reasonable distributions of y values, the expected (average) value of the rounded numbers is the same as that of the original numbers.

This is contrary to what the majority of people are accustomed to: Round half away from zero is taught in most countries (even for the Dutch, despite the alias “Dutch Rounding” for round half to even).

Round half away from zero rounds 1.5 rounds to 2, 3.5 to 4 and 4.5 to 5. Negative numbers round like this: -1.5 rounds to -2, -3.5 to -4 as does -4.5 to -5.

This is only free of overall bias if the original numbers are positive or negative with equal probability.

In short, .NET uses bankers rounding because it follows the IEEE 754 rounding rules.

This was his answer: Read the rest of this entry »

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, .NET CF, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | 3 Comments »

smallestdotnet.com via: shanselman/SmallestDotNet (thanks @shanselman)

Posted by jpluimers on 2014/03/29

Brilliant piece of open source:

SmallestDotNetSmallestDotNet.com is a single page site that does one thing. It tells you the smallest, easiest download you’d need to get the .NET Framework on your system.

Even on Mac OS X it is helpful and recommends Mono and on iOS it recommends looking at MonoTouch.

Thanks Scott Hanselman for making this available!

–jeroen

via:

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Apple, Development, 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 Retina, MacBook-Air, MacBook-Pro, OS X 10.8 Mountain Lion, OS X 10.9 Mavericks, Power User, Software Development, Windows, Windows 7, Windows 8, Windows 8.1, Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, Windows Server 2008 R2, Windows Vista, Windows XP | Tagged: , | Leave a Comment »

.NET/C#: DBNull explained

Posted by jpluimers on 2014/02/11

The DBNull type is a very special type in .NET. It represents null values in databases, which are slightly different than null values in .NET.

The biggest confusion that people have with it is that it won’t convert to anything. Which means that you see a lot of questions like “System.InvalidCastException: Conversion from type ‘DBNull’ to type” “is not valid”.

You’d think the full name would be System.Data.DBNull, it is actually named System.DBNull. The reason is that various other functionality of the System namespace depend on it, for instance the System.Convert class.

DBNull was already present in .NET 1.x, so it predates nullable types that were introduced in C# 2 / .NET 2.

A null value on the database side will result in a DBNull instance.

If you want to explicitly pass a null value to a database, you use a DBNull.Value, which is a singleton.

Why DBNull

There is a very interestin question/answer series on StackOverflow about this: via .net – What is the point of DBNull? – Stack Overflow.

There are a few good arguments both for and against DBNull.

But the baseline is that DBNull predates the introduction in the .NET framework of genuine nullable types. Both their behaviour is slightly different, so DBNull had to stay.

Which means you have to deal with it every now and then.

Invalid casts

A bit more background on the invalid casts.

It is thrown like this:

            throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));

from

Convert.DefaultToType();

which is called from the DBNull method

    object IConvertible.ToType(Type type, IFormatProvider provider);

All other IConvertible methods are implementated like

bool IConvertible.ToBoolean(IFormatProvider provider)
{
    throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromDBNull"));
}

So these all throw the same exception:

    bool IConvertible.ToBoolean(IFormatProvider provider);
    byte IConvertible.ToByte(IFormatProvider provider);
    char IConvertible.ToChar(IFormatProvider provider);
    DateTime IConvertible.ToDateTime(IFormatProvider provider);
    decimal IConvertible.ToDecimal(IFormatProvider provider);
    double IConvertible.ToDouble(IFormatProvider provider);
    short IConvertible.ToInt16(IFormatProvider provider);
    int IConvertible.ToInt32(IFormatProvider provider);
    long IConvertible.ToInt64(IFormatProvider provider);
    sbyte IConvertible.ToSByte(IFormatProvider provider);
    float IConvertible.ToSingle(IFormatProvider provider);
    ushort IConvertible.ToUInt16(IFormatProvider provider);
    uint IConvertible.ToUInt32(IFormatProvider provider);
    ulong IConvertible.ToUInt64(IFormatProvider provider);

–jeroen

via:

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 »

.NET/C#: When XmlSerializer throws “Unable to generate a temporary class (result=1).” / “error CS2001”

Posted by jpluimers on 2014/02/06

A while ago, I had this error when running an application on a hardened server:

Unhandled Exception:System.InvalidOperationException:
Unable to generate a temporary class (result=1).
error CS2001: Source file 'C:\windows\TEMP\0hocq2nq.0.cs' could not be found  error CS2008: No inputs specified
at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence)
at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies)
at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, String defaultNamespace, String location, Evidence evidence)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, String defaultNamespace, String location)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, Type[] extraTypes)

Usually I’m not the first with strange errors, but searching for “Unhandled Exception:System.InvalidOperationException: Unable to generate a temporary class” didn’t get many results.

This was a program running from SSIS under a non-system domain account with very little access.

My first guess was the right now: the XmlSerializer wants to generate a temporary C# file, then compile it into a temporary assembly. Since it cannot generate the C# file because the account does not have access to %windir\TEMP%, the compiler cannot find the (not generated) C# file.

After a few tries, I searched for XmlSerializer without GenerateAssembly, where the first hit ended at Changing where XmlSerializer Outputs Temporary Assemblies – Scott Hanselman.

That post indicated I should try looking for tempFilesLocation in the XmlSerializer context.

That got me these posts: Read the rest of this entry »

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 »

.NET/C#/VB.NET – Default value for generics (via: Stack Overflow)

Posted by jpluimers on 2014/02/05

Looking for the pesky little differences between C# and VB.NET, I stumbled over this nice question by Micah Martin on default values for generics in VB.NET as compared to C#. Actually there were 3 questions, so I did a bit of post-editing:

How do I create the default for a generic in VB.NET? in C# I can call:
T variable = default(T);

  1. How do I do this in VB?
  2. If this just returns null (C#) or nothing (VB.NET) then what happens to value types?
  3. Is there a way to specify for a custom type what the default value is? For instance what if I want the default value to be the equivalent to calling a parameterless constructor on my class.

User Konrad Rudolph – Stack Overflow. promptly gave three answers:

Question 1:

Dim variable As T ‘ or ‘ Dim variable As T = Nothing ‘ or ‘ Dim variable As New T() Notice that the latter only works if you specifiy either the New or the Structure constraint for the generic type.

Question 2:

For value types all members of the struct are “nulled” out, i.e. all reference type members are set to null (Nothing) and all value types are in turn nulled out. And no, since string is a reference type, it does not result in "" for strings as suggested in the other answer.

Question 3:

No, there’s no way to specify this. There are some threads about this on Stack Overflow already, e.g. here. Jon has posted an excellent explanation why this is.

–jeroen

via: c# – Default value for generics – Stack Overflow.

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, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 7.0, VB.NET 7.1, VB.NET 8.0, VB.NET 9.0 | Leave a Comment »

.NET/C#: refactoring some C# 1 code that uses HashTables as a poor mans property bag (via:Stack Overflow)

Posted by jpluimers on 2014/01/30

A while ago, I was refactoring some C# 1 code that uses HashTables as a poor mans property bag.

The problem was that I felt my code was convoluted, and should be denser, especially avoiding Convert.ChangeType. My code was already much simpler than casting tuples to a superclass.

So I asked this question on StackOverflow: c# – Is there a solution that feels less clumsy than Convert.ChangeType to get the value from a HashTable – Stack Overflow.

User dasblinkenlight showed it could be shortened and explained why (hyperlinks are mine):

Since System.String is sealed, the expression

genericType.IsSubclassOf(stringType)

is the same as

genericType == stringType

Therefore you do not need a call of Convert.ChangeType: you can cast to T by casting to object, like this:

object stringResult; // Note the change of type to "object"
if (haveValue)
    stringResult = ((string)properties[propertyName]).Trim();
else
    stringResult = string.Empty;
result = (T)stringResult; // It is allowed to cast object to generic T

The original .NET 1.1 code had loads of null checks wrapped if/then/else statements to assign default values for null values.
I wanted to get rid of that, and get code like this: Read the rest of this entry »

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 »