Dot Net Fluke: Getting by on C# for iSeries RPG Developers

Useful tutorials on C# and .NET for RPG iSeries AS/400 developers. Brought to you from the folks at AranRock Consulting

1/26/08

RPG Subroutine = C# Method?

Someone asked me the other day if RPG subroutines are the same as C# methods.
The answer is yes - like C# static methods but RPG Procedures are even more similar to C# static methods.
The reason being is that while C# methods are discrete named blocks of code you can call from within a program (C# Class) - just like an RPG Subroutine; C# methods allow parameters in the call - just like RPG procedures do. Methods have lots of other features that I’ll get in to but first lets draw comparisons.
Here’s an example:
Both applications read a table called ‘People’ and print out the name of each person on the console.
They both call a subroutine/method called Get_People that takes no parameters from the main block of code.

RPG
Fpeople IF E K DISK
/FREE
exsr Get_People ;
*inlr = *on;

BegSr Get_People;
read People ;
dow not %eof ;
Dsply name ;
Read People
Enddo;
Endsr;


C#


using System;
using System.IO;
namespace ConsoleApplication1
{
class ProgramClass
{
static void Main() // Main block of code 'Main' must be in the class
{
get_People();
}
static public void get_People( )
{
using (FileStream fileStream = new FileStream(@"C:\csharp\people.txt",
FileMode.Open,
FileAccess.Read,
FileShare.None))
{
using (StreamReader streamReader = new StreamReader(fileStream))
{
string text = streamReader.ReadLine();
while (text != null)
{
Console.WriteLine(text);
text = streamReader.ReadLine();
}
}
}
Console.ReadLine(); // Pause the screen
}
}
}



As you can see, RPG is easier to read and less verbose than its C# counterpart. To be fair DB2 is part of the iSeries operating system so all the file handling is already part of the DB.

Remember these are Static methods shown in C#. A static method in C# is where there is only one instance of the method. Believe it or not you can multiple instances of a method in C#. These are called instance methods and are used more often than static methods. I'll get into the difference but the purpose of this post is to show some similarity between RPG and C# first.

Labels: , , , , , ,

4 Comments:

At January 31, 2008 at 5:51 PM , Anonymous Anonymous said...

I don't know if it is fair to call C# more verbose since the iSeries can only access DB2 with ease - any other data source is a pain.

 
At January 31, 2008 at 5:55 PM , Anonymous Anonymous said...

No, C# is verbose and hard to read. But that's because its an object oriented language and needs to create objects to work on. I do think object oriented programming languages have still a long way to go before they will be adopted actually as object oriented and not procedurally as the blogger here is professing.

 
At January 31, 2008 at 5:57 PM , Anonymous Anonymous said...

I'm still finding it hard to get into C#. The coding is hard to read. I've read a couple of books on it now and to be honest it's beyond me. I'm glad I found this blog though 'cos at least now I'm starting fresh.

 
At January 31, 2008 at 5:59 PM , Anonymous Anonymous said...

Can you go through the entire For, Do, while loops? it would help me understand the two better.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home