Steven Nagy .NET

Thursday, 31 May 2007

Moving From Beginner to Intermediate

I've always thought that developers can fall into two categories. Category A does their 9 to 5 job, then goes home and plays around with all the things they don't see at work. Category B does their 9 to 5, then goes home and hangs out with friends, reads a fiction book, or watches TV all evening.

Ok so not everyone falls into exactly those two categories, but you can see what I am insinuating. Some developers have a natural drive/desire for knowledge, and they'll find it. That's what fuels them, drives them to install the Orcas Beta, get into work early to catch up on all their blogs, or sends them to their local community user group.

Don't think for a second that you can't fall into category B in be a good developer. That's definately not true, but I think a proper analysis would show statisitcs in favour of the fact. So quite possibly YOU have that drive, and you want to take your skills to the next step. You're a junior somewhere, and you're not getting the innovation/learning you think you need to get the good jobs. What do you do?

Well if you put yourself in category B, I can't help you. But if you have the desire/drive, here's some things to consider:

Design Patterns
Buy a good patterns book like those I listed in an earlier post. Learn why patterns are important and learn the most popular ones. Quite interestingly, I did a technical test for my new job, and one of the questions was 'Describe a design pattern that you like - NOT "Singleton", "Factory" or "Provider"' ! Some companies value this knowledge.

Refactoring
Learn about refactoring especially. Even learning design patterns for refactoring can be extremely useful!

Blogs
Subscribe to a 'healthy' amount of blogs. It helps if you have something new to read every day. You might find some authors who interest you, or you might stick to the big names like ScottGu. When I say 'healthy' I mean not so many that you can't keep up, and that you are getting duplication of topics. And not too few that you aren't getting a post or two per day. Its good to go to work and discuss a new post about some technology or other (if you have that kind of environment!)

Podcasts
I personally spend one night a month just downloading a whole heap of podcasts, then burn them as CD audio so I can listen to them in my car! This means that when I'm sitting in traffic, I'm actually learning. I can suggest www.asp.net for .Net specific, or IT Conversations for general IT topics. I find webcasts useful for learning about a specific topic, but podcasts can be listened to while you're on the move, walking the dog, on the bus, etc.

Lately Microsoft have been releasing a new technology at a rate of what seems like, 1 a day. So its almost a full time job just keeping up! If you want any more ideas about how to progress your career, I might be able to help.. just ask!

Labels: , , , ,

Sunday, 27 May 2007

Beginning with Vista Sidebar Gadgets

If you if the new version of Windows (Vista) you'll be aware of the Vista sidebar and the gadgets that sit within it. There's a few to choose from.. I like the RSS reader and the weather gadgets from Microsoft. I also have a CPU monitor that monitors both the cores in my dual core CPU.

Microsoft have made what I believe to be an extremely easy to use API. All you need is some HTML skills (or you can use Dreamweaver or Expression Web) to create your look and feel, and some javascript skills to program against the API.

Yes you heard right... no managed code. The sidebar runs kind of like a mini browser. In fact Scott Barnes mentioned that it uses some of the rendering engine from IE7. The real cool thing is that if you can run it in a browser, you can run it in a gadget. This means you can use Flash or Silverlight. That's right! Flash powered sidebar gadgets.

Ok so for people like me who are hardcore developers, we LIKE managed code. Give me C# over Javascript any day! Well I did mention Silverlight right? So you right your sidebar in Silverlight 1.1 using C#... woohoo! The only downside is that if you want to use the gadget API to save data or in some way interact with the API, you need to write javascript. Not sure how to broach this just yet, but I'll let you know if I find a post about it.

So I've gotten interested in gadgets because of a competition at Microsoft. I'm writing a gadget that displays information about the surf at your local beach (as long as your local beach is in QLD Australia, because the competition must have a QLD theme!). I've done my UI design since I believe that you have to have a nice looking gadget because people will have this on their desktop permanently. In fact, all I've done is design, and some research about where to get the wave heights from. Unfortunately I'm having no luck with the latter. If anyone knows a reliable web service or web page with this information, I'd be most greatful (and I'll let you come over and play on my XBOX when I win it).

Here's some screen shots of my UI design:






I don't really want to go into too much detail but here is the basic steps to creating and running a gadget:

1) Find your gadget folder. Mine is:
C:\Users\Steven\AppData\Local\Microsoft\Windows Sidebar\Gadgets

2) Create a new folder for your gadget, example: SurfWatch.gadget
(I think the .gadget part is essential, not totally sure)

3) Create a HTML page for your gadget (any name is fine, mines: SurfWatch.html)

4) Create a gadget.xml file, which specifies which HTML file is used for your gadget

I don't really want to say HOW to do it all, because its all readily available at this URL which is pretty useful for learning the basics. Besides, my last post was quite large so lets keep this one short, eh?

If you are in QLD Australia, you should enter the competition. Good luck with gadgets!

Labels: , , , , ,

Beginning With Events

Before we start, I just want to say that I think understanding how to create and consume events is extremely important as a developer. I was surprised that none of the developers (including the development director) knew how to create events and how useful they can be.

As developers, we're use to consuming events on a daily basis. For example, one of the most common events we consume is 'Page.Load'. Other ones we use for a page include 'Init' and 'PreRender'. Then of course are the control events such as 'Button.Click' and 'DropDownList.SelectedIndexChanged'.

So consuming event's is pretty easy. The button control has a click event that can be hooked up as follows:
myButton.Click += new EventHandler(myButton_Click);

And of course, there is a method called 'myButton_Click' that gets 'fired' when the event occurs. You can think of the 'myButton_Click' method as 'subscribing' to the event. And with all subscription models, more than 1 method can subscribe to an event. So the following code is perfectly valid:

myButton.Click += new EventHandler(myButton_Click);
myButton.Click += new EventHandler(anotherMethod_Click);

Naturally both methods must exist, otherwise you get a compile error. You can also programatically UNSUBSCRIBE a method from an event with the syntax:

myButton.Click -= new EventHandler(myButton_Click);

So lets look a little closer at this code. What does the 'EventHandler' part mean?

Well take a look at the 'myButton_Click' method. Here is its signature:

void myButton_Click(object sender, EventArgs e)

If we were to change this signature in any way (other than the parameter names) we would get a compile error. This is because the 'EventHandler' defines this signature, and therefore any method that subscribes via that declaration must implement the exact signature. Under the hood, when the button raises the Click event, it is sending information such as the sender and the event arguments. If you are subscribing to that event, then you must also receive that information. Therefore, the EventHandler states the rules of what the event will raise and what the subscribing method will handle.

Here's another example. In ASP.Net there is a control called 'GridView', and one of its events is 'PageIndexChanging'. To handle that event, code might look like this:

myGrid.PageIndexChanging += new GridViewPageEventHandler(myGrid_PageIndexChanging);

As you can see, the 'contract' for the subscription is called 'GridViewPageEventHandler'. When implemented, this ensures the method handles some different arguments. Its the event that defines what event handler to use for its signature, and all subscribing methods must implement that signature. When writing events, you can use these existing handlers, or you can create your own. More on this later.

Now we understand more about consuming events, lets create one. Imagine an object called 'TrafficLight' which simply rotates between the colours as a normal traffic light would. Internally it has a timer that after X number of seconds changes the colour. When the traffic light changes colour, it should 'raise an event' so that the rest of the application is aware that the change has occurred; perhaps we have a visual control that would show the traffic light and it wants to subscribe to the traffic light's colour changing event.

So creating the event is very easy. We will use an existing event handler:

public event EventHandler LightChanged;

This declaration states that there will be an event called 'LightChanged' and that it will use the method signature as defined by 'EventHandler'. Naturally there's not much point making the event private because nothing outside of the class could subscribe to it!

In the code, we would be raising the event when the timer ticks (Timer.Elapse) because that's when the light colour changes. Our code might look like this:

if (LightChanged != null) LightChanged(this, new EventArgs());

First lets look at the IF statement. This is a simple safety check that says not to raise the event unless something has subscribed to it. If no one is subscribing to an event and that event gets raised, a runtime exception will occur. So we check that its not null first. Then we simply call 'LightChanged' as if it was a method. And guess what? It IS a method! In fact, its the method that subscribed to the event. LightChanged is just a placeholder for functionality that might be added later.

Ok so the last thing we need to think about is that the information we are raising in the event is not very useful to the subscriber. We should actually include more helpful stuff, like the new light colour. So this is where we can define our own event handler:

public delegate void LightChangedEventHandler(TrafficLight sender, ColourStatus NewColour);

As we know from previous usage, the LightChangedEventHandler just defines a signature that the event and subscribers adhere to. In a way its similar to the contracts defined in interfaces. The keyword 'delegate' is special here because it says that this method signature is like a 'type'. The actual event instantiates that type.

So we now need to change the declaration of the event:
public event LightChangedEventHandler LightChanged;

And change how the event gets raised:
if (LightChanged != null) LightChanged(this, CurrentLightColour);

Well that's it. Best to have a play. I've completed the TrafficLight solution for you to see how it all comes together (VS2006 / .Net 2.0). I apologise in advance for using the Traffic Light example. I know its been done to death but it really is an excellent example of events. Feel free to post any questions/comments here as usual.

Labels: , , , ,

Friday, 25 May 2007

House Keeping

Just a quick entry to let you know what happened today.

I resigned from my job at Speedwell, giving 4 weeks notice. My new position will see me using the latest technologies in an innovative environment. I'll be able to get my hands dirty in WPF and WCF amongst other things, which should also aid in my Silverlight learning. I'll be able to post about more modern MS technologies, which means you all win as well!

However my aim this weekend is to do a post on events (beginner level, creating and consuming) and if I have time, http modules. I'm trying to claw back the developer aspect of this blog!

You can suggest anything you would like me to post about in the comments of any post.
Feel free to suggest anything, not matter what the difficulty level is. If I can't find an answer, I'll talk to the industry and find out for you!

Labels: , , , ,

Thursday, 24 May 2007

Reflector for Silverlight?

Silverlight 1.1 runs managed code. This means unlike 1.0 you can not just view source and see how the app works. Normally you would have analyse the files and find out where the dll resides, request the dll manually, then use reflector to view the code.

This guy Ernie Booth has written a plugin for Reflector that lets you point at a Silverlight app, and it does the rest! Check it out here.

Labels: ,

Tuesday, 22 May 2007

Introduction to Globalisation

Introduction to Globalisation

Tonight I attended a presentation as part of QMSDNUG and learnt a little about globalisation. I was quite impressed by VS2005 capability and thought it required a little further investigation.

I'll run through a very brief, simple example of using cultures to control resources on an ASP.NET page. To start, open a new web site in VS2005. You get the usual 'default.aspx' open by default. Drag a label onto your form, name it and give it some text. If you were to view the HTML, it could look something like this:

<asp:Label ID="lblWelcome" runat="server" Text="Welcome"></asp:Label>

One thing about dealing with globalisation is that you will NEVER directly insert text into a form. Always use a label, even for supposedly static text. This allows us to change the text, either programatically, or through the globalisation techniques I am about to show you.

Switch back to design mode. If you click on 'Tools' you will see a menu option called 'Generate Local Resource'. If it is not selectable, you need to click somewhere on your page, such as your label. Click 'Generate Local Resource'. A window pops up at the bottom of the screen indicating something about creating the resource.

When it's finished, there will be some key changes to your solution now. First, in solution explorer there will be a new special ASP.NET folder called 'App_LocalResources' and in it will be a new file called 'Default.aspx.resx'. This is the resource file for your default.aspx page. We'll look at it in a second.

Now, go back to view the HTML of your label and it might look something like this:

<asp:Label ID="lblWelcome" runat="server" Text="Welcome" meta:resourcekey="lblWelcomeResource1"></asp:Label>

Notice the new attribute: 'meta:resourcekey'. This indicates what resource key will be used for this server side control. In fact that key can be found in the new resource file. Open the .resx and you will see that there are some (3) properties for that key 'lblWelcomeResource1' such as Text and Tooltip. By default they have some values in it. Lets change them a little. I'll change the text to 'Welcome everyone' and the tooltip to 'This is english'.

Run the application, and you see that the text on the label is not 'Welcome' as expected, but 'Welcome everyone'. The default text is ignored, and the resource file attributes are applied.

So now lets make this truly culture specific. Duplicate the 'Default.aspx.resx' (copy and paste to same folder). Name it: 'Default.aspx.en-US.resx'. Now open the new resource file and change the text to 'Welcome PARTNER' and the tooltip to 'This is US english'. You'll notice that the key is still the same for the resources in this file. This is a good thing because it means the label on our form will use this key for this particular culture (en-US).

Run the application, and you see... NOTHING HAPPENS! That's a good thing because, if like me you are in Australia, our browsers are configured for Aussie english, ie. en-AU

Keep the browser running, and go to internet options, Languages, and remove en-AU and add en-US. Close modal windows, and refresh browser. "Welcomer PARTNER".

So you see how this works. When the http headers pass the user's culture, .NET loads the resource file for that culture. If it doesn't find that culture it defaults to the default culture settings. In fact, you can specify general languages, such that if it doesn't find 'en-US' it will fall back to a 'en' and if it doesn't find that, will fall back to the default (invariant) settings.

This is just the surface of what you can do, but here's a few quick pointers that might save you some hassle:

  • You can control lots of properties of items through resource file

  • You can add your own entries into the resource files

  • The resource files can be accessed programatically (when I learn it, I'll show it)

  • If you add more controls to your form, you need to regenerate resources again

  • Multiple controls can share the same resource key (just set it to the key you want in the HTML)

  • If you delete a control, it does not delete the resource key (because of previous point)

  • It is possible to store resources in adatabase by custom implementing the resource provider (too advanced to discuss right now)



Here's an article that will give you more info on CodeProject.

Have fun globalising, localising, and internationalising!

Labels: , , ,

Sunday, 20 May 2007

Silverlight: TicTacToe final

I've posted version 3 (final) of TicTacToe for your reference. Demo and source code is all available.

I've tidied up the UI so it doesn't like totally n00bish. I've removed the winning line and incorporated some animations (otherwise, what's the point right?) .

It's been a good experience and a good tool for learning the new framework and capabilities. Here's a summary:
  • Not all XAML elements will work in Silverlight. For example, in Expression Design I created a CROSS and gave it a bevel. This renders with a Bitmap render expression in the Xaml but it causes runtime exceptions in silverlight... so no bevels!
  • Grouping your objects into canvas' helps management of those objects. In TicTacToe, once I started grouping and hooking up events on the canvas that groups the objects rather than the objects themselves, things became easier.
  • If trying to accomplish something in managed code, checkout the WPF reference on MS site. Although the code won't be identical for Silverlight, it will open up your eyes how to approach your problems.

My next Silverlight project will be something more interactive, flamboyant, and functional. This means it will be a bigger project as well; hopefully my trial versions of Blend and Design won't expire! Any suggestions welcome.

In the meantime, I'll also look at writing a Vista gadget and posting that code as well. I attended a demo from Scott Barnes the other night. He had some technical difficulties, but it looks extremely easy to create gadgets.

Labels: , , , , ,

Friday, 18 May 2007

I'm a Twitter

That's what my mum use to say... well now its official.
Check my blog to see what I'm up to via the twitter badge on the right.

Saturday, 12 May 2007

TacTacToeToo, I mean two

In my last post I brought to life my first Silverlight application, a TicTacToe (Naughts and Crosses) game. As you would expect there were a lot of flaws. It was hastily constructed with no real knowledge about the Silverlight framework. It's also important to illustrate that I am not overly familiar with WPF; since .Net 3.0 beta, I have been working purely in web circles (and still am).

But yesterday I found the WPF reference on MSDN; how relevant! While some things are not quite the same, there is enough similarity that the documentation can help solve most simple learning problems. For example, I found the correct classes to perform a rotate and translate in C# (not in Xaml) in the WPF documentation.

Duh. I should have realised this earlier, but I was too caught up trying to find answers on the Silverlight site and in the SDK. So now I have progressed and am starting to get a better understanding of how things work.

Back to TicTacToe. I've released version 2, which looks a lot like version 1, only that I have refactored code and design somewhat. The only important visual change is the line that appears when you win. This is the object that gets a rotation/translation at times (when you win on a diagonal or vertical) so that code might be of interest to you.

The next version will address the buttons, which really aren't close to being buttons currently. Presumably there are actual buttons, or reusable controls in the May Futures release that might be useful. Oh, and the next version will have an unbeatable computer player! Finally, I want to create some animations as well.

Head to my TicTacToe page for more information on what has actually changed.

Wednesday, 9 May 2007

My First Silverlight App

I like to learn by doing. So after watching the ScottGu video on the
Silverlight website I decided to learn Silverlight by creating a Naughts and Crosses game (Tic Tac Toe). You can see my initial attempt here.

So I suppose its pretty crappy to look at, but hey, I'm no designer. And its my first app, so what do you expect??? I didn't want to post it until I could go back and make it better. It needs serious refactoring, there's missing functionality, and its probably doing things the "wrong way".

But Scott Barnes encouraged me to publish it now so I have. The idea now is to display the flawed project, and then post each iteration as it improves. At the end of each iteration, I'll post details about the changes and what improved. The focus won't just be about Silverlight specific functionality. For example, the way the CROSS control is loaded is purely hardcoded. Ideally there should be a dynamic way to load different CROSS Xaml files depending on user selection, such that a designer could just add a new Cross object from Expression Designer into a /Cross/ folder.

Hopefully as we progress on this project we'll come to realise some new design patterns for creating functionality that is friendly to designers and developers both, something that might not be so well defined because such integration hasn't existed before now.

So I am completely open to suggestions about how we progress from here. Oh, and to contributions too.

Labels: , , ,

Monday, 7 May 2007

Silverlight Events

Just a short post about Silverlight events.

When writing Silverlight apps using Orcas and Silverlight 1.1, there are 2 key ways that you can hook up events.

The first way is to specify the event directly in the Xaml:

<canvas mouseleftbuttonup="ClickMeButton_MouseLeftButtonUp" background="Red" height="30" width="100" name="ClickMeButton">

The second way (as demonstrated in the ScottGu video on the Silverlight.net site) is to do it in code:

ClickMeButton.MouseEnter += new MouseEventHandler(ClickMeButton_MouseLeftButtonUp);

In both cases there would be a resultant method to deal with the event like in all event driven applications.

I think most people are aware of the latter approach because of the video but the former approach also lets you keep your code behind a bit more tidy.

ende

Labels: , ,

Sunday, 6 May 2007

Vista: The View from Here

I wanted to post a little bit about my experiences with Vista.

Ok so I had a duel boot transition period while I set myself up on Vista. I didn't want any downtime so I kept XP running on one partition, and I decided to install Vista on another.

XP was ALREADY duel booting: I had a trial of Windows Media Centre 2005 running on another partition already and so was receiving the usual boot menu at startup. Since I bought Vista Ultimate which includes Media Centre, I decided to blow away that partition and use it for Vista.

Anyway after the install, my machine now asks me at boot up if I want to run:
1) Windows Vista
2) An earlier version of windows

Because I was still running my life from XP, choosing option 2 then presents me with my OLD bootup menu:
1) Windows XP
2) Windows Media Centre

Weird! Media centre is gone because of the Vista install. I guess its not very good at tidying up the MBR. What's worse is that I later decided to merge 2 partitions into 1 to install Vista again. I don't own partition magic, so was forced to blow away the partitions and start again. Now I get the following menu at startup:
1) Windows Vista
2) Windows Vista
3) An earlier version of windows
... and if you press 3, you get my old boot menu.

Naturally all this is not a big deal, but rather silly none-the-less. No doubt there are suitable records to edit to fix this. I'll find them sooner or later.

All in all, I quite like Vista. I especially like the sidebar and associated gadgets. There is an RSS reader that shows the feeds you have setup in IE7. There's a nice little picture show that cycles continuously, and there's also a weather gadget that shows Brisbane weather and temperature (although yesterday it said it was 22 degrees when I was sweating like a pig). You can download more gadgets as well... I found a nice simple CPU metre which is designed for dual core CPUs; it shows both cores, as well as RAM usage.

It's worth mentioning here that my RAM usage is sitting at over 1gb... and that's with only this IE window open. Vista is damn hungry!

One final thing. The commercial's that MAC have been showing on TV; you know the ones, with the 2 guys talking. Well there's one where PC comes to a "sad realisation" about how annoying his security software is. I can tell you that it is quite true... the amount of times during install that I got asked "Cancel or Allow" was amazing; having said that everytime it happened I was quite entertained, simply because of those Mac commercials. For me, it seems that those commercials softened the blow.

All up, I'm pretty happy. I'm still learning how to get around the OS and options. Naturally some software doesn't work, but nothing too stressful.

Labels: , , ,

Thursday, 3 May 2007

Visual Studio Orcas Beta

I only downloaded the March CTP about 2 weeks ago. Then last week Microsoft released Beta 1.
Its not like I'm complaining... I have 20gb a month so whats the problem?

http://www.microsoft.com/downloads/details.aspx?FamilyID=5d9c6b2d-439c-4ec2-8e24-b7d9ff6a2ab2&DisplayLang=en

That's it. Download it. There's also a white paper on Orcas:
http://www.microsoft.com/downloads/details.aspx?FamilyId=17319EB4-299C-43B8-A360-A1C2BD6A421B&displaylang=en

You can find Virtual PC versions floating around for the March CTP but even more interesting is that you can also download Beta 1 of Team Foundation Server and Team Suite as Virtual PC images as well.

Personally, I'm looking forward to LINQ, XLINQ, and intellisense for Javascript amongst other things. ASP.NET Ajax is also fully integrated and supported in Orcas. Check the white paper appendix for a full list of all the new features.

Going with this release is the Beta release of .Net Framework 3.5. I must say that I am a little lost when it comes to Microsoft's version numbering. Anyway, no need to install all those extensions floating around... Just go for 3.5. Suffice to say that there are a lot of cheaper hosting companies that do not support .Net 2.0 yet (well mine anyway!) let alone, 3.0, or 3.5. The releases are coming thick and fast, although it seems an eternity since I first saw LINQ at Tech Ed last year.

Oh finally, check out the video from ScottGu on the Silverlight "Getting Started" page.
http://silverlight.net/GetStarted/

You can download the 1.1 alpha tools for building Silverlight applications in Orcas. And Expression Blend is available for 6 month trial from the Microsoft web site. You'll be able to fully recreate the integration that Scott demonstrates in the video.

I certainly will be once this 5gb download finishes...

Labels: , , , , , , ,

Tuesday, 1 May 2007

Silverlight != Flash

... or so Microsoft tries to tell us.

Today SilverLight was officially launched: http://www.silverlight.net

There's been a lot of talk about Silverlight being a Flash killer, but Microsoft is pushing a different stance. They seem to be almost PLEADING that it not be compared as a Flash-Killer and that it in fact can co-exist in the same market, that they can go hand-in-hand. None more so than Scott Barnes (http://blogs.msdn.com/msmossyblog/), who comes from an Adobe background. Scott is now a Developer Evangelist for Microsoft, based in Brisbane. He posts to his blog incredibly frequently, and has a strong following of Adobe enthusiasts who are quick to point out the MS flaws. It makes for a very interesting read. Scott also makes appearances at the various user groups held in the Brisbane office so you may get to meet him.

Back to Silverlight. Version 1.0 is in beta, while version 1.1 is in Alpha. 1.0 supports development in Javascript, while 1.1 supports development in .NET. In fact, head to the comparison page on the Silverlight site. As a .Net developer, I am very interested in 1.1 because it has full support for .Net 3.0, C#, LINQ, and a host of other wonders. It will make development easy. All I need now is the design skills...

So why is Microsoft NOT pushing Silverlight as a Flash killer? Maybe they want to bring the Adobe developers into the fold, instead of alienating them.

As the Irish guy at my work pointed out... if you think about it, a bright 'Silver Light'... something like a 'Flash' perhaps?

Labels: