0

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.

4
  • Can you show how are you trying to use that list? Commented Feb 11, 2014 at 22:24
  • What do you want your list to hold? this.Properties isn't referred to anywhere in your snippet, so I'm not surprised that your list doesn't have it... Commented Feb 11, 2014 at 22:26
  • @Ben probably, this.Properties are this.FileID, Name, etc. So it is possible to access them like FileClassList[i].Name ... Commented Feb 11, 2014 at 22:27
  • @JleruOHeP If that's the case, then instances of FileClass would serve just fine; without more info, it's impossible to know exactly what is being asked. Commented Feb 11, 2014 at 22:36

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah - I was not re-instantiating the class - nice catch!
1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.