Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
from an assembler dinosaur and OO newbie : I have a method that calls an external program which either returns a zero return code some data in stdout, or returns with a non-zero return code and error messages in stderr and maybe stdout. This is what I've done :

C#
class methodReturn
{
    public int rc { get; set; }
    public string message { get; set; }
    public string returnData { get; set; }
}
.
.
methodReturn r = GetStuff(someParm);
if (r.rc != 0)
{
  MessageBox.Show(r.message);
  Application.Exit();
}
.
.
private methodReturn GetStuff(string myParm)
{
    methodReturn r = new methodReturn();
    ProcessStartInfo pInfo = new ProcessStartInfo();
    .
    .
    pInfo.UseShellExecute = false;
    pInfo.RedirectStandardOutput = true;
    pInfo.RedirectStandardError = true;
    Process p = Process.Start(pInfo);
    p.WaitForExit();
    string ProcessStandardOutput = p.StandardOutput.ReadToEnd();
    string ProcessStandardError = p.StandardError.ReadToEnd();
    int rc = p.ExitCode;
    p.Close();
    if (rc == 0)
    {
        r.returnData = ProcessStandardOutput;
        r.message = null;
    }
    else
    {
        r.rc = 8;
        r.message = "Error : \n" +
                    "Exit Code : " + rc + "\n" +
                    "StandardOutput : \n" + ProcessStandardOutput + "\n" +
                    "StandardError  : \n" + ProcessStandardError;
        r.returnData  = null;
    }
    return r;
}


I'm guessing creating a class just to call a method is not good OO practice. What would be a better way? thanks.

[Modified: changed lang from msil to cs]
Posted
Updated 21-Apr-10 11:34am
v2

1 solution

was8309 wrote:
I'm guessing creating a class just to call a method is not good OO practice


That's quite acceptable IMO. Many events in the framework have EventArgs derived class purely for the one event and corresponding delegates - this is much the same.

Wherever I need to return multiple values from a method I create a class/struct as appropriate, much tidier than using refs/outs which is the only other way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900