-2

I have two class. how do I get the value of a different class. for example ...

public class bat
{
    public int a; 

    void valueA()
    {
        int a = 20; 
    }
} 

class nah
{
   public int b;

   void valueB()
   {
       b = a; // variable a from class bat. 
   }   
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
pramitafirnanda
  • 71
  • 1
  • 2
  • 12
  • 2
    Are you familiar with the concepts of [inheritance](http://msdn.microsoft.com/en-us/library/ms173149.aspx) and [composition](http://social.msdn.microsoft.com/Forums/en-US/0b6677dc-3eb6-4eae-9c8f-c042ccbfefb3/what-is-composition-in-c-?forum=csharplanguage)? If so: what are your motives for not using either of them? – Jeroen Vannevel Mar 29 '14 at 20:41

4 Answers4

1

You can pass an instance of bat into nah, then reference a:

public class bat
{
    public int a; 

    void valueA()
    {
        int a = 20; 
    }
} 

class nah
{
   public int b;

   void valueB(bat someBat)
   {
       b = someBat.a;
   }   
}

Or make a a static variable, so that you don't need an instance of bat to reference it:

public class bat
{
    public static int a; 

    void valueA()
    {
        int a = 20; 
    }
} 

class nah
{
   public int b;

   void valueB(bat someBat)
   {
       b = someBat.a;
   }   
}

Regarding that void valueA() method in bat...

In the first case, calling bat.valueA() is not possible from outside the class because it's not public. Also you're assigning 20 to a local variable that goes out of scope when valueA() ends.

In the second case, you couldn't call valueA() without making it public and static... and you'd still be assigning to a local variable, not the class-level variable.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

A class is only a template for the creation of objects. You must create instances of these classes i.e. you must create objects with the new keyword.

class nah
{
   public int b;

   void valueB()
   {
       bat batObject = new bat();
       batObject.valueA(); // Set the value.
       b = batObject.a; // variable a from class bat. 
   }   
}

In .NET programming it is usual to use PascalCase for class names, method names and property names. Many programmers use the prefix "_" for field names. Field names and variable names are written in camelCase. Fields are usually kept private. Use properties for publicly accessible values.

public class Bat
{
    public int A { get; set; }

    public void SetValueA()
    {
        A = 20;
    }
} 

public class Nah
{
   public int B { get; set; }

   public void SetValueB()
   {
       Bat bat = new Bat();
       bat.SetValueA();
       B = bat.A;
   }
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

you can do like this:

public class bat
{
public int a; 

void valueA()
{
    int a = 20; 
}
} 

class nah
{
public int b;

void valueB(bat bt)
{
   b = bt.a; // variable a from class bat. 
}   
}
Henka Programmer
  • 433
  • 6
  • 18
-1

You can have the nah inherit bat.

Example:

public class bat{
    public int a; 
    void valueA(){
    a = 20; }
} 
------ 
class nah : bat {
   public int b;
   void valueB(){
   b = a; // variable a from class bat. 
   }   
}

Read more here

This implies that these classes are both in the same application. If they are not, then you can also accept a type of bat as a parameter to valueB

void valueB(bat Bat)
{
    b = Bat.a;
} 

Or pass values by HTTP requests, read about it here.

Community
  • 1
  • 1
Liam McInroy
  • 4,339
  • 5
  • 32
  • 53
  • You can also use composition, turn `bat` into a singleton or use an http request to call an API that returns the value of `a`. The question as posed is too broad to answer and if you're going to answer you should at the very least provide the reasonable alternatives and how they correlate to eachother. – Jeroen Vannevel Mar 29 '14 at 20:38
  • @JeroenVannevel True.... Check edits... – Liam McInroy Mar 29 '14 at 20:44