Posts Tagged ‘Solutions’

Structured Exception Handling in .NET Part 2: Multiple Catch Clauses and Different Exception Types

Monday, March 17th, 2008
This entry is part of a series, Structured Exception Handling in .NET»

Not everyone realises that you can have multiple Catch clauses per Try clause. The point in this is that you can catch different types of Exception.

You may have a function which can throw several different types of Exception. You can catch specific types of Exception separately and deal with each of them differently.

For example, you may wish to do something like this (nb. these Exception classes are made up – they aren’t in the .NET framework)…

Try
   DBConn.ExecQuery(Query)
Catch ex as ForeignKeyException 'inherits from KeyException
   MsgBox("Sorry -  this query would cause a conflict with another table")
Catch ex as KeyException
   MsgBox("Sorry - this data is not valid")
Catch ex as ConnectionException
   MsgBox("The database connection has gone down.  Please reconnect and try again")
Catch ex as Exception
   MsgBox("Something else went wrong - " &  ex.Message)
End Try

Note that catch clauses will catch any object of inherited Exception classes as well as the class that you specify, so if you need to catch a more specific type of Exception, you need to put it before a less specific type, which is why ForeignKeyException comes before KeyException, and everything comes before Exception.

SharpDevelop

Sunday, March 16th, 2008

If you find Visual Studio too expensive, and you don’t want to use the Express Editions, then there is an open source alternative called SharpDevelop.  I’ve never used it myself, but I have heard some quite good things about it.  According to the features list, as far as I can tell, they have most of the main features of Visual Studio 2005.

The Enum Class

Sunday, March 16th, 2008

A lot of people don’t realise that there is actually an Enum class in .NET that you can use to perform a few nice tricks. In order to access the shared methods in it in VB.NET, you need to enclose it in square brackets. The following functions I find particularly useful: -

  • [Enum].GetName
  • [Enum].GetNames
  • [Enum].GetValues

These are all useful ways that you can access the values and corresponding strings that are in Enums. In case you aren’t familiar with it, you’ll usually need to use GetType to pass in the type of the enum eg. [Enum].GetName(GetType(System.DayOfWeek),1) will return “Monday”.

Design Patterns

Friday, March 14th, 2008

Sometimes in programming, you find the same problem coming up quite often. It is usually worthwhile working out a standard solution to the problem, or finding out if someone else already has. This solution is called a design pattern. It isn’t actually a piece of code itself, but it is a way of solving a particular recurring problem.

Once you have worked out a design pattern, you can often come up with some code to implement all or part of it, or at least a set of utility functions that will help.

On Reflection Part 1: What is Reflection?

Friday, March 14th, 2008
This entry is part of a series, On Reflection»

Reflection is a way of finding out information about the structure of an object. In other words, we can find out information about what fields, methods, properties, events etc. it contains, usually including names and types. Once we have this information, we need to be able to use it, so in .NET, we have methods for accessing all these things via reflection.

The point in this is that we can write reusable code that can be very generalised to work across all sorts of different classes. This can save us a lot of time in the long run and it often makes the code a lot neater. I quite often find that reflection can save me time even in the short run, so it is well worth the investment of time in learning how to use it.

Entries in this series:
  1. On Reflection Part 1: What is Reflection?
  2. On Reflection Part 2: GetType
Powered by Hackadelic Sliding Notes 1.6.5

Setting register_globals from php.ini

Thursday, March 13th, 2008

I don’t do that much work with php these days, but I still have a few sites that I use it for.  The problem is that a lot of these sites are still relying on register_globals working.  That means that instead of $_GET[name], $_POST[name], $_SESSION[name] etc., you could just use $name.  This was pretty easy, but it did cause a lot of security problems.

By default, php has register_globals disabled on new installs these days, and it will actually be completely removed in php6, but for the moment, I needed a workaround.  Now, I’m no expert on all the different configurations of php running on various web servers (apache, IIS etc), but if you have one where you can do this, it is fairly easy to turn register_globals on, or alternatively, if it is on, you could possibly use this to turn it off.

Simply, create a file called php.ini in your http root, and put the following text in it…

register_globals = On;

That should do it, as long as your configuration allows it!

Practical Code Organisation and Reuse

Thursday, March 13th, 2008

I find the following are useful pieces of advice for keeping code organised: -

  • Try to plan out as much as you can do beforehand and work out what you are going to need to reuse.
  • If you feel like you are doing the same task a second time, see if you can work out a way of making it reusable code then. If you leave it, it will be a bigger job to go through afterwards and sort it out.
  • It is usually worth putting all the data access and business logic code into a library for a project, and just keeping the user interface in the actual executable or website. Even if it looks like you are never going to need the library for anything else, it makes you organise the code better, and you never know when the client will decide that they want a new system to access the existing data.
  • Work out the scope of what you are going to reuse. It is worth having one or more shared code libraries for use across different projects for anything that you think will come in useful again. If you are certain you are only going to reuse within this project, put the code inside this project’s executable or library, otherwise, it is worth thinking about how you might reuse it in future and put it in your shared library.
  • Train yourself to spot patterns. If you use your imagination, you can work out some clever reusable pieces of code that can perform all sort of general functions.
  • Build it up slowly. Don’t try to make an enormous function that does everything at once. If you are absolutely sure you are never going to use the internal functions, make them private. If they are going to be useful by themselves directly from outside, make them public. See my articles on Black Box Coding and Coupling and Cohesion for more details.
  • It is worth using well thought out Namespaces to organise your code.

Buying Visual Studio 2008 from the USA

Thursday, March 13th, 2008

I’m based in the UK, and it costs rather a lot for Visual Studio 2008.  I decided to take a look at what the prices are in the USA, and after playing about with Google Shopping for a bit, I discovered that for the upgrade version, even after shipping, it still only about half the price that I would have to pay here!

I ordered it, and then the company decided that I had to provide a faxed/scanned copy of various things including my credit card for fraud prevention.  I don’t have a fax machine, and I am not really very happy to start emailing an image of my credit card insecurely, so I cancelled the order and found somewhere else.

Second time lucky, it was despatched a few days later and I have it here now.  One thing to bear in mind if you do this is that you may have to pay import tax.  In theory you have to do this in the UK, but the nice people from HM Revenue and Customs seemed to decide that I didn’t need to for mine, so unless I’ve not been hit with it yet, it looks like I got off lucky.

Dining Philosophers – The Simple Solution

Thursday, March 13th, 2008

Most people don’t consider the simplest solution to the Dining Philosophers’ problem – buy more cutlery. It is also a lot more hygienic.

Custom Constructors and Overloading Constructors in .NET

Wednesday, March 12th, 2008

A lot of people don’t take advantage of the fact that .NET will let you create custom constructors and overload them.  The advantage of this is that you can force an object to be in a valid state once it has been created even if it needs some data to be able to be in that state.  Even if it doesn’t need any data, you can still provide a constructor with no parameters and overload it with parametarised constructors as alternatives.  That just makes using the class a lot easier when it comes to creating objects.