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: .net, as/400, exsr, iseries, method, RPG vs C#, subroutine
4 Comments:
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.
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.
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.
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