Wednesday, October 27, 2010

Developing for the iPad

The past months we have been busy with a new challenge: Developing for the iPad.
I can tell you that developing for the iPad is a total different experience compared to Delphi and Visual Studio. Never the less it is great fun! Besides the fact that it is a fun platform to work on it is even more fun to learn a new platform, language and IDE.

The tools: XCode and Interface builder
We decided to go the hard way and develop with Apples native IDE XCode. As .NET developer an easier approach would be to choose for a Mono solution like MonoTouch, however we felt that we had to know 'the metal' first.
The main tools you use developing for iPad are XCode and Interface builder. XCode is a good to work with IDE, however compared to Visual Studio it lacks some features. (Must say I still find new features, even today). One annoying point of XCode is that it opens every file in a new window (when double clicking on a file) so after a while it can be hard to seperate the trees from the forest with all that open files. However if you click once you can keep them in one window but for some reason that is hard to persist.
A big plus for XCode, in my opinion, is that it helps you to work in the MVC design pattern way which gives you clean code.
XCode does not offer an integrated form designer like VS does instead you make your views (forms) with Interface builder.

The language Objective-C
The language, Objective-C, looks at first very weird with all that brackets but there are great resources online that helped us to get started.

If you would like to start iPad development take a look at some resources we gathered along the way:
Apples developer website (Get yourself a free account)
Stanford University lectures on iTunes U (Highly recommended!!!!)
Cocoa Dev Central - Learn Objective-C

It is likely that I will write some more blogposts about this new adventure in the future.

Tuesday, August 17, 2010

Fooling around with TWebbrowser compilation post

In the statistics of my blog I find a lot of people searching for Delphi’s TWebbrowser solutions and examples. I wrote a few post about some TWebbrowser specific problems and possible solutions.
Because they are in seperated, non related, posts I thought it would be nice to compilate all the knowledge in one blogpost. Well here it is. :)

Fooling around with TWebbrowser #1
Describes how to use TWebbrowser for editing HTML

Fooling around with TWebbrowser #2
Describes how to retain the browser session within you application

Fooling around with TWebbrowser #3
Describes some TWebbrowser basics like how to get the right popwindowsize, how to fill up a combobox with visited websites, how to enable/disable navigate buttons etcetera.

Fooling around with TWebbrowser #4
Describes two solutions for the Enter key problem.

Posting data with TWebbrowser
Describes how to post data to websites using TWebbrowser

Saving TWebbrowser content with IPersistFile
Describes two ways to save the content of the current loaded document in the browser.

Two way interaction with Javascript in Winforms using Webbrowser
Although about Winforms C# this should be compatible with Delphi TWebbrowser.

Enjoy!

Wednesday, May 19, 2010

Using Extension methods on Lists to make it fluent

Since C# 3.0 you can write extension methods on any class that you want, even if you don’t have the source code of the class. In other words you can extend a class with your own methods.

Extension methods can only be declared in static classes as static methods.

Extensions on List<T> classes can be very handy and make your code more readable and fluent.
Suppose you have this mixed Animal list with Dogs and Cats from this blogpost.

List<Animal> animalList = new List<Animal>(); 
animalList.Add(new Dog("Dog1", ConsoleColor.Red));
animalList.Add(new Dog("Dog2", ConsoleColor.Green));
animalList.Add(new Dog("Dog3", ConsoleColor.Red));
animalList.Add(new Cat("Cat1", ConsoleColor.Black));
animalList.Add(new Cat("Cat2", ConsoleColor.Black));
animalList.Add(new Cat("Cat3", ConsoleColor.Black));

(Never mind the ConsoleColor for the animal ;-) )

Suppose we want all the red dogs from the animalList, we could write our code like:


//Get the Red docs

List<Dog> redDogList = animalList.OfType<Dog>().Where(n => n.Color == ConsoleColor.Red).ToList();

This works, however everytime we want to get some colored animal we would get the same type of code. We would probably write some static methods to avoid this :


 List<Dog> redDogList = ListUtils.GetTheDogs(animalList);


Even better is to make this static extension methods so that the functionality looks to be encapsulated by the list its self. We can then also make the method names more readable, for instant not “GetTheDog”, but “WhichAreDog”.


For example:
public static List<Animal> WhichAreDog(this List<Animal> animalList)
{
  return animalList.OfType<Dog>().Cast<Animal>().ToList();
}



public static
List<Animal> HavingColor(this List<Animal> animalList, ConsoleColor color)
{
  return animalList.Where(n => n.Color == color).ToList();
}

The “this” before the first parameter of the method indicates that this is an extension method on this parameter, in this case the list with animals.


Now getting the red dogs looks like this:


List<Animal> allRedDogs = 
animalList.WhichAreDog().HavingColor(ConsoleColor.Red);

Much more readable and fluent!

Monday, May 03, 2010

Exploring LINQ #3

LINQ keeps amazing me in its power and beauty. So much that for each problem I have to solve (coding problem that is)  I find myself wondering can this be done with LINQ, and how.

I am working on a rewrite of an old application to C#. For some reason the original designers had put some ‘model’ data into a none relational database. Instead of making a good relational data model the data has been put as follows (simplified record from the database) :

Option1 = “1;2;3”
Option2 = “A;B”
Value = 1

In other words foreach possible combination of option1 and option2 there is a value. (1-A-1, 1-B-1, 2-A-1 etc.) I would prefer a relational set up, but to honor the past this will have to stay that way. For use in our program it  is required to have all possible combinations into a single flat list which we then can query with LINQ.
Of course it easy to construct some code that would knock this down into a flat list though I don’t think any will be as elegant and as fast to produce as this LINQified solution.

Suppose we read the raw data from the database into a list with raw objects looking like this:

List<Raw> rawList = new List<Raw>();
rawList.Add(new Raw() { Option1 = "1;2;3",
Option2 = "A", Value = 1 });
rawList.Add(new Raw() { Option1 = "4;5",
Option2 = "A;B", Value = 2 });
rawList.Add(new Raw() { Option1 = "1;2;3",
Option2 = "C", Value = 3 });

Using LINQ it is easy to combine the original raw list with the “;” splitted list (arrays in fact) from the fields Option1 and Option2. From that a flat list can be constructed which holds all the combinations:


List<Raw> flatList = (from n in rawList
from opt1 in n.Option1.Split(';')
from opt2 in n.Option2.Split(';')
select new Raw
{
Option1 = opt1,
Option2 = opt2,
Value = n.Value
}).ToList();
flatList.ForEach((n) => Console.WriteLine(n.Option1 +
"-" + n.Option2 +
"-" + n.Value.ToString()));

Resulting in a ‘flat’ list:


1-A-1
2-A-1
3-A-1
4-A-2
4-B-2
5-A-2
5-B-2
1-C-3
2-C-3
3-C-3


Querying this list is now child’s play:

int Result = (from n in flatList
              where (n.Option1 == "4") && (n.Option2 == "B")
              select n.Value).FirstOrDefault();

Tuesday, March 09, 2010

Two way interaction with JavaScript in Winforms using Webbrowser

In a recent project I had to integrate Google maps into a Winforms application. To use Google Maps from a desktop client you must be able to call the Javascript in webpage, which can be done with the Webbrowser component.

Calling JavaScript from C#
Calling JavaScript in a Webbrowser component is very easy. JavaScript is exposed through the IHTMLWindow2 interface which can be referenced from the IHTMLDocument2 interface through the parentWindow property.

doc = webBrowser1.Document.
DomDocument as IHTMLDocument2;
doc.parentWindow.execScript("createMapMarker
("52.3738007, 4.8909347", 1);", "JavaScript");

Calling C# from JavaScript
Calling C# code from JavaScript can be done using the ObjectForScripting property of the Webbrowser component. The object can be called in JavaScript using window.external. The communication is established through COM interop so the class should be visible for COM.
//Class example
[ComVisible(true)]
public class ExternalApplication
{
public string GetApplicationName()
{
return "The application";
}
}
//Connect it to the webbrowser
webbrowser1.ObjectForScripting
= new ExternalApplication();

From JavaScript call the external application, our C# app, like this:

(Google maps click event example)
function createMapMarker(lat, lng, html) {
var point = new GLatLng(parseFloat(lat),parseFloat(lng));
var marker = new GMarker(point);
GEvent.addListener(marker, "click", function() {
var appname = window.external.GetApplicationName();
alert(appname);
});
map.addOverlay(marker);
return marker;
}

For Delphi equivalent code take a look at Steve Trefethen blog serie about Google Maps / Delphi integration here.

Wednesday, January 20, 2010

Connecting (struggling) to Oracle database

This week I have been working (read struggling) on connecting a C# .NET application to Oracle. In the end I was trapped between 32 bits vs 64 bits, .NET Oracle providers and 32 bit OLE DB providers.

First of all I challenged this task, not bothered by any knowledge about Oracle. I did know that if you want to connect to an Oracle database you have to install the Oracle Client on the client machine. This only 1,7 Gb (!) download (Oracle 11g) happily told me that my Windows 7 version (real version 6.1, 64 bits) was not certified for the job. Only Windows 6.0 was.

Huh, now what?

After some googling found this blogpost about how to trick the setup to install on Window 7 (6.1) anyway.
After that it installed gracefully (some warnings…), time to startup VS to give it a spin!

Well not. That app that I am writing connects besides Oracle and SQL Server to an Access database. Because the Jet OLEDB driver for Access is 32 bits only I am doomed to target the x86 platform for now. No problem, however the 64 bits Oracle client can not connect targeting the x86 platform. Getting a BadImageFormatException:

[InvalidOperationException: Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.]

(Still having the feeling it is the other way around, because I have the 64 bit Oracle client components installed)

Huh, now what?

Because the ADO.NET provider for Oracle from Microsoft suits well in the other technologies I tried to google more information. As it seems this driver is by the coming of .NET 4.0 deprecated. So on second thoughts not a good choice after all. Using the .NET provider from Oracle (ODP) then? Can I install 32 bits Oracle client on 64 bits machine? Who knows.

Fortunately Twitter came to rescue someone pointed out that there are third party solutions, which can connect to an Oracle database even without the Oracle Client.

So I tried Devarts dotConnect, which indeed can connect without the Oracle Client, isn’t that just great! Within 5 minutes I was connected (either 32 or 64 bits).

So if you want to connect to Oracle on .NET, keep some sparetime afterwards, I would suggest using third party components for it!

Use an image as your UIBarButtonItem

Using an image as your UIBarButtonItem in your navigationcontroller bar can only be achieved by using a common UIButton as the BarButtonItem...