从另一个类中的另一个方法访问变量 [英] Access variable from another method in another class

查看:90
本文介绍了从另一个类中的另一个方法访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个文件ABC.CS,该文件中有一个如下所示的类ABC和方法Test():

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

我想使用另一个文件的另一类的另一种方法访问ab ...等

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

  • 文件:XYZ.cs
  • 类别:XYZ
  • 方法:MyMethos().
  • File : XYZ.cs
  • Class: XYZ
  • Method: MyMethos().

注意:变量可以是任何类型,例如intfloatDataTable

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

已更新 好像人们忽略了static关键字,请举个有效的例子

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

我该怎么做?

谢谢.

推荐答案

声明方法外的变量publicstatic

您只需使用ClassName.VariableName

希望这会有所帮助.

这篇关于从另一个类中的另一个方法访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆