Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sorry in advance if this is a very newbie question.

I have a routine that is used a lot throughout the program and subforms programs. To clean things up, I would like to have the code in one place, like a .dll. The problem is that it needs to return three values, two bool, and one string. I read I can do this with an object array, but I have no idea how to set it up and what the header would look like.

What I have tried:

private void VerifyDate(string thisDay, object [] status)
        {
Posted
Updated 4-Aug-22 14:39pm
Comments
PIEBALDconsult 3-Aug-22 21:22pm    
Define a class or use a Tuple.

Quote:
The problem is that it needs to return three values, two bool, and one string.
Depends on what you need, and how much old C# language you need to support. But the latest version of C# can handle a Tuple easily.
C#
private Tuple<bool, bool, string> VerifyDate(string thisDay, object[] status) {
It should work just fine, and this would return the three values.

Read more here:
Tuple types - C# reference | Microsoft Docs[^]

More latest versions of the language will enable you to return the tuple structure with a more syntactic-sugar syntax:
C#
private (bool, bool, string) VerifyDate(string thisDay, object[] status) {
However, if you need to label the fields, it would be better to simply create a struct. The structures would provide better performance over class simply because they are put on the stack and not the heap.
 
Share this answer
 
v2
Comments
PaulaJoannAllen 4-Aug-22 10:09am    
It almost works, but I get a
Compiler Error CS0161, method': not all code paths return a value

The code is long, so I will include what I think you need.

All the variables are declared and set in the code.

        
        //public (bool, bool, string) VerifyDate(string thisDay, object[] status)
        public Tuple <bool, bool, string> VerifyDate(string thisDay, object[] status)


                
                //status[3] = (dateError, dateNotValid, outDate);
                var retruneArray = new Tuple<bool, bool, string>(dateNotValid, dateError, outDate);

                return retruneArray;
                //return var retruneArray = new Tuple <bool, bool, string>(dateError,dateNotValid,outDate;
                //return status;
                //return ((bool, bool, string))(status[3] = (dateError, dateNotValid, outDate));
Afzaal Ahmad Zeeshan 4-Aug-22 14:47pm    
Deleted
Afzaal Ahmad Zeeshan 4-Aug-22 15:26pm    
That means you are not putting a return statement in one of the branches in the code. Since you have not shared the entire code, we don't know where the problem is.
PaulaJoannAllen 4-Aug-22 20:38pm    
You were right; I found it in an if statement. I don't know how many times I looked there and did not see it.

Thank you
Added necessary code to an if statement.
 
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