Access variable from another method in another class

前端 未结 7 1372
闹比i
闹比i 2020-12-12 06:27

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

public class ABC
{
   public          


        
7条回答
  •  一整个雨季
    2020-12-12 06:58

    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.

提交回复
热议问题