VB.NET vs C# – Event handling

Basic concept of the events is simple:
Class can raise an event. User of the class can subscribe to the event and react to it using delegate functions.

For example, handling the click event of a button. Button is named btnOK, delegate function is named btnOK_Click.

Subscribing:

//C#
btnOK.Click += new EventHandler(btnOK_Click);
'VB.NET
AddHandler btnOK.Click, AddressOf btnOK_Click

That’s very similar but this is also where similarities end. Now for the differences:

VB – Syntactic sugar:

Instead of manually attaching to the event, you can decorate the handler function with Handles keyword:

Private Sub btnOK_Click(...) Handles btnOK.Click

With that you don’t have to worry about adding or removing the handler and it’s obvious what this method is doing and what it handles. One method can handle multiple events of the same signature.

Private Sub txt_Validating(...) Handles txtName.Validating, txtNumber.Validating, txtDate.Validating

C# – IDE feature:

After writing “btnOK.Click +=” IDE will offer you to complete the statement for you (press TAB to insert “new EventHandler(btnOK_Click)”). Even better, after that it will offer you to create delegate sub if you press another TAB. Useful and unobtrusive, great feature.

VB – IDE feature:

If a variable is declared with WithEvents keyword (all UI controls created in designer are), you can select it from pull-down menu above the code and all events will be offered in another pull-down menu next to it. Clicking on the event automatically creates a delegate sub with Handles keyword.

C# – IDE problem:

Lack of handles keyword is problematic in C# when you create the event handler from UI designer. Double clicking a button automatically creates the delegate and attaches the event to it. Problem is that attacher code is hidden in autogenerated part of the code and you can’t see if handler is really attached which can lead to duplicate attachment or no attachment at all.

VB – feature:

You can just raise an event, if somebody will handle it, great, if not, no worries.

C# – feature:

You can check if any event is attached and raise only if there is.

Tags:

VB.NET vs C#

I have been working in VB.NET at work and C# at home for some time now. Sometimes I get puzzled when they differ in unexpected places. They are very different in syntax, sure, but that isn’t the issue for me.

I can’t say for some, for others I just accept the differences. You learn one way or another. Or both.

That said, it is interesting that although they are product of the same company and compiled to the same intermediate language, using them is very different experience. After the syntax which is, as I said, just the way it is, the next big difference is made by IDE. That’s what puzzles me. One product, Visual Studio, behaves very different for each language. It doesn’t just accommodate the differences, it expands them.

Under VB vs C# tag I’m collecting puzzling differences I stumbled upon. It’s not about syntactic differences – that is just fodder for religious wars. Some syntax differences are mentioned only when they result in big advantage for one or another language. But what I want to express here are different experiences which come combined from IDE, syntax, available tools and community.

  1. Event handling
Tags:

G-startup development considerations

I want G-startup to work on both Windows and Linux. Although it is possible to produce cross platform application, that gave me a few choices and accompanying problems. I want the application to look native to platform it is running on, avoid numerous dependency installations and avoid lengthy downloads.

Unfortunately, this is yet impossible. None of my solutions are nice, rather I must choose the approach which sucks the least. Here are some options:

Python and GTK:

Runtime is mostly present on Linux. Only PyGTK needs to be downloaded. Unfortunately, on Windows it requires download of Python, GTK runtime and PyGTK.

.NET:

2.0 framework is installed on many Windows machines. Linux is covered through the Mono project which is a separate install.

Multiple codebase:

Python + GTK for Linux and .NET for Windows. This approach covers previous drawbacks but adds maintenance problems with having two versions of source code.

I don’t like any of this but that’s the state of the industry every developer must cope with.

For now, I picked multiple codebase approach because I know how to do it immediately. Since my source code is under 1000 lines, maintenance is not an issue yet. I repeat, YET. It will be after the first few bugfix sessions. This version is now in trunk in repository and available for downloads.

I remain curious about Mono version. I suppose I will branch the project in the near future to try Mono approach.

Gstartup on Google code

Tags:

G-startup project

It’s hard to find an application or utility that wasn’t already written. If you have some problem or want some improvement for your desktop, chance is you are not first with this idea.

These are obscure tools, I don’t know anybody (in person) who uses it beside me. But they exist.

Most projects start with scratching an itch: solving a problem a user or developer have. But there is already a utility for almost everything. If problem looks simple, it’s probably already solved. So I never actually developed any small, useful, hobby application. Oh joy when I finally found an unexplored land.

For some time I’ve used a script to delay starting of startup application. My startup folder in Windows and Startup applications in Ubuntu are very densely populated. Thunderbird, Skype, Pidgin, Dropbox, Deluge, Gnome Do, WinSplit, background changer, calendar… Having them all start simultaneously (competing with antivirus and firewall) at logon renders my PC unusable for few minutes. And if I just want to quickly open some file or check something on the Internet, I can’t for some time. Ant this is where my script helps.

Instead of starting all applications, only script is started. Script waits for some time and then starts the first program. Then it waits some more and start the next one. And so on.

This is quick and dirty vbscript:

WScript.Sleep(240000)
call ShowApp("Pidgin")
WScript.Sleep(20000)
call ShowApp("Skype")
WScript.Sleep(60000)
call ShowApp("Mail")
 
sub ShowApp(app)
    Set shell = CreateObject("WScript.Shell")
    select case app
    case "Mail"
        shell.Run """C:\Program Files\Mozilla Thunderbird 3\thunderbird.exe""" , 7, false
    case "Pidgin"
        shell.Run """C:\Program Files\Pidgin\pidgin.exe""" , 7, false
    case "Skype"
        shell.Run """C:\Program Files\Skype\Phone\Skype.exe"" /nosplash /minimized " , 7, false
    end select
    Set shell = Nothing
end sub

Simple. And it works. Delayed startup the simplest way. But now here come them features. One case is when I want to completely bypass all startup applications. Other case is that I need the application now but it’s not scheduled for next few minutes. Then I decided to convert this script into a system tray enabled GUI application. I also used this opportunity to try running the project on Google code. So here it is: G-startup (from gentle startup, yes, searching for new name).

gstartup

This is what you get: only tray icon is displayed which disappears once all applications are started. Clicking on it a window drops down allowing some actions.

Currently there is version for Linux only. It’s written in Python using Glade for GUI. Check it at: Gstartup on Google code

Tags:

A new blog

Here it is, another blog in the blogosphere. It seams that writing a blog is a worthwhile habit. When writing about something you don’t know much about, you’ll learn and improve. When writing about stuff you do know, you’ll learn that you still have much to learn. And knowing that you lack in knowledge is a reward in itself.

I’m author of another blog, games and code. That was my first blog where I wanted to write about games and game programming. I made some good articles, hosted a D&D campaign and practiced my writing. It is like my hobby side on the Internet.

Code complex should cover my growing passion about programming in general, from code techniques to enterprise applications. The blog name Code complex expresses two things: how programming is a complex affair and how I’m getting a complexes from coding.

Enough for intro. Now to see how much of a real stuff I have to say.