Click here to Skip to main content
15,909,205 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
This is my code and tried to experiment on it.

C#
public string _getContact(int contactID)
{

//i had also do something in my contactID

string val1="";
string val2="";

//something to do in this part

return val1;
return val2;

}


And how do i possibly retrieve it?

C#
//dont know what to do this part
string pasval1 = _getContact();
string pasval2 = _getContact();
Posted
Updated 23-Jul-12 0:39am
v3
Comments
Herman<T>.Instance 23-Jul-12 6:40am    
you defined a method that needs an int as parameter and then you don't know how to use your method?
newbie011292 23-Jul-12 6:43am    
thats why im i asking, i know how to use the method but i dont know how or it is possible to have more than 1 return values =)

You can either use out parameters : http://msdn.microsoft.com/en-us/library/t3c3bfhx(v=vs.71).aspx[^]

Or better: Return an object
C#
public class Contact{
   public string Firstname {get;set;}
   public string Lastname {get;set;}
}


....

public Contact _getContact(int contactID)
{
   Contact c = new Contact();
   c.Firstname = "Bill";
   c.Lastname = "Gates";
   return c;
}
 
Share this answer
 
v2
Comments
pradiprenushe 23-Jul-12 6:50am    
My 5 for giving both option.
Espen Harlinn 24-Jul-12 4:38am    
5'ed!
Hi,

You can't use multiple return statements as you have shown. When the return statement gets executed the control goes back to the previous thread. Please check below link for more clarity.

http://csharp.net-tutorials.com/basics/functions/[^]

In this particular case you can use an array or a comma separated string to combine multiple values in return.

Hope this helps
Sebastian
 
Share this answer
 
Comments
newbie011292 23-Jul-12 6:44am    
thanks for the link il try sir
StianSandberg 23-Jul-12 6:51am    
"array or a comma separated string to combine multiple values in return" serious?
hello,

In this case you can pass the List<string>
and return the list. both value will you get on index of list

XML
List<String> list = new List<String>();

list.add("aa");
list.add("bb");

return list;
 
Share this answer
 
Comments
newbie011292 23-Jul-12 6:59am    
thanks
Use as follows

C#
public static List<string> _getContacts(int contactID)
        {
            List<string> val = new List<string>();
            //i had also do something in my contactID

            string val1 = "ABC";
            string val2 = "DEF";

            //something to do in this part

            val.Add(val1);
            val.Add(val2);
            return val;
        }


Retrieve data as follows
C#
List<string> VAL = _getContacts(contactID);


Now you can use any element using index,. i.e., VAL[0],VAL[1]
 
Share this answer
 
v5
Comments
newbie011292 23-Jul-12 6:59am    
thanks sir but i dont get that retrieving part what if i want to put the value in like textBox1.Text? THANKYOUSOMUCH SIR=)
JakirBB 23-Jul-12 7:01am    
TextBox1.Text = VAL[0];
TextBox2.Text = VAL[1];

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