panky sharma
panky sharma

Reputation: 2169

Access function return type as Tuple in C#

Its a really naive question but I haven't used tuples before so asking this out. I am facing some issue while accessing a function return value which is a tuple in another function.

Here is my code :

  public Tuple<string, string, string> CreateTransaction()
        {
            var tCompResp = new Tuple<string, string, string>(T, TCode, Message);
            return tCompResp;
}

Now I am not sure how can I access this return values of above function in other page

  var tResp = new Tuple<string, string, string>((objManager.CreateTransaction()).Item1, (objManager.CreateTransaction()).Item2, (objManager.CreateTransaction()).Item3);

In Above line I am could access the return tuple but problem is I am calling same function ( CreateTransaction() ) 3 times

So my question is how can I get function's tuple return value in single call?

 String tRespon = objManager.CreateTransaction()   ///Suppose if return type is string.

I will use this value in another function like this

Update(tRespon.Item1, tRespon.Item2, tRespon.Item3);

Upvotes: 1

Views: 1434

Answers (3)

Kamil Budziewski
Kamil Budziewski

Reputation: 23087

Try this:

Tuple<string,string,string> tRespon = objManager.CreateTransaction();

Update(tRespon.Item1,tRespon.Item2,tRespon.Item3);

CreateTransaction returns Tuple<string,string,string> not string

always you can use var instead Tuple<string,string,string>

var tRespon = objManager.CreateTransaction();

Note that var is just another way to write it, not a type.

but better way should be using new class to return this data:

public class ConnectionInfo
{
   public string T;
   public string Code;
   public string Message;
}

public ConnectionInfo CreateTransaction()
{
     var tCompResp = new ConnectionInfo{T=T, Code=TCode, Message=Message};
     return tCompResp;
}

then

var tRespon = objManager.CreateTransaction();
Update(tResp.T, tResp.Code, tResp.Message);

Upvotes: 1

Benoit Blanchon
Benoit Blanchon

Reputation: 14521

Even shorter:

var tRespon = objManager.CreateTransaction();
Update(tRespon.Item1,tRespon.Item2,tRespon.Item3);

Upvotes: 2

thumbmunkeys
thumbmunkeys

Reputation: 20764

you dont need to create a new Tuple:

   var tResp = objManager.CreateTransaction();
   Update(tResp.Item1, tResp.Item2, tResp.Item3);

CreateTransaction() already returns a Tuple that you can use right away

Upvotes: 2

Related Questions