Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
i want to return a value using only void method ..
Posted
Updated 28-Mar-14 22:24pm
v2
Comments
[no name] 29-Mar-14 4:45am    
Where is your code? Returning a value from a void method is not good design - why is the method void if you what to return a value.

This will help you....

Returning value from Void Function[^]
 
Share this answer
 
void methods to not return values, however you can return changed values with ref or out:
C#
void method1(string p1, ref int i)
{
   i = 10; // will be changed for the caller 
}

void method2(string p1, out int j)
{
   j = 20; // you must set an 'out' parameter or the compiler will complain
}

http://www.dotnetperls.com/out[^]
http://www.dotnetperls.com/ref[^]
 
Share this answer
 
I'm afraid that voids cannot return anything, hence why they are called 'voids' - 'void' is a fancy word that means 'nothing' or 'empty' in English.

You can always try using an out, like this:
C#
string test = "Starting Value"
Console.WriteLine(test); //output: Starting Value
Modify(out test);
Console.WriteLine(test); //output: Modified Value


And the void Modify is defined like this:
C#
void Modify(out string input)
{
   input = "Modified Value";
}
 
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