Wednesday, December 27, 2006

Throw vs Throw ex

A common mistake a lot of new .Net developers do is using Throw ex instead of just Throw. The difference between Throw ex and Throw is very simple but can cause major headaches when it comes to maintenance. If you use Throw ex it overwrites the stack trace this makes it very hard to find the original line of the thrown exception as you can see in the example I have provided.
Public Class Form1
Private Sub ThrowsExceptions()
Throw New System.Exception("Exception Thrown") 'Line 3
End Sub
Private Sub JustThrowIT()
Try
ThrowsExceptions()
Catch ex As Exception
Throw 'Line 9
End Try
End Sub
Private Sub ThrowEX()
Try
ThrowsExceptions()
Catch ex As Exception
Throw ex 'Line 16
End Try
End Sub
Private Sub ThrowEX_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Try
ThrowEX() 'Line 21
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub

Private Sub JustThrowIt_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Try
JustThrowIT() 'Line 29
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
End Class

When you click the Throw ex button you get only get references to two lines 16 and 21 the orginal line the exception is thrown is missing(line 3).



When you click the Throw button you get the full stack trace including the line of the orginal exception that was thrown.



Download Source.
throwDemo.zip (47.73 KB)
#     Comments [4]  
kick it on DotNetKicks.com
 Friday, December 22, 2006

My Wish List

I think one of the hardest parts of becoming a successful and educated programmer (besides just sitting down and coding) is finding good tools and resources to learn from and use. I put a lot of effort into finding these. In order to find these tools and recourses I look toward programmers I consider successful, most of which are listed under the Developer and Technologist Journals on my site. So far I have found frequent references to the following books and tools, these are on my to read/buy list.

Book Wish List
Tools Wish List
Tools and Resources I Own & Recommend
  • Reflector for .NET - Reflector is the class browser, explorer, analyzer and documentation viewer for .NET. Reflector allows you to easily view, navigate, search, decompile and analyze .NET assemblies in C#, Visual Basic and IL.
  • Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers.
  • Head First Design Patterns:  I got this book for Christmas, thank you Minette and Jesse, it is an entertaining read and easy to understand.

#     Comments [0]  
kick it on DotNetKicks.com
 Tuesday, December 19, 2006

Using an Enum as the DataSource for a ComboBox

While working on my spell check app Dyslexic Helper I had to use an Enum (System.Windows.Forms.keys) as a data source for a Combo box I found this to be very easy and very useful. Here is an example of how to do this.

Public Class frmExample
'Enum to fill the combo box with
Public Enum Names
Jim
Bob
Dave
Sam
Tim
Jon
Suse
Ann
Sassy
Silvia
End Enum

Private Sub FormExample_Load(ByVal sender As Object, _
ByVal
e As System.EventArgs) Handles Me.Load
'fills the combo box with the names from the Enum Names
Me.ComboBoxExample.DataSource = System.Enum.GetValues(GetType(Names))
'selects the last selected name in the combobox
'My.Settings.Name is defined in the project/properties/settings as a string
Me.ComboBoxExample.SelectedIndex = Me.ComboBoxExample.FindStringExact(My.Settings.Name)
End Sub

Private Sub frmExample_FormClosing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'saves the selected name in settings as form is closeing
My.Settings.Name = Me.ComboBoxExample.Text
My.Settings.Save()
End Sub
End Class

You can download the source here.

CboExample.zip (50.48 KB)
#     Comments [0]  
kick it on DotNetKicks.com
 Monday, December 18, 2006

System Wide Spell Check

Dyslexic Helper is a spell check that can be used anywhere in windows.

I have a bit of a problem spelling due to my dyslexia, to help me with this I wrote an application that spell checks copied text. This application can be run anywhere in windows by a combination of keys or the application menu, accessed via the system tray. Feel free to use this application as much as you like. If you want to show your appreciation you can donate few dollars to help me continue my education.

To use this application just follow the steps below.

  1. Download Dyslexic Helper 
  2. Unzip the File named DyslexicHelper.zip
  3. Once unzipped open the newly created folder and run the setup.exe
  4. This setup will install the necessary files to run Dyslexic Helper. Dyslexic helper will also place a shortcut file in your programs folder and another in the startup folder.
  5. Once the application is installed run it start>All Programs>Dyslexic Helper.
  1. Now that Dyslexic Helper is running, to use it you just need to highlight any text in windows copy it and hit the Windows key + F7. This hot key combination can be changed through options in the menu.
  1. Finally correct your spelling and paste it where you need it.

This application is written by me using VB.Net as well as C# and the entire source is availble here, if you would like to see how it works. The spell check control I used is called NetSpell. I have changed this control a little bit but mostly just for looks. The system wide hot key control I found at The Code Project I modified it a little so I could change the hotkey at runtime and return the current selected hotkey as a text value.

#     Comments [0]  
kick it on DotNetKicks.com
 Wednesday, December 13, 2006

XNA for everyone

I just got a really interesting email from Jon Schwartz about a C# program that he has been working on. It's called Phrogram, this is a simple language that is interpreted through the CLR, it is designed for educating people on programming. Jon Schwartz writes,

“In a nutshell, Phrogram makes it fun and easy to program games and graphics – including, for instance, 35 simple instructions to fly a 3D model around in a 3D skybox, controlled with an Xbox controller.  10-year olds can do this, and grown-ups who have never before programmed.”

Phrogram will soon have an add-in that will allow you or your 10 year old to compile your creations to your Xbox 360. Now what I am looking forward too is these same guys that made it simple enough for 10 year olds to write games, are partnering up with Garage Games. They are using TorqueX game engine as the rendering engine for there new product Phrogram Indie Game Developer coming next year.

Check out these links for more details

Phrogram

Jon Schwartz’s Blog

Channel 9 Video on Phrogram

TorqueX

#     Comments [0]  
kick it on DotNetKicks.com

XNA Fun – Develop Your Own Games

If you are like me and you have always wanted to develop games now is a great time to start Microsoft has put out an exciting new product called XNA. XNA allows you to develop your own game in manage C# code then compile it and play it on your Xbox 360(there is a $99 yearly subscription to compile on Xbox). To get started you will need to do the following:

1.      Download and install Visual C# Express (You cannot use the other editions but express will run fine along side them.)

                        System Requirements for Visual C# express:

  • Operating Systems: Windows 2000 sp 4, Windows XP sp 2, Windows Server 2003 sp 1, Windows x64, and Windows Vista.
  • Processor: 600 MHz or faster recommended 1 GHz
  • Ram: Min 192 MB recommended 256 MB
  • Hard Drive: Min 500MB Full 1.3 GB (Includes MSDN Library)

2.      Download and install latest DirectX runtime, the full DirectX is not required.

3.      Download and install XNA Game Studio Express 1.0 . To run your games on other computers the target computer   must have XNA Framework to execute.

Video Card Requirements:

  • Direct3D 9.0 video card capable
  • Support Shader Model 1.1 recommended support for Shader Model 2.0

That is it you are ready to get started.

Helpful XNA Links

Rory Blyth shares his excitement through his blog and his work at channel 9.

Channel 9 video – Looking at XNA part 1

Channel 9 Video – Looking at XNA part 2

Rory’s Blog - XNA - Restoring the Intimacy and Fun of Game Development

XNA Forums

LearnXNA.com – This forum contains some useful video tutorials.

MSDN XNA Forums - XNA Framework, XNA Game Studio Express

XBOX 360 Homebrew  - this site really is a must, has a great community and growing.

Dedicated XNA sites

XNA Development: Dame Development for the masses

The Microsoft XNA Home

XNA Team Blog at Microsoft

XNA Game Studio Express Documentation

XNAtutotial.com the name says it all

The amount of information available is overwhelming and it is only release 1.0 this is really going to be huge truly a revolution in the gaming world.

 

#     Comments [0]  
kick it on DotNetKicks.com