Comparing RPG code to C# : The For Loop
There are similarities between free format RPGIV and C#. In the example below I show a small program that accomplishes the same task in both languages. The task is to find the first blank character in a string from the end to the beginning. I'm well aware there is a single method available for this on String in C# but the purpose here is to show the similarities.
RPG is below:
Now C#:
You can see some basic similarities:
- Statements end with a ';' ,
- the For loop is similar.
- 'Leave' gets you out of an RPG loop while 'Break' exits a C# loop.
- Assigning values is the same in both.
- Declaring variables in RPGIV requires both type and how many characters (you can declare varying length variables but do define an initial length). To define a variable you must put values in fixed positions in a special kind of statement block called 'Definition Specifications' or D-specs. In C# you simply declare the type. The type of the index 'i' is denoted by a zero in the decimal placess position.
- C# requires a 'Main' method. RPG just executes from the first executable line.
- C# variables are local by default. I can't access that 'i' variable outside of that For loop - the program wouldn't even compile. RPG variables are global unless used in procedures.
- C# requires a class definition, namespace and you define what other namespaces in .Net you want to refer to in your program with the 'Using' statements. The using just means you don't have to refer to the full name like 'System.Console.Readline()'.
- C# doesn't need 'EndFor' or 'EndIf'. Coding is accomplished in blocks between curly braces '{' and '}'.
- The biggest difference is that C# strings and objects have a multitude of methods and properties on the string itself - not part of the language. RPGIV is pretty good at string manipulation too but can never approach the flexibility of C# as the RPG designers would have to create a new keyword when it needs to perform a function on a string that it doesn't already have. In C# there are only a handful of keywords - most actions are accmplished by methods on the objects themselves. For instance in the above example you access the length of a string by examining its property. e.g. 'field.length' returns an integer value whereas in RPG I chose to use the built-in-function %len to do the same thing.
To find the first non-blank in a string starting from the end you can use field.IndexofLast(' ') . RPGers note how the methods and properties are on the string 'field' which I defined. As soon as you define a variable it creates a copy of the string object so you have full access to a huge amount of manipulation techniques. Adding new methods and properties to strings and other types isn't a major change in the language.
C# Output:
1 Comments:
Thanks for this Colm. The book I have doesn't explain it very well. I will look into your recommendation for the Head first series.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home