Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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,

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


C#
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,

C#
foreach (var userId in Qscan.UC_FileMgmt.UserId)  
           {  
               foreach (var imgName in Qscan.UC_FileMgmt.ImgName)  
               {  
Posted
Updated 25-Dec-16 22:21pm
Comments
Kornfeld Eliyahu Peter 25-Dec-16 8:13am    
Do you ever CLEAR the list?
Rajesh Kumar 2013 26-Dec-16 4:05am    
No, How can I clear list before call again and again in C#?
Tomas Takac 25-Dec-16 14:55pm    
Do not use static variables, static is evil! You need to understand what the life-cycle of your objects is. When you are creating a new window then pass a new view model and populate it with your list. Also using two separate lists for something that is clearly one entity is bad. Create an other class to encapsulate userid and imgname. Overall my feeling is you are trying to tackle a problem that is too much for you. Nothing bad with that really but to be able to learn you need to break it down into smaller pieces.

1 solution

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)[^]
 
Share this answer
 
Comments
Rajesh Kumar 2013 26-Dec-16 5:43am    
Thank you sir! Its working. God bless you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900