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

2/11/08

Comparing Loops in RPG and C#

Looping operations are quite similar in C# and RPG. Note that RPG uses 'EndDo' or 'EndFor' and C# uses curly braces {} as the beginning and end of a loop (or any code block for that matter).

Here's the different loops
A = 0; B=10
'Statements' is all the code in the loop that you wish to iterate over.

RPG Loop C# Loop
DOW (A<b); statements EndDo; While (a <b)  {statements}
FOR A = 1 to 10;  statements  EndFor; For (int a=0; a<10; a++)  {statements}
DOU A > B; statements  EndDo; Do   {statements}
while (A >B);
   

 

Both languages have ways to skip statements or break out of the loop

iter; Continue;
Leave; break;

 

Here's sample code showing the C# while loop (same as RPG DOW)
Note that the While operation code has {} braces to denote the beginning and end of the loop.

The Do While loop...

            // The while loop
// The while statement is equivalent to the RPG 'DOW'
int ebayJunk = 0;
int CreditLeft = 500;
bool HaveMoney = true;
// The loop will execute as long as HaveMoney is true;
while (HaveMoney) // checks before going into the loop
{ // beginning of while loop
ebayJunk += 1;
CreditLeft -= 100;
Console.WriteLine("Useless Gadgets={0}", ebayJunk);
Console.WriteLine("Money left=${0}", CreditLeft);

if (CreditLeft <= 0) { HaveMoney = false; } // Set condition for while loop
// Entire loop is executed a
} // end of while loop (same as EndDO in RPG
Console.WriteLine("You're Broke!!");
Console.ReadLine();
And the For loop....
// For loop example. Finds the first blank space in a string
// The block of code between the curly braces gets iterated
// over. The loop decrements from the length



String field = "To the Galaxy and Beyond";



for(int i = field.Length; i > 0; i--)


{
Console.WriteLine("Letter is not blank: "+ field[i - 1]);



if(field[i - 1] == ' ')   // note how I can treat a string like an array


    break; // exit once I reach a blank space


}


 














Share this post :

Labels: , ,

11/15/07

Calling a program on an iSeries with .net

Here's an easy way to call a program on the AS/400 with .net. What's neat about this function is that it also accepts a return argument.

Requirements:
  1. Client Access
    This method uses the Client Access library so you will need to have Client Access installed on your PC. Specifically you need cwbx.dll which is in the Iseries access folder C:\Program Files\IBM\Client Access\Shared
    Add this into your Visual Studio project as a reference
  2. The host server on the iSeries must be started using STRHOSTSVR SERVER(*ALL)
    Copy the code below into Visual Studio



    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using cwbx; // cwbx.dll is in the Iseries access folder C:\Program Files\IBM\Client Access\Shared
    // YOu must add it in to you project; Click on your references folder in solution explorer to add it in.

    namespace CallAS400pgm
    {
    class Program
    {


    static void Main(string[] args)
    {

    // Modify the following for your own iSeries

    string AS400Name = "192.168.0.1"; // Change this to the IP address of your machine
    string AS400User = "USER"; // User name to sign on to AS/400
    string AS400Password = "password"; // Password used to sign on to the AS/400
    string AS400Pgm = "DOTNET1"; //Name of program you wish to call on the AS/400
    string AS400Lib = "QGPL"; // Name of library where the program is located


    Console.WriteLine("Creating AS/400 object....");

    cwbx.AS400System AS400 = new cwbx.AS400SystemClass(); // creates an as/400 object
    cwbx.Program program = new cwbx.Program(); // Create a program object

    AS400.Define(AS400Name); // IP of AS/400

    program.system = AS400;
    program.system.UserID = AS400User; // Your user name
    program.system.Password = AS400Password; // Your password

    // define the name of the program you want to call on the iSeries
    program.LibraryName = AS400Lib; //Library where your program is located
    program.ProgramName = AS400Pgm; // Program that this app will call

    // NOTE: before you sign on, the host server on the iSeries must be started using STRHOSTSVR SERVER(*ALL)
    Console.WriteLine("Signing on to " + AS400Name);
    AS400.Signon();
    AS400.Connect(cwbcoServiceEnum.cwbcoServiceRemoteCmd);

    if (AS400.IsConnected(cwbcoServiceEnum.cwbcoServiceAll) == 0)
    {
    Console.WriteLine("Not connected");
    }
    else
    {
    ProgramParameters parms = new ProgramParameters(); // must create parameter collection
    // parms.Clear(); // if you have no parm use this statement

    // Define the parms you are sending and receiving from the iSeries pgm
    parms.Append("MsgToAS400", cwbrcParameterTypeEnum.cwbrcInput, 30); // // Input parm called 'MsgToAS400;
    parms.Append("ReplyFromAS400", cwbrcParameterTypeEnum.cwbrcOutput, 30); // create a parameter object name, type & length

    // puts a value into the parameter object
    StringConverter strcon = new StringConverterClass();
    strcon.Length = 30;
    parms["MsgToAS400"].Value = strcon.ToBytes(" This is from a dot net pgm, hi");


    try
    {
    Console.WriteLine("Sending a message to the iSeries....");

    Console.WriteLine("Calling program on the AS400....");
    program.Call(parms); // Runs until job is completed

    // Get the return value from the ISeries pgm

    String reply = strcon.FromBytes(parms["ReplyFromAS400"].Value);
    Console.WriteLine(reply);
    //This program dotnet1 is called on the iSeries. dotnet1 has 2 parameters
    // it takes the first and displays it to a user
    // it then sends back a message to this class in ReplyFromAS400

    Console.ReadLine();


    }
    catch (Exception e)
    {
    foreach (Error error in AS400.Errors)
    {
    Console.WriteLine(error.ToString());
    }

    throw;
    }

    }


    }
    }
    }


Labels: , , , , , , , ,