473,804 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

method that returns more than one value?

VM
In my Win app, I have an event that calls method myMethod.Proces s() 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_Clic k_1(object sender, System.EventArg s e)
{
myMethod m1 = new myMethod();
myStruct Struct1 = new myStruct();
zipStruct = myMethod.Proces s();
}
--------------------------------
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_Clic k_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 3017
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.Proces s() 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_Clic k_1(object sender, System.EventArg s e)
{
myMethod m1 = new myMethod();
myStruct Struct1 = new myStruct();
zipStruct = myMethod.Proces s();
}
--------------------------------
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_Clic k_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.Applicat ionException

--
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******** ******@tk2msftn gp13.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.co m>
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
36418
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 variable with the "ref" syntax then it gets passed as a reference to the object and any changes to
4
1799
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 results to a variable. I have been using a couple of ways so far in my project.: 1. strFirst=objDR("Fname") 2. strFirst=objDR.GetOrdinal("Fname")
18
4753
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
0
1685
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 specified date. - DeleteProfile. Enables you to delete a profile associated with a specified username. - DeleteProfiles. Enables you to delete a set of profiles. - FindInactiveProfilesByUserName. Returns a collection of ProfileInfo
6
1152
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 unassigned local variable retFinalPercentage. Please look at the code below and offer your advice. Thanks public static double SelectFinalPercent2(int imyFuture) { int aAge = {55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65};
7
3314
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
23
2841
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 declarations etc.) FileOpen(iFileIn, sInputFile, OpenMode.Binary, OpenAccess.Read)
4
7149
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 parameters and returns the two return values. My C# programmer here says that webservice methods can only return 1 value per method. Is that right? Though I haven't ever created a webservice, I would have thought that a method could return a whole lot...
7
11061
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 following error: 3218 Couldn't update; currently locked
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10354
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.