Access variable from another method in another cla

2020-02-16 04:46发布

问题:

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

  • File : XYZ.cs
  • Class: XYZ
  • Method: MyMethos().

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.

回答1:

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

You can access them by just using ClassName.VariableName

hope this helps..



回答2:

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.



回答3:

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



回答4:

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.



回答5:

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;
}


回答6:

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.



回答7:

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.


标签: c# oop