Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
I am declaring a function in class1 of windows form.the function is

public static void sum (double a,double b)
{
      double sum1;
      sum1=a+b;

}//in class1
Now in form1. I am writing the code
double a=2,b=3,sum1=0;

(in Button1)

class1.sum(a,b)//calling function from class1;


I want that the value of sum1 should be changed by class1.sum(a,b) function (i.e. it should be 5 ); but the output value is not changing (i.e.it remains 0).

I do not want to use return data type of class1.sum(a,b) function.

Please help me!
Posted
Updated 9-May-11 22:32pm
v3
Comments
Dalek Dave 10-May-11 4:32am    
Edited for Grammar and Readability.
Sergey Alexandrovich Kryukov 11-May-11 0:19am    
"I don't want"... how cares? Have 1.
--SA

Hi adil09,

You have several way to do so:
first :

public static double sum (double a,double b)
{
      return a+b;
}//in class1


Button1 :
sum1 = class1.sum(a, b);



or Second :

public static void sum (double a, double b, out double sum1)
{
      sum1=a+b;
}//in class1


Button1 :

class1.sum(a, b, out sum1);//calling function from class1


I hope this help,
:)
 
Share this answer
 
v3
Comments
OriginalGriff 10-May-11 4:13am    
Michael, that won't work: If you do not declare the parameters as Ref or Out, then you can't affect the value outside the method...

[edit]Sorry, spelled your name wrong :O - OriginalGriff[/edit]
Pong D. Panda 10-May-11 4:16am    
The second solution is wrong. sum1 is the parameter, it will not assign a value to your sum1 variable.
Michael Waguih 10-May-11 4:22am    
Thanks Prerak Patel for editing I was missing out
Dalek Dave 10-May-11 4:32am    
Nice to see you edited it! :)
adil09 10-May-11 6:28am    
2nd Method is working:).Thank you
You can use an output parameter (as described here[^]), if you dont want to return a value.
 
Share this answer
 
Comments
Dalek Dave 10-May-11 4:32am    
Nice.
adil09 10-May-11 6:26am    
Thanks......
Your method does nothing.
Yes, it adds the two numbers together, and puts the result in a variable called "sum1".
But then, the method ends. Since "sum1" is a local variable, it is thrown away when the method exits.

If you want to preserve the result for later use, there are only three practical ways:
1) Return the result as a datatype - I know, you don't want to, but it is the most sensible way to do it:
public static double sum (double a,double b)
   {
   return a + b;
   }

2) Return the result as a static variable (since your method is static, you can't return it as a class variable)
private static double result;
public static double sum (double a,double b)
   {
   result = a + b;
   }
This has the disadvantage that if you have two instances of your class, they will both overwrite the same variable.
3) Change your method to a non-static method, and return the result via a class level variable:
private double result;
public double sum (double a,double b)
   {
   result = a + b;
   }
This is a clumsy solution, particularly if there are many routines doing this for different purposes - each needs it's own variable.

Use option (1) - it is the easiest, most flexible, and most friendly!
 
Share this answer
 
Comments
Dalek Dave 10-May-11 4:33am    
Good Call.
adil09 10-May-11 6:34am    
Thanks:)
I have actually more than one values(about 5) that i will have to be return. So, i want instead of returning values, just change them in class and then use the new values in programme.
If i get you correctly you need to do something like this

C#
class myClass
    {
        public static void sum(double a, double b, out double sum1)
        {
            sum1 = a + b;
        }//in class1
    }


then you call it like this

C#
double a = 2, b = 3, sum1 = 0;
           myClass.sum(a, b,out sum1);
           MessageBox.Show(sum1.ToString());


and that should give you the correct result.

Hope this helps.
 
Share this answer
 
Comments
adil09 10-May-11 6:25am    
Thanks....... .it is working:)

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