method that returns more than one value?

V

VM

In my Win app, I have an event that calls method myMethod.Process() which
returns a struct. This method Process() is composed of several other
methods: openDB(), checkDB, expireDB(), etc... and all these methods return
0 (Success) or 1 (Fail). If any of these methods return a 0, the process is
cancelled. How can I return the 1 value (failure) if the method itself
returns a struct?

Here's some of the code:
private void btn_getZip_Click_1(object sender, System.EventArgs e)
{
myMethod m1 = new myMethod();
myStruct Struct1 = new myStruct();
zipStruct = myMethod.Process();
}
--------------------------------
class myMethod:
public myStruct Process()
{
myStruct Struct1 = new myStruct();
if ((OpenDB() == 0) && (ExpDB == 0))
{
int iRes = goProc(Struct1): // initialization was successful
}
else
{
// How do I tell event btn_getZip_Click_1 that OpenDB() or ExpDB()
failed? I'd like to be able to send the value "1" and the method that
returned it.
}

Thanks again.
 
C

C# Learner

VM said:
[...] How can I return the 1 value (failure) if the method itself
returns a struct? [...]

You could use a ref/out parameter.

Simple example:

void StartPoint()
{
int number;
bool success = MyMethod(out number);
// number is now 44 and success is now true
}

bool MyMethod(out int number)
{
number = 44;
return true;
}
 
M

mikeb

VM said:
In my Win app, I have an event that calls method myMethod.Process() which
returns a struct. This method Process() is composed of several other
methods: openDB(), checkDB, expireDB(), etc... and all these methods return
0 (Success) or 1 (Fail). If any of these methods return a 0, the process is
cancelled. How can I return the 1 value (failure) if the method itself
returns a struct?

Here's some of the code:
private void btn_getZip_Click_1(object sender, System.EventArgs e)
{
myMethod m1 = new myMethod();
myStruct Struct1 = new myStruct();
zipStruct = myMethod.Process();
}
--------------------------------
class myMethod:
public myStruct Process()
{
myStruct Struct1 = new myStruct();
if ((OpenDB() == 0) && (ExpDB == 0))
{
int iRes = goProc(Struct1): // initialization was successful
}
else
{
// How do I tell event btn_getZip_Click_1 that OpenDB() or ExpDB()
failed? I'd like to be able to send the value "1" and the method that
returned it.
}

You should probably have these methods throw an exception when they have
a failure. The callers can catch the exception and handle it appropriately.

Many of the exception classes in the framework will provide you with the
capability to pass on the error detail that the caller will need to
determine how to handle the exception they way you want.

If you need the exception to have more detail, you can always write a
custom exception class that derives from System.ApplicationException
 
V

VM

what's the difference between ref and out?
I've used ref but what does the ref do?

Thanks.

C# Learner said:
VM said:
[...] How can I return the 1 value (failure) if the method itself
returns a struct? [...]

You could use a ref/out parameter.

Simple example:

void StartPoint()
{
int number;
bool success = MyMethod(out number);
// number is now 44 and success is now true
}

bool MyMethod(out int number)
{
number = 44;
return true;
}
 
C

C# Learner

VM said:
what's the difference between ref and out?
I've used ref but what does the ref do?

With ref, the variable that's passed must've been previously assigned a
value, or it'll cause a compile error. With out, this isn't necessary,
but the method that receives the parameter must assign it a value
somewhere along the line, or you'll get a compile error.

Basically, *ref* parameters are input/output parameters, but *out*
parameters are output-only parameters.

Take a look here for more details -
http://www.c-sharpcenter.com/Tutorial/params.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top