Posts Tagged ‘Coding’

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.

Open/Close the CD/DVD Drive

Sunday, March 16th, 2008

Here is a useful bit of WIN32 API code to open/close the CD/DVD drive.

First you need to declare a reference to mciSendString, unless you are coding in C++ or something that already has the Windows API declared.  I suggest you use the API viewer to do that.  Then you need to call it, passing in either “set cdaudio door open” or “set cdaudio door closed”.

I think this works all the way back to Windows 3.1, if that is any use to anyone!

Sensible Security for Passwords or PINs

Sunday, March 16th, 2008

There are lots of times when you need to do some sort of login system that takes a password or a PIN number and tries to login to a server with it.  It might be obvious to some people, but it is a simple mistake to make that you shouldn’t ask your server application what the correct PIN/Password is and then compare it, but you should instead send the PIN/Password to the server and ask whether it is correct.

A good example of this is a cash machine.  If someone hacks into a cash machine network, they shouldn’t be able to ask the system for a PIN number for a specific card.  Instead, in order to authenticate, the cash machine should send the card number and PIN number and ask whether they match.

Visual Studio Express Editions

Saturday, March 15th, 2008

For those of you who don’t know about them, Microsoft have free cut-down versions of several of the Visual Studio languages available to download.

The three main languages are available – Visual Basic Express Edition, Visual C# Express Edition, Visual C++ Express Edition. Also, for developing ASP.NET Websites and Web Services, there is Visual Web Developer Express Edition.

I find that when I am working on an ASP.NET website with some other people, whether they are designers, or just people writing/altering text, it is much easier for me if everyone uses Visual Studio or Visual Web Developer Express.

The main disadvantage of the Express editions as far as I’m concerned is that they don’t work with source control integration. That doesn’t affect individual developers, but when you are working as part of a team, it makes life difficult, especially for non-web based systems. For web based systems, as long as you are careful, it is usually ok, and Visual Studio will normally warn you if you try to save over something that someone else has changed, at least if you are using FrontPage Extensions to access the site.

Extensibility is also not supported in the Express editions, so if you need any extensions or add-ins to Visual Studio, they won’t work.

There are a number of other differences. Microsoft have a full feature comparison on their site.

Type Inference in .NET 2008 Part 2: Turning Type Inference On and Off

Friday, March 14th, 2008
This entry is part of a series, Type Inference in .NET»

To turn on/off Type Inference in a normal .NET project (apart from a website), go into My Project, select the Compile tab, and you can choose Option infer from the drop down list.

To control it per file in VB.NET, you can write at the top Option Infer On or Option Infer Off.

In an ASP.NET website, there is a problem setting it – see this Microsoft Feedback Report. If anyone has any information on how to work around this, please post a comment.

Entries in this series:
  1. Type Inference in .NET 2008 Part 1: What is Type Inference?
  2. Type Inference in .NET 2008 Part 2: Turning Type Inference On and Off
Powered by Hackadelic Sliding Notes 1.6.5

Type Inference in .NET 2008 Part 1: What is Type Inference?

Friday, March 14th, 2008
This entry is part of a series, Type Inference in .NET»

I’ve just been playing with type inference.  This is a new feature in VB.NET 9 and C# 3 (I think it is v3) where you can get the compiler to assign a type to a new variable automatically by using “common sense”.  Note that this looks just the same as weak typing, but it actually isn’t – the variables will be forced to the correct type by the compiler automatically, and intellisense even works!

For example, if you have type inference turned on, you can do this…

Dim i = 0 ‘i is automatically of type Integer
Dim Numbers = {1,3,5,7,9}’Numbers is automatically of type Array(of Integer)

For Each Num in Numbers ‘Num is automatically of type Integer

Next

It might seem lazy, but isn’t that the whole point?

Entries in this series:
  1. Type Inference in .NET 2008 Part 1: What is Type Inference?
  2. Type Inference in .NET 2008 Part 2: Turning Type Inference On and Off
Powered by Hackadelic Sliding Notes 1.6.5

Nullables in VB.NET with ?

Friday, March 14th, 2008

I don’t know whether this is just in the new version of the VB.NET compiler which goes with Visual Studio 2008 or whether I just missed it completely in 2005, but I have just noticed that you can declare Nullables in VB.NET the same way you can in C#.

For example, you can do

Dim CustomerID as Integer?

Note the question mark on the end – this indicates that it is nullable.

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

Upgrading Visual Studio 2005 Solutions to 2008

Friday, March 14th, 2008

Last night I upgraded a Visual Studio 2005 solution to 2008.  It contained 1 Windows Forms executable, 1 Class Library and an ASP.NET Web Forms website.  I was pleasantly surprised to find that Visual Studio did the upgrade with no hassle, and even asked me whether I would like to target .NET 3.5 or .NET 2.0.  For the moment, I selected .NET 2.0, as I will be upgrading it to 3.5 later.  I don’t know if it is so easy for all projects, but it was nice to see how easy it was for me.

Structured Exception Handling in .NET Part 1: Try/Catch/Finally

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

A friend has asked me to write some articles on how to handle exceptions in .NET, so here we go… A little bit of background first: -

Exceptions occur when things go wrong or other exceptional circumstances. They are a bit more complicated than the older error trapping systems, but the complexity lets you do a lot with them. Every Exception is an object, and it is either an object of type Exception, or of a type that inherits from Exception.

In this post, I’m just going to deal with catching a general Exception. I’ll cover what you can do with the Exception class itself, multiple Catch clauses, different Exception types, re-throwing and creating your own Exception classes in other posts.

So here is the way it works. When something goes wrong, an Exception object gets created and “thrown”. This means that the stack rewinds back until it finds something that catches the Exception. Basically, this means that anything that gets called inside a Try clause will be handled if an Exception gets thrown. Not only that, but any functions that that calls etc. on and on either forever or until there is another Try clause that will handle it. “Later” Try clauses take precedence over earlier ones.

This is how to use it…

Try
   Some code
Catch ex as Exception
   Some exception handling code
Finally
   Cleanup code that gets called whatever
End Try

Note that the Finally clause is actually optional – it is fine to leave it out. You would normally use it if you wanted to close a database connection, file, stream, network connection etc. or some other form of cleanup code. Also, you can have multiple Catch clauses, but I’ll cover that later.