如何从静态类C#中删除旧值? [英] How can I remove old values from static class C#?

查看:309
本文介绍了如何从静态类C#中删除旧值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想将列表项传递给一个类到另一个类。我用过static关键字。但现在,当应用程序运行时仍然存在旧的变量。



实际上我有一个带复选框的列表视图。选中复选框后,单击一个按钮。然后弹出另一个窗口页面,其中包含关于每个Parent_File_Name和UserId的listview。我面临的问题是,在运行这个windows应用程序后,再次点击该按钮,静态变量包含整个列表项。怎么办呢?你能帮我吗?



请帮帮我......



我尝试了什么:



在第一堂课,



Hello everyone,

I want to pass list items to one class to anther class. I have used static keyword. But now, when the application is running still old variable is there.

Actually I have a listview with checkboxes. After checked checkboxes then click a button. Then popup another window page that contain listview with respect to each Parent_File_Name and UserId . I am facing the issue is, after running this windows apps then many time click on the button again both static variable contain whole list items. How can do this? Can you get me?

Please help me...

What I have tried:

In 1st class,

foreach (DocumentsUser item in listView1.SelectedItems)
{
    ImgName.Add(item.Parent_File_Name);
    UserId.Add((int)item.UserId);
}







private static List imgName = new List();
      public static List ImgName { get { return imgName; } }

      private static List userId = new List();
      public static List UserId { get { return userId; } }





第二课拿这些静态变量,





2nd class taking those static variables,

foreach (var userId in Qscan.UC_FileMgmt.UserId)  
           {  
               foreach (var imgName in Qscan.UC_FileMgmt.ImgName)  
               {  

推荐答案

因为你使用了静态变量,它在所有实例中都会有相同的值(这可能非常糟糕),所以如果你在一个实例中添加元素,那么当你在另一个实例中时它们就会存在...

如前所述,这可能非常糟糕,并且肯定没有建议...

但是,如果你想要的话,你应该清理清单,然后重新填充它。 ..

List(T).Clear Method(System.Collections.Generic) [ ^ ]
Because you used static variable it will have the same value across all instances (which is probably very bad), so if you add elements form one instance those will be there when you are in an other instance...
As told before this probably very bad and for sure not advised...
However, if by any chance this exactly what you want, you should clear the list before refilling it...
List(T).Clear Method (System.Collections.Generic)[^]


这篇关于如何从静态类C#中删除旧值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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