473,387 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

method that returns more than one value?

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.
Nov 15 '05 #1
6 2998
Are you against using Out params? Or you could add the return codes into the
struct.

public myStruct Process(out OpenDBReturn, out ExpDBReturn)
{
}

HTH;
Eric Cadwell
http://www.origincontrols.com
Nov 15 '05 #2
VM wrote:
[...] 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;
}
Nov 15 '05 #3
VM wrote:
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

--
mikeb
Nov 15 '05 #4
VM
what's the difference between ref and out?
I've used ref but what does the ref do?

Thanks.

"C# Learner" <cs****@learner.here> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
VM wrote:
[...] 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;
}

Nov 15 '05 #5
VM wrote:
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
Nov 15 '05 #6
VM <vm@none.com> wrote:
what's the difference between ref and out?
I've used ref but what does the ref do?


See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
4
by: Lerp | last post by:
Hi all, With regards to calling data from a database and filling in an editing form based on some query, which is the best (least intensive on processor) method for assigning the returned...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
0
by: george_Martinho | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older than a...
6
by: Joseph | last post by:
Hi All, I am trying to create a method that executes a parallel array and returns a value. I am not sure why the method I wrote is generating an error. The error is displayed as: Use of...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
23
by: ShaneO | last post by:
Hello, I wish to extract embedded string data from a file using a Binary Read method. The following code sample is used in VB.NET and similar code is used in VB6 - (Assume variable...
4
by: Jonathan | last post by:
I have a SQL stored procedure for adding a new record in a transactions table. It also has two return values: CounterID and IDKey. I want to create a webservice that accepts the 10 input...
7
by: mirandacascade | last post by:
The questions are toward the bottom of this post. Situation is this: 1) Access 97 2) Multi-user appplication 3) SQL Server 2000 4) Sporadically (i.e. less than 1% of the time) encounter the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.