Thursday, June 26, 2008

Java : Implementing the C# foreach

This is a common construct:

for (int i = 0; i < "object".length; i++)

It's messy and in C# you can easily do this far more elegantly with the "foreach" command.

How do you do this in Java?

Use something like e.g. for Reflection fields:

import java.lang.reflect.Field;

Field[] fields = "object".getClass().getDeclaredFields();

for (Field field : fields)


Java : Iterating and printing out elements of objects

It's a common problem.

You have an object aa with variables bb, cc and dd and for logging and for debugging purposes, you want to print out the current values of all three so you have:

System.out.println ("value of bb is " +;
System.out.println ("value of cc is " +;

which gets tedious when it's a large object.

Reflection to the rescue!

import java.lang.reflect.Field;

Field[] fields = aa.getClass().getDeclaredFields();

for (int i = 0; i < fields.length; i++)
System.out.println (fields[i].getName() + " = " + fields[i].get(xicmd));

catch (Exception ex)
System.out.println ("Reflection exception = " + ex.getMessage());

Note that if the variables in the object are declared as "private", this code will throw an exception. Hence the:


method which overrides this.


Tuesday, June 24, 2008

.NET : Timing averages

Following on from my previous post about TimeSpan, I needed to do some performance testing and calculate the average time each call took.

This was for a Windows Form project.

for (int i = 0; i < iterations; i++)

DateTime startTime;
DateTime endTime;

int iterations;
int alteredIterations = 0;

TimeSpan total = new TimeSpan(0);
TimeSpan average = new TimeSpan(0);

alteredIterations = i + 1;

startTime = DateTime.Now;

... some method ...

endTime = DateTime.Now;

TimeSpan duration = endTime - startTime;

txtDuration.Text = duration.ToString();

total = total + duration;

average = TimeSpan.FromMilliseconds(total.TotalMilliseconds / alteredIterations);

txtAverage.Text = average.ToString();



The "Application.DoEvents();" is to allow Windows some time to update the counts on the screen.


C# : Calculating timing differences

I needed to put some timings in my program. So I need to get a start time, an end time and then calculate the difference between them. TimeSpan to the rescue.

static public DateTime startTime;
static public DateTime endTime;

TimeSpan duration = new TimeSpan();

startTime = DateTime.Now;


endTime = DateTime.Now;

duration = endTime - startTime;

"Print" ("Duration is " + duration);


Eclipse : Starting with a different VM

For various reasons, a PC I use has Java 1.4 installed and the latest Eclipse Europa requires 1.5. I am unable to update the classpath, environment etc. so although 1.5 was installed, Eclipse would not find it.

The solution was to use the command line option:

C:\eclipse\eclipse.exe -vm "C:\Program Files\Java\jdk1.5.0_15\jre\bin\java.exe"

This forces Eclipse to load with the 1.5 VM.

I placed the command above in a shortcut so it's easily accessible from the desktop.


Thursday, June 05, 2008

XP : Find that missing USB drive

I have a USB drive that simply will not load on one of my PC's.

The PC simply wouldn't recognise the drive no matter what I did.

So Mr. Google to the rescue.

I found the answer here.

The problem was "More than likely the cause is that Windows renamed the drive to a letter that is already in use. This will happen if you have several card readers, thumb drives or external hard drives attached. It will also happen if you are on a network and have mapped drives".

After following the advice, all was well.