SMI
SMI

Reputation: 311

Access variable from another method in another class

Suppose I have a file ABC.CS in that file I have a class ABC as below and method Test():

public class ABC
{
   public static void Test()
      {
            int a=10;
            int b=11;
            int c=12;
               //many many variables
      }
}

I want to access a, b ... etc in another method of another class of another file say

Note: Variable may be any of type, like int, float, and DataTable etc

Updated Seems like people ignored static keyword, please give working example

How can I do this ?

Thanks.

Upvotes: 1

Views: 43774

Answers (7)

stratovarius
stratovarius

Reputation: 3880

Using static variables/functions is one thing; accessing a variable from outside of its scope is another thing.

From MSDN:

If a local variable is declared with the Static keyword, its lifetime is longer than the execution time of the procedure in which it is declared. If the procedure is inside a module, the static variable survives as long as your application continues running.

And also static variable will exist per class, not instance.

So, I assume that you will need instances of ABC class:

public class ABC
{ 
    private int a; //also you can add 'readonly' if you will not change this value in       its lifetime
    private int b;
    private int c;

    public int A()
    {
        get { return a; }
    }

    public int B()
    {
        get { return b; }
    }

    public int C()
    {
        get { return c; }
    }

    public void Test()
    {     
        a=10;
        b=11;
        c=12;
        //many many variables
    }
}

From another class you can call:

ABC abcInstance = new ABC();
abcInstance.Test(); //you set the values
int aTemp = abcInstance.A(); // now you have the 'a' value of ABC's property.

Upvotes: 4

Chris Serrao
Chris Serrao

Reputation: 135

If you are calling the method XYZ.myMethod from Test then myMethod must be a static method. In this case for value type variables, call the method using ref for the value types.

If you need to access the variable, as it seems you need to access a Datatable, directly using the class name or the object reference, you will need to declare the variable as a class variable and make it static.

Once you've done this, try initializing the datatable in the static constructor :

static ABC()
{
ABC.SomeDataTableVariable = new DataTable();
}

Please try this out.

Upvotes: 1

Daniel Mann
Daniel Mann

Reputation: 59055

The other answers I've seen so far are giving what I would consider extremely bad advice.

Without more a more focused example, it's hard to say what the best approach would be. In general, you want to avoid your methods having side effects -- it makes it harder to debug and test your application. Basically, if you call the method 100 times with the same inputs, it should return the same results 100 times.

In general, a good approach is to have your methods return something that represents the results of the operation it's performing.

So, you have a class or struct that represents your return values:

public struct SomeMethodResult
{
    public int WhateverProperty {get;set;}
    public int WhateverOtherProperty {get;set;}
}

And then return an instance of that class as necessary:

public SomeMethodResult SomeMethod()
{
   var result = new SomeMethodResult();
   result.WhateverProperty = 1;
   result.WhateverOtherProperty = 2;
   //etc
   return result;
}

Upvotes: 1

pareto
pareto

Reputation: 196

Theoretically you cannot do so, but if you change the method a little bit you may get what you want.

Declare a list of objects publicly and access it from the other class.

public  List<object> differentVarColl = new List<object>();

    void MethodA()
    {

        int a = 0;
        int b = 10;
        double c = -90;

        DataTable dtData = new DataTable();

        //Do rest of things


        differentVarColl.Add(a);
        differentVarColl.Add(b);
        differentVarColl.Add(c);
        differentVarColl.Add(dtData);

    }

From other method, iterate the list and store them in new some variables depending on their type.

I hope this helps.

Upvotes: 1

invernomuto
invernomuto

Reputation: 10221

You cannot access to local vars by reflection, but you can access to static vars

Upvotes: 1

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101711

You can't. You should define your variables outside of your method and make them public.Or if your other class inherits from ABC then you can make them protected.

Upvotes: 1

Sai Avinash
Sai Avinash

Reputation: 4753

Declare the variables as public and static out side of the method

You can access them by just using ClassName.VariableName

hope this helps..

Upvotes: 3

Related Questions