Reputation: 1835
I have a Non Static class which contains a number of properties
private static List<FileClass> FileClassList = new List<FileClass>();
internal void AddFile(Alphaleonis.Win32.Filesystem.FileInfo finfo)
{
this.FileID = FileIDCounter;
this.Name = finfo.Name;
this.FullName = finfo.FullName;
this.Attributes = GetFileAttributes(finfo);
this.CreationTime = finfo.CreationTime;
this.Extension = finfo.Extension;
this.isReadOnly = finfo.IsReadOnly;
this.LastAccessTime = finfo.LastAccessTime;
this.LastWriteTime = finfo.LastWriteTime;
this.Length = finfo.Length;
Interlocked.Increment(ref FileIDCounter); // As a Static value this is shared amongst all the instances of the class
// Interlocked.Increment is the Thread Safe way of saying FileIDCounter ++;
FileClassList.Add(this);
if (FileClassFileAdded != null) FileClassFileAdded(this);
}
Although the Class is added FileClassList.Add(this); the final result is a FileClassList filled with whatever the last instance of the class contained and not the this.Properties values.
So, how do I add the current instance of the FileClass to the FileClassList so that the contents of the FileClassList contains the different instances of the FileClass.
Upvotes: 0
Views: 119
Reputation: 19330
Here is what your issue most likely is...
Having this:
internal void AddFile(Alphaleonis.Win32.Filesystem.FileInfo finfo)
Somewhere you probably do:
MyClassWhichAddsAFile cls = new MyClassWhichAddsAFile();
cls.AddFile(fileInfo1);
cls.AddFile(fileInfo2);
cls.AddFile(fileInfo3);
While you need to do (under this design):
MyClassWhichAddsAFile cls1 = new MyClassWhichAddsAFile();
cls1.AddFile(fileInfo1);
MyClassWhichAddsAFile cls2 = new MyClassWhichAddsAFile();
cls2.AddFile(fileInfo2);
........
Again, I am not discussing your design here or how to do it right. I am telling you that your problem is probably comes out from this situation
Upvotes: 1
Reputation: 65476
I think you have your design slightly askew. I don't think that AddFile should be part of FileClassList rather. However in leiu of not having another place to hold it. I'd say do this:
internal static void AddFile(Alphaleonis.Win32.Filesystem.FileInfo finfo, FileClass theClass)
{
theClass.FileID = FileIDCounter;
theClass.Name = finfo.Name;
theClass.FullName = finfo.FullName;
theClass.Attributes = GetFileAttributes(finfo);
theClass.CreationTime = finfo.CreationTime;
theClass.Extension = finfo.Extension;
theClass.isReadOnly = finfo.IsReadOnly;
theClass.LastAccessTime = finfo.LastAccessTime;
theClass.LastWriteTime = finfo.LastWriteTime;
theClass.Length = finfo.Length;
Interlocked.Increment(ref FileIDCounter); // As a Static value this is shared amongst all the instances of the class
// Interlocked.Increment is the Thread Safe way of saying FileIDCounter ++;
FileClassList.Add(theClass);
if (FileClassFileAdded != null) FileClassFileAdded(theClass);
}
A better approach would be to create a contrstuctor on FileClass that takes a FileInfo and fills these properties in and call it like this:
var someFileClass = new FileClass(theFileInfo);
FileClass.AddFile(someClassFile);
and AddFile would be:
internal static void AddFile(Alphaleonis.Win32.Filesystem.FileInfo finfo, FileClass theClass)
{
Interlocked.Increment(ref FileIDCounter); // As a Static value this is shared amongst all the instances of the class
// Interlocked.Increment is the Thread Safe way of saying FileIDCounter ++;
FileClassList.Add(theClass);
if (FileClassFileAdded != null) FileClassFileAdded(theClass);
}
even then I think the AddFile should be a method on the the caller not the callee!
Upvotes: 1