Highlighted
Altair Absent Member.
Absent Member.
248 views

Question about Assignment operators

Hi, how to convert this code C# to Visual Cobol?

string FromNumber = "9999999999999";
string password = "xxxxx";
string nickName = "xxxxx";

WhatsApp wap = new WhatsApp(FromNumber, password, nickName, false, false);

wap.OnConnectSuccess += () =>
{
   MessageBox.Show("Connected to whatsapp SuccessFully...");

   wap.OnLoginSuccess += (PhoneNumber, data) =>
   {
     MessageBox.Show("Enterned");
     wap.SendMessage(Phnumber, message);
     MessageBox.Show("Message Sent Successfully...");
    };

  wap.OnLoginFailed += (data) =>
  { 
   MessageBox.Show(data);
   MessageBox.Show("Yes Failed login : {0}", data);
   };

  wap.Login();
};

wap.OnConnectFailed += (ex) =>
{
MessageBox.Show("Conncetion Failure");
};

wap.Connect();

 

Visual Cobol 2.3
Visual Stúdio Community 2015

0 Likes
1 Reply
Micro Focus Expert
Micro Focus Expert

RE: Question about Assignment operators

Please try to remember to include product version information with questions.

Here the C# += is the "add to" operator, and the => is the lambda (anonymous method) assignment operator. With add-to, what's being "added" is an event handler; this is an overload of the add-to operator for combining delegates, a standard aspect of .NET programming.

Delegate-combining and anonymous methods have been supported in Visual COBOL (for .NET managed code) for a while. The Visual COBOL + operator supports combining delegates. I'm not sure if the add verb does; I'd expect it to, but I haven't tried it.

There's a sample named Delegate Combining in the Visual COBOL Samples Browser which demonstrates delegate combining. The Delegates sample demonstrates anonymous delegates. (The Events sample also shows use of an anonymous delegate.) Here's a simple example of both (untested), based on part of your code fragment above:

set wap::OnLoginSuccess to wap::OnLoginSuccess +
   delegate using phnumber as string, message as string
      invoke MessageBox::Show("Entered")
      invoke wap::SendMessage(phnumber, message)
      invoke MessageBox::Show("Message sent successfully...")
   end-delegate

(I've taken some liberties because the code in your example above doesn't appear to make sense - the parameter names are inconsistent, for example -  and I don't know the data types for the delegate parameters.)

In the case of this code we have nested delegate combining and anonymous methods, and while that should be possible with Visual COBOL, the syntax might get confusing. (I'm not particularly fond of the syntax used in the example.) So I'd recommend declaring delegate variables and using those:

declare myOnLoginSuccess =
   delegate using phnumber as string, message as string
      invoke MessageBox::Show("Entered")
      invoke wap::SendMessage(phnumber, message)
      invoke MessageBox::Show("Message sent successfully...")
   end-delegate

declare myOnLoginFailed =
   delegate using data as string
      invoke MessageBox::Show("Login failed: " + data)
   end-delegate

set wap::OnConnectSuccess to wap::OnConnectSuccess +
   delegate
      invoke MessageBox::Show("Connected to WhatsApp")
      add myOnLoginSuccess to wap::OnLoginSuccess
      add myOnLoginFailed to wap::OnLoginFailed
      invoke wap::Login
   end-delegate

And so on.

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.