Click here to Skip to main content
15,885,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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");
a=Convert.ToInt32(Console.ReadLine());	
	   Console.WriteLine("Enter the second digit");
b=Convert.ToInt32(Console.ReadLine());	

   }


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

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");
  int a=Convert.ToInt32(Console.ReadLine()); 
  Console.WriteLine("Enter the second digit");
  int b=Convert.ToInt32(Console.ReadLine()); 
  int answer = Calculate(a, b);
}

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.
 
Share this answer
 
v2
Comments
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");
a=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the second digit");
b=Convert.ToInt32(Console.ReadLine());
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...
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).
 
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