Thursday, November 11, 2010

Hello World!

This is my real first post in Powerbuilder. (I wrote a lot about PB and my association with it before). Like it is customary for any tutorial or book on programming to start with a program to print Hello World, I will try to do the same in Powerbuilder.

Before I jump into it, let's some simple ones: (I am only including the actual lines and not full program code here,  please refer to the really interesting site that prints hello world in various languages - here)

In a Pascal program, you can just say,

[sourcecode language="delphi"]
{Hello World in Pascal}
writeln("Hello world");
[/sourcecode]

(In Delphi which is based on Pascal, it is written as,

[sourcecode language="delphi"]
{Hello World in Delphi}
WriteLn('Hello World');
[/sourcecode]

In C, you can do

[sourcecode language="cpp"]
/*Hello World in C*/
puts("Hello World!");
[/sourcecode]

In Basic Language, (which contributed to  Powerbuilder's Powerscript) you can do

[sourcecode language="vb"]
10 REM Hello World in BASIC
20 PRINT "Hello World!"
[/sourcecode]

Coming to the more Object oriented world,

In C++ you can write,

[sourcecode language="cpp"]
/*Hello World in C++*/
cout << "Hello World!" << endl;
[/sourcecode]

And in Java, this would be,

[sourcecode language="java"]
/*Hello World in Java*/
System.out.println("Hello World!");
[/sourcecode]

Well, almost of all of them print to a console - a text based screen, like in DOS, Unix, Linux. The same task of showing hello world to the user takes a bit more, when it comes to a windowing world (like MS windows, X-windows, Apple mac interface). The presentation becomes little more important. After all, in such an operating environment, everything is shown inside a window, so our program needs to write into one such too! In a windowing environment, simplest way to present a text is using some type of message box, which is a small window showing the text (and may be some question buttons).  Luckily windows API offers functions to create such a messagebox with ease. Let's see how we could do the same in Java, for a windowing environment:

[sourcecode language="java"]
/*Hello World in Java -windowing version*/
javax.swing.JOptionPane.showMessageDialog(null,"Hello world!");
[/sourcecode]

Swing is a package available in Java to create an application for a windowing environment.
Coming back to what we started with, to print Hello World in Powerbuilder, we could do something like above using a messagebox. Luckily PB offers a function for that - MessageBox. So, in the simplest form, we will do,

[sourcecode language="cpp"]
/*Hello World in Powerbuilder-Simple*/
MessageBox("Hi from Powerbuilder", "Hello World!")
[/sourcecode]

We are almost done. Like almost all of the examples above (except BASIC which was interpreted, you just type that one line and it did print it), we need to wrap this line in a program. In the java example above,
we would do,

[sourcecode language="java"]
// Hello World in Java using Swing GUI
class HelloWorldSwing {
static public void main(String args[]) {
javax.swing.JOptionPane.showMessageDialog(null,"Hello world!");
}
}
[/sourcecode]

Notice the main function. That is the entry point in various languages like C, C++, Java. This is the function that is called when the program is executed.

Powerbuilder is built slightly differently. It does have an entry point function - actually an event. This will be the open even of an Application object. Just like the class with main function becomes entry point object in Java, an Application object is the entry point object in Powerbuilder. So in it's simplest form, we simply add the messagebox function in the application open event and just run the application. When run, this is what you will see,





Well, that's for showing the simplest way. But a typical PB application is built not just with application object, but with one or more windows as well. We will see about that in a later post.

No comments :

Post a Comment

I will be happy to hear your comments or suggestions about this post or this site in general.