English 中文(简体)
复习职能C#的公共变量
原标题:Public variable in Recursive function C#

在C#中,我有一项可收回的职能,即想篡改一个全球性的变量(即假定它属于全球范围,而此前它是由公众宣布的)。 由于某种原因,我不知道它无法看到这一具体职能中的公共变数。 它可以在我的法典中将其放在第一功能中,但不能在第二功能中看到它需要查阅和修改,以节省时间,打开档案文件。

是否有什么理由可以进入? 如果是的话,如何绕过它?

预言如此之多!

public int[] timeInfo = new int[2];
   private void ListDirectory(TreeView treeView, string path) 
        {     
            treeView.Nodes.Clear();   
            var rootDirectoryInfo = new DirectoryInfo(path);    
            treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo)); 
        }

   private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo) 

   {

       var directoryNode = new TreeNode(directoryInfo.Name);

       foreach (var directory in directoryInfo.GetDirectories())
            directoryNode.Nodes.Add(CreateDirectoryNode(directory));

       foreach (var file in directoryInfo.GetFiles())            
       {

           int check =0;                         
           try                
           {

               string s = "";                    
               s = directoryInfo.FullName + "\" + file.Name;                    
               List<string> row, row2, row3 = new List<string>();

               using (StreamReader readFile = new StreamReader(s))
               {

                   row = (readFile.ReadLine().Split( , ).ToList());
                   try 
                   {                           
                       row2 = (readFile.ReadLine().Split( , ).ToList());
                       //timeInfo[0] = row2[0];
                   }
                   catch { check = 1; }
                   try
                   {
                       row3 = (readFile.ReadLine().Split( , ).ToList());
                       //timeInfo[1] = row3[0];
                   }

                   catch { }
                }

                TreeNode[] headerNodes = new TreeNode[row.Count];

                for (int i = 0; i < row.Count; i++)
                {

                    headerNodes[i] = new TreeNode(row[i]);
                    if (check == 1)
                    {                       
                        headerNodes[i].BackColor = Color.Red;
                        headerNodes[i].ForeColor = Color.White;
                    }

                }
                directoryNode.Nodes.Add(new TreeNode(file.Name, headerNodes));
            }
            catch 
            {
                directoryNode.Nodes.Add(new TreeNode(file.Name));
            }
        }         
        return directoryNode; 
    }     
最佳回答

第二项功能是静态的,变量只在物体的情况下存在。

问题回答

这种方法固定不变。 变量不是。 不能以静态方式接触非静态(instance)阶层成员。 某类公共变量不是全球性的。 你们必须公开,使之具有全球性(我从未建议使用全球变量),例如:

public static int[] timeInfo = new int[2];

你们需要把它固定下来,才能看到你静态的职能。

你们也需要把变数定义为静态:

public static int[] timeInfo = new int[2];

你的变数必须固定,因为你的方法。 由于该方法是固定的,因此只能看固定的变数。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签