15,882,464 members
See more:
If there is one class having two member function then how to access the data of member function by the other member function.

e.g there are two functions:
public void accept()
{
Console.WriteLine("Enter the first digit");
Console.WriteLine("Enter the second digit");

}

Then how can we access the value of a and b for another function for calculation?

public void calc()
{
c=a+b;
}
Posted
Updated 22-Aug-11 3:24am
v2

## Solution 2

I guess you are pretty new to development :)
there are a few ways to achieve what you want, the simplest (but not necessarily the best, depending what you are doing) is:

C#
public void Accept()
{
Console.WriteLine("Enter the first digit");
Console.WriteLine("Enter the second digit");
}

int Calculate(int firstValue, int secondValue)
{
return firstValue + secondValue;
}

You can of course just add a and b in the Accept method, but I assume you don't want to do this. If you are doing a lot of this sort of stuff, you should create something like a Calculator class, the exact implementation depends on what you need to do. I strongly suggest you google for "C# Properties" and "Object Orientation". Object Orientation is really worth learning as you learn the language, otherwise you will end up fighting the language rather than working with it.

v2
pcprincecharles 22-Aug-11 9:54am
return keyword is not allowed in c#.
and i am telling the whole code find out where i am wrong

using System;
class sum
{
int a,b,c;
public void accept()
{
Console.WriteLine("Enter the first digit");
Console.WriteLine("Enter the second digit");
calc(a, b);
}

public void calc(int firstValue, int secondValue)
{
c=Convert.ToInt32(a + b);
Console.WriteLine("no.is ",c);
}
}

class sum1
{
static void Main()
{
sum ob=new sum();
ob.accept();
sum ob1=new sum();
ob1.calc(2,3);

}
}
phil.o 22-Aug-11 10:32am
return keyword not accepted in C# ? Wow, obviously we are not living on the same planet ^^
Keith Barrow 22-Aug-11 10:35am
I assure you return is allowed by c#, if it isn't I must have hallucinated the last decade or you have some other language calling itself c#. I suggest you buy yourself a introductory book or find a beginner's online tutorial.
phil.o 22-Aug-11 10:34am
Hi Keith, I modified your Calculate() method so that it uses the right parameters names :)
Moreover, this method could be made static as it does not use any member variable.
Keith Barrow 22-Aug-11 10:37am
Thanks, I was on my way out of work so I didn't try compiling it...

## Solution 1

Where are a, b and c declared?

You need to read a reference book about the 'scope' of a variable, it is very important. If you had declared a and b inside your function calc, you would not be able to access them from outside that function. If you declare them within the class declaration then any function in that class would be able to access then, however neither of your functions appear to be within a class (although I am not familiar with C#, so I could be wrong about that).