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

Duh moment: in C#, the integer zero (0) is compatible with all enums

Posted by jpluimers on 2011/11/30

Even if you have used something for over a decade, you can learn about it :)

I was refactoring bits of code where someone clearly didn’t understand the benefits of enumerations, similar to this very contrived example:

using System;

namespace Demo
{
    // explicit equivalence of: 
    // public enum TrafficLight { Red, Yellow, Green };
    public enum TrafficLight { Red = 0, Yellow = 1, Green = 2 };

    public class Program
    {
        public static void Main()
        {
            // old code:
            Console.WriteLine(IntIsRedTrafficLight((int)TrafficLight.Red));
            Console.WriteLine(IntIsRedTrafficLight((int)TrafficLight.Yellow));
            Console.WriteLine(IntIsRedTrafficLight((int)TrafficLight.Green));
        }

        public static bool IntIsRedTrafficLight(int trafficLight)
        {
            return (trafficLight == (int)TrafficLight.Red);
        }
    }
}

The code was using way too many casts, and my goal was something as simple as this: Read the rest of this entry »

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

I was baffled…

Posted by jpluimers on 2011/11/15

When I saw code like this in a production app, I was speachless:

			if (matcher.Trim().Length > 0)
			{
				if (eesteWhere){sqlWhere += "WHERE ";eesteWhere = false;}
				else{sqlWhere += "AND ";}
				sqlWhere += "m.matcher like '" + matcher.Trim() + "%' ";
			}

Not once, twice, but hundred of fragments like these. Not generated, but hand copy-pasted. And the client thought they were running stable, reliable apps :(

This is soo XSCD ‘Exploits of a Mom‘ (aka Bobby Tables):

The department that wrote the code has been closed a while ago, but some serious refactoring time needs to be invested here, as all applications delivered by that department are vulnerable to SQL Exploits.

–jeroen

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

c# – Panel.Dock Fill ignoring other Panel.Dock setting – Stack Overflow

Posted by jpluimers on 2011/11/08

Every once in a a while I do WinForms development. On the .NET platform it still is the best way to create simple business applications that just, well: work.

WinForms apps are not fancy, but the actual users don’t care much, as long as they can their daily work done. They love fanciness of their mobile devices, but for stuff they use 8 hours a day, they just want something that works quickly, well and easily. So WinForms for a baseline is good.

WinForms historically has had two ways of automatically: Anchors and Dock (.NET 2 introduced another way using FlowLayoutPanel and TableLayoutPanel, but often they make things more complicated than needed).

One of the pitfalls of Docking is when you set Dock to Fill. Sometimes the affected control will be too large.
Every time that happens, I am baffled, as .NET is the only platform with that behaviour; I use other platforms too, and they don’t have this docking peculiarity (of course the have others, that’s the fun of using multiple platforms <g>).

The solution is simple:

  1. Right click on the control that misbehaves
  2. Choose “Bring to Front”
Done :)

–jeroen

Via: c# – Panel.Dock Fill ignoring other Panel.Dock setting – Stack Overflow.

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

Using InputBox in C#

Posted by jpluimers on 2011/11/03

Sometimes you just want to ask a user for a simple string of input.

The InputBox function is an easy way to do do this. It has a tiny issue with the icon (it uses the one that belongs to the application installation, not the icon in the project properties).
InputBox has been part of Visual Basic since the 90s. And it is very easy to use from C# and other .NET languages:

  1. Add the Microsoft.VisualBasic assembly (which has been part of the .NET FrameWork since it first got released)  to your solution
  2. Make a call like
    Microst.VisualBasic.Interaction.InputBox("Did you know your question goes here?","Title","Default Text");

Sometimes you have to look a bit further than your regular toolbox for simple solutions.
I should dig up my 2006 session on the My Object in Visual Basic:  that is also very easy to use in C#.

–jeroen

via: Input Message Box in C#?.

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

process – How to check if a program is using .NET? – Stack Overflow

Posted by jpluimers on 2011/10/18

Many processes use or host the .NET run-time.

For Microsoft implementations of the CLR, this is a quick trick of listing them:

tasklist /m "mscor*"
tasklist /m "clr.dll"

The first statement lists all processes that use or host .NET 1.x through 3.x.
The last statement lists all processes that user or host .NET 4.0

On my system, this is the output:

C:\Users\jeroenp>tasklist /m “mscor*”

Image Name PID Modules
========================= ======== ============================================
explorer.exe 1696 mscoree.dll, mscoreei.dll
PrivacyIconClient.exe 7256 MSCOREE.DLL, mscoreei.dll, mscorwks.dll,
mscorlib.ni.dll, mscorjit.dll
PaintDotNet.exe 459736 MSCOREE.DLL, mscoreei.dll, mscorwks.dll,
mscorlib.ni.dll, mscorjit.dll

C:\Users\jeroenp>tasklist /m “clr.dll”

Image Name PID Modules
========================= ======== ============================================
explorer.exe 1696 clr.dll
[/sourecode]

–jeroen

via: process – How to check if a program is using .NET? – Stack Overflow.

Posted in .NET, C#, Development, Power User, Software Development | 1 Comment »

MonoTouch 5.0 released: iOS 5 support for Mono on the iOS 5 release day

Posted by jpluimers on 2011/10/13

With the and iOS 5 release today and the MonoDevelop 2.8 release last week, there is also a new MonoTouch 5.0 released that binds the two and allows you to develop iOS 5 using Mono.

Almost like a mirracle: on the iOS 5 release day, MonoTouch 5 gets released. Lot’s of new stuff to play with, just read the announcement :)

Quote: “If you already have MonoTouch, simply launch MonoDevelop and you will be prompted to update – it’s that easy!

Be sure to also read the new MonoTouch 5 documentation on new iOS  5 features and the comprehensive API diff between MonoTouch 4.2 and 5.0.

–jeroen

via: MonoTouch 5.0 – MonoTouch.

Posted in .NET, C#, Development, Mobile Development, MonoTouch, Software Development, xCode/Mac/iPad/iPhone/iOS/cocoa | Leave a Comment »

MonoDevelop 2.8 is Here! via: Xamarin blog

Posted by jpluimers on 2011/10/12

Last week, Xamarin released version 2.8 of the MonoDevelop development environment.

Biggest feature is xCode 4 support (which integrated the Interface Builder therefore broke MonoDevelop 2.6).

Since it is hard to run xCode 3 on Mac OS X Lion (Mac OS X Lion more than prefers xCode 4), and you need at least xCode 4.2 beta to develop for iOS 5, this welcome upgrade when you are staying current on Mac OS X.

You can use MonoDevelop to create .NET applications for:

  • iOS (iPhone/iPad/iPod-touch)
  • Mac OS X
  • Android
  • Windows
  • ASP.NET

Be sure to read the MonoDevelop 2.8 release notes, as even the list of Major Highlights is long:

  • C# 4.0
  • Defaults to the 4.0 profile.
  • New Garbage Collection engine
  • New Frameworks:
    • Parallel Framework
    • System.XAML
  • Threadpool exception behavior has changed to match .NET 2.0
    • potentially a breaking change for a lot of Mono-only software
    • See information below in the “Runtime” section.
  • New Microsoft open sourced frameworks bundled:
    • System.Dynamic
    • Managed Extensibility Framework
    • ASP.NET MVC 2
    • System.Data.Services.Client (OData client framework)
  • Performance
    • Large performance improvements
    • LLVM support has graduated to stable
      • Use mono-llvm command to run your server loads with the LLVM backend
  • Preview of the Generational Garbage Collector
  • Version 2.0 of the embedding API
  • WCF Routing
  • .NET 4.0’s CodeContracts
  • Removed the 1.1 profile and various deprecated libraries.
  • OpenBSD support integrated
  • ASP.NET 4.0
  • Mono no longer depends on GLIB

–jeroen

via: MonoDevelop 2.8 is Here! « Xamarin.

Posted in .NET, C#, Development, Mono for Android, MonoTouch, Software Development, xCode/Mac/iPad/iPhone/iOS/cocoa | 1 Comment »

Impersonation on the iSeries: Changing Profile User

Posted by jpluimers on 2011/10/12

When running on Windows, changing the current user is called impersionation.

There are various ways to do this in Windows, including

On the iSeries, there is only one way, as everything goes through the same API: use QSYGETPH (Get Profile Handle) to verify a username/password combination and obtain a handle to the authentication token, then use QWTSETP (Set Profile Handle) to change the user currently signed on, as for instance mentioned by Colin Williams directing to the CHGCURUSR tool on FreeRpgTools.com and an article on Swapping AS/400 User Profiles by Shannon O’Donnel that comes with source code.

On the iSeries, when you are done, you should use QSYRLSPH (Release Profile Handle) when done impersonating, and you need to get the handle from the original user profile if you want to return to it.

The IBM documentation contains a small sample with QSYGETPH, QWTSETP and QSYSRLSPH that sets and restores the profile handle (edit 20120214: it got moved to http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Fapis%2Fapiexushand.htm)

Note that on both Windows ans iSeries, impersonation within a process/thread will make it run in the new context, but the process still keeps the identity of the user that started the process.

If you want to change that, then on both you need to start a new process from the impersonated thread.

In Windows, you can combine the impersionation and the creation of a new process by using the CreateProcessWithLogonW function (as mentioned at the Old New Thing by Raymond Chan). I’ve yet to find an equivalent on the iSeries.

–jeroen

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

Forcing decimal dot (.) for number parsing

Posted by jpluimers on 2011/10/11

Small but useful, especially in countries that have something else than a dot (.) as decimal digit separator (all of the green countries: probably more than you’d thought).

The C# sourcecode is really simple use a NumberFormatInfo instance (or an instance of another class implementing IFormatProvider) in a Single.ToString call:

                float number = 3.1415;
                NumberFormatInfo numberFormatInfo = new NumberFormatInfo();
                numberFormatInfo.NumberDecimalSeparator = ".";
                numberFormatInfo.NumberGroupSeparator = string.Empty;
                string numberString = number.ToString(numberFormatInfo);

The classes implementing IFormatProvider are CultureInfo, DateTimeFormatInfo and NumberFormatInfo.

–jeroen

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

Registry Search-Replace tools – correcting the havoc after a data migration

Posted by jpluimers on 2011/10/07

A while ago, I found myself in the situation where at a corporate client the user profiles had moved on the LAN. Very understandable: it was one of the migrations towards DFS. They notified this in advance, so I made backups of everything (home drive and user profile) just to make sure.

The move indeed caused all sorts of havoc, because the data was moved, but the registry was only slightly modified.

Some of the errors I got were like these:

[Internet Explorer - Search Provider Default]
A program on your computer has corrupted your default search provider setting for Internet Explorer.

Internet Explorer has reset this setting to your original search provider, Live Search (search.live.com).

Internet Explorer will now open Search Settings, where you can change this setting or install more search providers.
[OK]

and

[Desktop]
\\old-server\old-share\user-id\Desktop refers to a location that is unavailable. It could be on a hard drive on this computer, or on a network. Check to make sure that the disk is properly inserted, or that you are connected to the Internet or your network, and then try again. If it still cannot be located, the information might have been moved to a different location.
[OK]

Below some of the ramblings on what I did to get everything working again, including registry searches when you are not allowed to run RegEdit, searching through text, and the places in the registry that had to change. Read the rest of this entry »

Posted in .NET, C#, Delphi, Development, Encoding, Power User, Software Development, Unicode | Leave a Comment »