Click here to Skip to main content
15,890,440 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two class. how do I get the value of a different class.
for example ...

class bat () {
    a = 20;
}
------
class nah() {
   ren = a; // variable a from class bat.
}
Posted
Comments
[no name] 29-Mar-14 16:31pm    
bat b = new bat(); var ren = b.a;?

Assuming of course that your classes were correctly constructed and you are able to access the value of a outside of the bat class.

1 solution

Usually a class doesn't get a values from another class. On the other hand, an object, instance of a class could get a value from another object, instance of a different class.
That could happen in several ways. The most evident uses a public 'accessor':
C#
class A
{
  int i;
  public A(int k){i = k;}
  public int the_i
  {
    get { return i; }
  }
}
class B
{
  int j;
  public B(int k){ j = k;}
  public B(A a){ j = a.the_i;} 
}
 
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