Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE
Austin1 Honored Contributor.
Honored Contributor.
3125 views

Visual COBOL equivalent to C# "Using" statement?

Jump to solution

Hello all,

Is there a Visual COBOL equivalent to the C# "Using" statement?

... as shown In the following C# example, the "Using" statement (not the "Using" directive)... (as in the statement that says "Using (OleDbConnection..." ?

.................................................................

using System;

using System.Data;

using System.Data.OleDb;

class Program

{

    static void Main()

    {

        // The connection string assumes that the Access

        // Northwind.mdb is located in the c:\Data folder.

        // string connectionString =

        // "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="

        // + "c:\\Data\\Northwind.mdb;User Id=admin;Password=;";

        // Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Austin1\Documents\Northwind.mdb

        string connectionString =

        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="

        + "c:\\Users\\Austin1\\Documents\\Northwind.mdb;User Id=admin;Password=;";

        // Provide the query string with a parameter placeholder.

        string queryString =

            "SELECT ProductID, UnitPrice, ProductName from products "

                + "WHERE UnitPrice > ? "

                + "ORDER BY UnitPrice DESC;";

        // Specify the parameter value.

        int paramValue = 10;

        // Create and open the connection in a using block. This

        // ensures that all resources will be closed and disposed

        // when the code exits.

        using (OleDbConnection connection =

            new OleDbConnection(connectionString))

        {

            // Create the Command and Parameter objects.

            OleDbCommand command = new OleDbCommand(queryString, connection);

            command.Parameters.AddWithValue("@pricePoint", paramValue);

            // Open the connection in a try/catch block.

            // Create and execute the DataReader, writing the result

            // set to the console window.

            try

            {

                connection.Open();

                OleDbDataReader reader = command.ExecuteReader();

                while (reader.Read())

                {

                    Console.WriteLine("\t{0}\t{1}\t{2}",

                        reader[0], reader[1], reader[2]);

                }

                reader.Close();

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

            Console.ReadLine();

         }

    }

}

.................................

And would anyone like to convert all of the above to Visual COBOL and post the Visual COBOL equivalent? :-)

 

0 Likes
1 Solution

Accepted Solutions
Highlighted
evank Absent Member.
Absent Member.

RE: Visual COBOL equivalent to C# "Using" statement?

Jump to solution

This perform is not a loop. It is the equivalent of the C# or VB using block.

0 Likes
7 Replies
Micro Focus Expert
Micro Focus Expert

RE: Visual COBOL equivalent to C# "Using" statement?

Jump to solution

Hello,

The equivalent of the C# "Using" statement in Visual COBOL would be $set ilusing

Example:

      $set ilusing "System.Data"

Regards,

–Ғɑɳօ
0 Likes
Austin1 Honored Contributor.
Honored Contributor.

RE: Visual COBOL equivalent to C# "Using" statement?

Jump to solution

No, as I pointed out in the above, I am looking for the equivalent of the Using statement, not the Using directive.

0 Likes
Micro Focus Expert
Micro Focus Expert

RE: Visual COBOL equivalent to C# "Using" statement?

Jump to solution

Sorry about that!

As you already know, "using" has always been a reserved word in Cobol, and I read from MSDN here that "The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. "

In Visual COBOL, you will then need to use the Try...Finally construction.

–Ғɑɳօ
0 Likes
evank Absent Member.
Absent Member.

RE: Visual COBOL equivalent to C# "Using" statement?

Jump to solution

Below is an example:

          perform using cn as type SqlConnection = new SqlConnection(connectString)          

              set cmd to cn::CreateCommand()

              set cmd::CommandText to "select * from dbo.evantb"

              invoke cn::Open()

              perform using rdr as type SqlDataReader = cmd::ExecuteReader()

                  invoke rdr::Read()

                  display rdr["EMPNAME"]::ToString()

              end-perform

           end-perform  

0 Likes
Austin1 Honored Contributor.
Honored Contributor.

RE: Visual COBOL equivalent to C# "Using" statement?

Jump to solution

@MF_Fano - OK, well, as to there being no exact equivalent to the C# "using" statement, that confirms my suspicion, and I guess I can see why (there is already a using reserved word in COBOL, used for another purpose).

Now on to the 2nd question at the end of my original post...

@evank - How do these performs exit their loops?  I see no tests for exiting the loops.

0 Likes
Highlighted
evank Absent Member.
Absent Member.

RE: Visual COBOL equivalent to C# "Using" statement?

Jump to solution

This perform is not a loop. It is the equivalent of the C# or VB using block.

0 Likes
Austin1 Honored Contributor.
Honored Contributor.

RE: Visual COBOL equivalent to C# "Using" statement?

Jump to solution

So, there really IS an equivalent!  I will work with the code you provided to prove it to myself.  Thanks!

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.