Category Archives: Programming

New To Android? Why Not Start Developing For It!


I am new to the Android platform, but a few weeks ago I bought a Samsung Galaxy S2 mobile phone running Android 2.3.3 (gingerbread) and I absolutely love it. It has gotten me really interested in developing applications for it and as I already have experience writing applications in Java from my university days the learning curve should be pretty small.

All that’s really needed to get started is:

  • A Java Integrated Development Environment (IDE) such as Eclipse
  • The JAVA Software Development Kit (SDK) – The latest version is JDK version 7
  • The Android SDK available here
Android itself is open source so there are heaps of great resources online to get started developing applications. Google themselves have a great developer resource at their website http://developer.android.com/guide/index.html which has a lot of source code examples and tutorials. If I manage to build anything exciting I’ll be sure to post it up on here.

Wish me luck!

VBScript: How-To Obtain The Currently Logged On User’s SID


I wanted a quick script to determine the current logged on user’s SID which I could then write into a new script for example to log each user’s SID at logon (during a logon script, etc).

The script I wrote below uses the environment variables USERNAME and USERDOMAIN to determine who the current logged on user is, and which domain they have logged on to. That information is then used to in a call to the getSid() function which connects to the local computer WMI service and queries it to retrieve the SID for the current user from the Win32_UserAccount wmi class.

First we want to find the current user and domain that they have logged on to:

'find current user & domain
Set wshShell = CreateObject("WScript.Shell")
strUsername = wshShell.ExpandEnvironmentStrings("%USERNAME%")
strDomain = wshShell.ExpandEnvironmentStrings("%USERDOMAIN%")

We’ll then show that information to confirm we’ve retrieved the right information:

WScript.Echo "Username: " & strUsername
WScript.Echo "Domain: " & strDomain

'use the user/domain information to retrieve the SID of the user and print it to the screen
WScript.Echo getSid()

The code above makes a call to a function called ‘getSid() so lets write that procedure. The procedure below creates an object with reference to the local machine’s WMI service, and then retrieves the SID information from the Win32_UserAccount class. It would be better programming practice to pass the username and domain variables to the function and use those parameters locally in the function, but this was written quickly to illustrate the idea.


Private Function getSid()
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objAccount = objWMIService.Get("Win32_UserAccount.Name='" & strUsername & "',Domain='" & strDomain & "'")
getSID = objAccount.SID
End Function

That’s it. This will return the SID for the currently logged on user. Hope this helps.

Recursively Process A Directory Using DirectoryIterator In PHP5


Perhaps one of the most used features of PHP5 for me are the great new in-built iterators, more specifically the DirectoryIterator and RecursiveDirectoryIterator functions. Using these saves a lot of time hand-coding the logic behind listing files in a directory on the local machine as these new iterator functions handle all that legwork themselves.

For those of you who are unsure as to what an iterator is, its basically an object that is able to ‘cycle’ through occurrences of other objects. It doesn’t matter if we are cycling through a bunch of letters, numbers or files in a directory, iteration is a key programming concept that is a fundamental building block of most programs.

You may have seen the usage of a numeric iterator before (say $i):

<?php
$i = 0;


for($i=0;$i<=10;$i++)
{
echo “Number is equal to: “ . $i
}


?>

Processing A Single Level Directory:

A simple example of the use of the (much simpler) DirectoryIterator function can be seen below:

<?php
$it = new DirectoryIterator("/tmp/mystuff");
foreach( as ) 
{ if (!->isDot())
{ echo . "\n"; } } ?>

Processing A Multi-Level Directory (Nested):

<?php
$it = new RecursiveDirectoryIterator("/tmp");
foreach(new RecursiveIteratorIterator() as ) 
{ echo . "\n"; } ?>

In this case, the output should now include a list of all the files and directories under the starting directory (very handy for when you want to quickly print and dig down into multiple levels of a directory tree!

Java JDK 1.5+ Better Way To Read User Input


You Java programmers out there may think I’m crazy but I haven’t written anything in Java for at least 4 years, and in that time the talented guys over at Sun Microsystems have made reading user input much much easier than the old days of JDK 1.4 where we had to suffer with using bufferedinputstream objects.

For those of you who may be new to Java or are coming back to it (like me) after a long hiatus you will be very happy to know there is a nice handy class you can call upon to do this for you…the Scanner Class.

The scanner class is much easier to use to capture user input these days. All you do is create the object:

Scanner in = new Scanner(System.in);

Then call the appropriate method to capture a string, integer, long, etc.

number = (double)in.nextInt();

Here’s a quick example of it in action:

import java.io.*;
import java.util.Scanner;
public class MyUserInput
{
  public static void main(String[] args)
  {
    double number;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter your gross income: ");
    if (in.hasNextInt())
    {
      number = (double)in.nextInt();
      System.out.println("You entered " + number);
    }
    else if (in.hasNextFloat())
    {
      number = (double)in.nextFloat();
      System.out.println("You entered " + number);
    }
    else if (in.hasNextDouble())
    {
      number = in.nextDouble();
      System.out.println("You entered " + number);
    }            
    else
      System.out.println("Token not an integer or a real value.");  
  }
}

A much easier alternative to what we used to have to go through. Many of you may have written your own classes when JDK 1.4 was used to simplify the process (which was what I did) but this new way is a real nice way to capture input.

Which Programming Language Is Right For Me?


So you’re new to software development, or you’ve been writing software for a while but curious as to what else is out there for your line of business? I believe there are 5 key fields software developers work in, and I’ll list my favourite and best suited programming languages to meet the needs of that field.

Enterprise: Java

As more and more businesses are moving many software systems typically run on workstation machines to an intranet environment, Java can play a big part in helping to migrate your software across. If the software you’ve written is already written in java, modifying it slightly to work in a web environment should be a piece of cake thanks to applets and JavaScript.

Windows Application Development: VB.NET / C#

Arguably the two best modern programming languages out there right now on the .NET framework Visual Basic.NET and C# on the .NET Framework. With high extensibility and interoperability these two languages can do pretty much what the other can do with a lot of flexibility.

Web Development Server Side: PHP, ASP.NET

PHP is a very easy to learn server side web scripting language that can do a lot and doesn’t take a lot of code to do even the most complicated tasks. ASP.NET on the other hand is another kettle of fish, and although ASP.NET isn’t a programming language as such (more a web technology) using either VB.NET or C#/++ with this technology can see you getting a lot done quick. It lets you build very well structured and stable web applications with any external data connection you want very easily.

Web Development Client Side: HTML, JavaScript

For any web developer, you’ll most likely be writing a combination of client side and server side code so having JavaScript in your programming arsenal is highly advantageous.

Database Programming: SQL

Whether it be desktop applications development, web development or applications for mobile devices having a sound knowledge of SQL (Structured Query Language) is highly regarded. Making sure you can write well formed and efficient queries may mean the difference of a few seconds or a few minutes of query time saved if you write your queries properly.

So those are just my thoughts anyway and you’ll see that most of these languages are very ‘windows centric’ (I’m personally a windows programmer after all so I may be a little biased) I think its a good idea if you know at least some of these different languages for whatever you do.

Obviously writing software for a Unix/Linux environment you’re choices may differ slightly (C, pearl, python, etc) but this should be used as a guide only and can be augmented for whichever environment you’re developing for.

Write Entry to Eventlog using VB.NET


Are you a VB.NET programmer who’s ever looked through the windows eventlog (eventvwr.exe) thinking to yourself “How can I get my program to write events to the eventlog”? Here’s how:

Public Function WriteToEventLog(ByVal Entry As String, Optional ByVal AppName As String = "VB.NET Application", _
Optional ByVal EventType As EventLogEntryType = EventLogEntryType.Information, Optional ByVal LogName As String = _
"Application") As Boolean

Dim objEventLog As New EventLog

Try
'Register the App as an Event Source
If Not objEventLog.SourceExists(AppName) Then

objEventLog.CreateEventSource(AppName, LogName)
End If

objEventLog.Source = AppName

'WriteEntry is overloaded; this is one
'of 10 ways to call it
objEventLog.WriteEntry(Entry, EventType)
Return True
Catch Ex As Exception
Return False

End Try
End Function

The function WriteToEventLog has 1 parameter (entry to write to event log) with 3 other optional parameters specifying the Application Name, Event Type raised (Information, Warning, Critical) and the logname (whether the entry is written to Application, System, or other log within the event log).

I find this very useful especially in very large applications where writing an external log (besides .txt or .log files) becomes tedious. Writing entries into the event log is really create when you’re writing windows services also.

Hope this helps, please feel free to send me an email if you have any questions.

Windows developer dumps “pile of crap” for MacOSX


Pete Wright who is was a programmer for Microsoft Windows one day dreamed of joining Microsoft to contribute to the computer industry and really change the way people use computers.

I dreamed of working at Microsoft. When Microsoft joined up with Accenture to form Avanade the word ‘consultant’ sounded so wonderfully romantic to me and I wondered if ever I’d make it there as one of the elite band of Avanade consultants, spreading the Microsoft message all over the world. I dreamed of systems that would change lives, help people, and do cool new things never seen before”  – Pete Wright (MacDailyNews).

Now how things change so quickly. Everybody who knows anything about computers knows that Microsoft’s flagship product ‘Microsoft Windows’ is a spaghetti code situation at best, once with the idea that complexity in how an operating system is designed would better the overall product has eventually ended up with a product that is a nightmare to code for, and almost a bigger headache for an end-user to use.

So Pete has done what many programmers im sure have thought at Microsoft before, he has jumped ship to go work for Apple Computer to work on a project which prides it self on end-user simplicity, and a much more managable code-base…Apple MacOSX.

To read all about Pete’s experiences with working with Microsoft and his jump into MacOSX, be sure to check out his blog entry entitled “Goodbye Microsoft, Pete has left the building!”. It is really interesting and well worth a read if you are a programmer youself or student with the same dream of working with Microsoft has Pete once had.

Programmers need to stop writing code?


I read an article recently on The Inquirer which discusses why software developers should stop writing code from scratch, and start learning to be ‘agile’ and recycle existing code.

“Apparently the keys to unlocking the “agility paradox” are architecture, a focus on software process and engineering, and recycling” – Nick Farrell, The Inquirer

Sure, programmers are definately not going to loose their jobs based on opinions in this article, they will still need to be there to assemble a variety of packages to implement greater functionality in whichever software project they are working on. As a programmer myself (BCompSc, final year), I have noticed that over the years, especially with the introduction of the .NET framework by Microsoft, that work is being done to cut down the workload of a programmer by the amount of ‘fresh code’ they need to come up with to make a program functional.

The idea of reusing or recycling code has been around for quite some time, probably ever since modern programming languages have been around. A programmer could write a new class() which provided ‘x’ functionality, and could keep reusing that class wherever and whenever he needed that same functionality over again, whether it be in the same or different projects.

My main languages I code in are C/C++, JAVA and VB.NET (.NET framework, I should probably learn Visual C# or something too hehe). Whenever I switch between these various languages to program in the .NET environment, you can really notice a big different in the amount of code you need to write to achieve a similar result as you would get if you wrote the same program in C++ for example. Microsoft’s .NET framework includes a vast amount of ‘pre-defined’ code if you will, objects that can be reused to save time.

I noticed this when the .NET framework v2.0 was released, and started using Visual Studio.NET 2005, we programmers had access to a bevy of new and interesting functionality at our fingertips. No new code needed to be written as we could simply recycle objects and code from which had already been writen and provided to us by .NET. One new object which stands out in my head is the Web Browser object which provides web browsing functionality to an application with basically no extra code required.

I also remember a week ago I think, I read an article online somewhere (forgot exactly where) which discussed how programmers spend their time predominantly whilst they are programming. Either writing code, modifying existing code or understanding code written by other developers. The biggest chunk of time amongst many programmers (~60% est.) was when they were sitting there, reading and understanding other’s code, then (~30% est.) modifying existing code and finally on average (~10% est.) of programmers would actually be writing new code.

Don’t take this data I just spewed out at you as accurate, but based on these estimates, you can see that many programmers don’t write new code these days as it is, so have these guys at ‘The Inquirer’ totally lost it in this recent article, saying that programmers “need to stop writing code”?

At Gartner’s Application Development Summit event recently, Matt Hoyle (Gartner Analyst) gave a keynote speech saying, “The future of application development is not about programmer productivity,”…”but in assembling functionality from components.”

I Think this couldn’t be more true, especially with everything I just talked about earlier about reusable objects in the .NET framework for example.

Programmers shouldn’t stop writing code altogether, but the future of Rapid Application Development (RAD) is definately to assemble functionality from existing componments. Who knows, maybe in 20 years time, Software Architect maybe a better term for a programmer. We shall see.

Open Source: changing the world and web


“Lets face it, not everyone is a tech junkie at this stage in their life (I know that I am not) but the further one gets in their college career, the closer we get to the corporate realm, and it is important to note one of the most interesting tech issue revolutionizing our world, open source programming.”

I have been a big advocate of open source software for a long time and have recommended it to many people as a free alternative to many commercial software which typically costs many hundreds of dollars. Not only is it getting something for a whole lot cheaper, but it is introducing amazing new products into a bloated marketplace in much need of rejuvination which open source software can provide.

Check out the artcle via the ‘read more’ link below, a very interesting read.

read more | digg story