Posts Tagged ‘Coding’

The .NET Stack Part 1: How The Stack Works

Thursday, March 13th, 2008
This entry is part of a series, The .NET Stack»

A stack frame is the way that .NET keeps track of what sub/function/method/whatever you want to call it you are in, which one called that, which one called that one etc. all the way up to the main function that runs the whole program. It also keeps track of where it is up to within each function.

For example, if you have the following code (in VB.NET as usual): -

1  Module Module1
2
3      Sub Sub3()
4
5     End Sub
6
7     Sub Sub2()
8         Sub3()
9     End Sub
10
11     Sub Sub1()
12         Sub2()
13     End Sub
14
15     Sub Main()
16         Sub1()
17     End Sub
18
19 End Module

If we were to stop at Line 4, the stack frame would look something like this…

Sub3:4
Sub2:8
Sub1:12
Main:16

Of course it is a bit more complicated than this, but that is the gist of it. The same idea applies for most other languages.

Entries in this series:
  1. The .NET Stack Part 1: How The Stack Works
  2. The .NET Stack Part 2: The StackTrace Class
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.

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.

Black Box Coding

Wednesday, March 12th, 2008

I’m a big fan of designing my code to work like black boxes. The idea is that you can interact with each object without ever having to worry about how it works inside. You should never need to look inside. It should have all the functionality you need from the outside. My ideal way of building up a framework is by building layers of objects based on this principle on top of each other.

For example, if you build a Windows Forms UserControl, whenever you use it, you should never need to know what controls are inside it. Everything should be accessed via a set of properties and methods on the object, not via accessing the child controls.

Nullables in .NET

Wednesday, March 12th, 2008

I don’t know if you’ve ever had a problem with trying to store a null value in a value type in .NET.  If you have, you may be interested to know that there is a generic type wrapper in .NET which can sort the problem out.  It is called Nullable.

You can use it like this (in VB.NET)

Dim n as Nullable(of Integer)

You can then use the HasValue property on it to see if it has a value, and the Value property to retrieve the value.  You can also assign to and from it as normal, including assigning Nothing/null to it.

Distinct in Linq to SQL

Wednesday, March 12th, 2008

I was trying to get Linq to SQL to do a Distinct query today, and found that it wasn’t quite so obvious how to do it.

This works: -

From item in table Select field Distinct

This doesn’t: -

From item in table Distinct Select field

Neither does this: -

From item in table Select field.Distinct

Note that all 3 of these appear fine with Intellisense

Microsoft .NET Framework 3.5 in Windows 2000

Wednesday, March 12th, 2008

Microsoft don’t support using .NET 3.5 in Windows 2000, which is particularly annoying, especially if, like me, you have a client who has 8 out of 12 PCs still using Windows 2000.

So, I decided to have a go and see if I could find any way of doing it. This is completely unsupported, and if it breaks anything, don’t blame me, but it is actually fairly straightforward and it seems to work fine to me (although I doubt it works with things like WPF, but if anyone tries it, please leave a comment to say how it goes)…

  1. Make sure you have Windows 2000 SP4 installed
  2. You may need to install KB 835732 before step 3
  3. Install .NET Framework 2.0 Service Pack 1

Now, there are 2 ways of continuing. Either you can copy over all the .NET 3.5 assemblies (you’ll have them on a .NET 3.5 PC in the Program Files\Reference Assemblies\Microsoft\Framework\v3.5 folder). You can either dump them in the application folder or probably register them in the GAC (not tried it, but it should work).

Alternatively, try running your application. You will probably get a load of AssemblyReferenceFailedExceptions. Copy in the required DLL from the above folder into your application folder for each one.

It should all work now!