Archive for March, 2008

String Processing With Linq

Wednesday, March 26th, 2008

It just occurred to me that since a String can be treated as an array of characters, you should be able to do a Linq query on it, so I tried it, and it seems to work quite well…

Dim Test = “The cat sat on the mat”
Dim Test2 = (From c In Test Select c Where c <> ” “).ToArray()

Personally, I much prefer string processing this way than with Regular Expressions.  Granted, it isn’t so versatile and is undoubtedly more processor intensive, but if you aren’t bothered about the clock cycles and it will do what you want, it is much easier to code and read!

Microsoft .NET Framework 3.5 in Windows 2000 Test 1

Wednesday, March 26th, 2008

I mentioned earlier how to install .NET framework 3.5  in Windows 2000.  I have been testing this on 7 Windows 2000 PCs, and I have successfully managed to get them to do a simple Linq to SQL query with the method that I mentioned.  I can’t speak for WPF or anything, but I am currently only interested in Linq.  I will post more in future when I have the live system in place.

On Reflection Part 2: GetType

Monday, March 24th, 2008
This entry is part of a series, On Reflection»

In order to use reflection in .NET, you need to get an object of class Type.  I will explain how to use the Type object later, but first, you need to be able to get it.

There are 3 ways to get a Type in .NET.

  1. VB.NET has a GetType keyword which takes a class as a parameter.  eg. you can writeDim T as Type = GetType(String)

    This is not really a function, because you are passing in a class, not an actual value, and not even the name of a class!  C# has a similar keyword, which I think is called typeof.

  2. All .NET objects inherit from Object.  Object has a function defined on it called GetType, so all you need to do is eg.Dim p as New Person()
    Dim T as Type = p.GetType()

    Note that if you have an Interface reference you may need to cast it using the CType operator in order to access the GetType function.  You should be able to cast any .NET object to Object.

  3. You can get a Type by name from an Assembly.  I will post details on this in a later post on Assemblies.

Structured Exception Handling in .NET Part 5: Throwing new Exceptions

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

As I mentioned earlier, throwing Exceptions can be a very useful way of notifying the appropriate code that something unexpected happened.  In order to throw an Exception, first you create an object of type Exception, or a class which inherits from Exception if there is a more appropriate one.  You should also set the Message property, which can usually be done from a constructor.  You can actually do all this in a single line.  eg.

Throw New Exception(“Invalid username”)

Some Exception classes will want additional data, depending on the type of Exception that you are notifying about.  It is worth looking around the .NET framework or any other set of code that you are using to see what Exceptions are available.  I will post up some common ones in a later post.

Affiliate Programs

Thursday, March 20th, 2008

If you want to make some money from an existing website or forum, a good way of doing it can be to sign up to affiliate programs.  These work by you linking to companies or specific products and services that may be of interest to your readers.  The pay depends on the affiliate program.  For some programs you get paid per click through, but most you need to actually get them a sale.  There is a company called TradeDoubler who run a very large number of affiliate programs with some very big names.

Attributes in HTML

Wednesday, March 19th, 2008

Another thing to remember if you want to be XHTML compliant (see Closing all Tags in HTML) is to make sure that all your attribute values are in quotes. eg. height=”100″ and not height=100.

Structured Exception Handling in .NET Part 4: Re-Throwing Exceptions

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

Sometimes when you have caught an Exception, you want to do a bit of processing and then throw the Exception on again, or perhaps check the Message property to decide if you can handle it, and if you can’t then you throw it again. This is actually quite easy – you just use Throw, with no parameters.


Catch Ex as Exception
   If Ex.Message="Connection Failed"
   End If
End Try

One thing that you really don’t want to do is to Throw Ex

Google AdWords

Wednesday, March 19th, 2008

If you want to bring some (more) visitors to your website, Google have a keyword based system that they use to show adverts next to their search results and on people’s websites (see down the right hand side for example). You only have to pay for any visitors that you get – you don’t pay each time the advert is shown. Also, because it is keyword based, you can target people who are searching or viewing a website about something related to your product/service. This means you are much more likely to get a sale.

The .NET Stack Part 2: The StackTrace Class

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

The StackTrace class is used to get a collection of StackFrames.  A StackFrame contains information about a single point in the Stack.  I’ll come back to that in a later post.  The StackTrace class has multiple constructors with various parameters: –

The Default Constructor (no parameters)
Gets the current point in the stack in this function that you are calling it from.

fNeedFileInfo as Boolean
Whether we need the line numbers, file names etc in the stack trace (if available).

skipFrames as Integer
The number of frames to skip.  1 starts with the function that called this function. 2 starts with the one that called that function etc.

e as System.Exception
An Exception to build the stack trace for.

frame as StackFrame
A frame that this stack trace should contain.

targetThread as Thread
The thread to get the stack trace for.

Since there are 10 different constructors in .NET 3.5 at the time I am writing this, there are plenty of combinations that these can be used in.

The StackTrace class has several members that are of interest: –

The number of frames that this StackTrace contains.

GetFrame(index as Integer)
Get a frame at a certain position.  See skipFrames (above) for what the position means.

Returns an array of the frames in the StackTrace.

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

.NET Compilation Part 1: Profiles

Tuesday, March 18th, 2008
This entry is part of a series, .NET Compilation»

You may have noticed that in Visual Studio, most projects start off with Debug and Release profiles. This is used to control the way that the code is compiled.  By default, Debug mode creates a pdb file and compiles the code to allow it to be used.  A pdb file contains information which will help you debug the program, such as information about how the source code maps to the compiled code.  This means that eg. when you get an Exception, the StackTrace property will give you information including line numbers.

On the other hand, a Release profile is much more optimised, both in terms of file size and code speed.  However, it is entirely up to you whether you ship a Debug application.  I wouldn’t recommend it for a large number of users, but for a constantly changing application in a small office, full stack traces with line numbers can be useful to find bugs that are difficult to trace.