List problems

I have 2 lists of classes that I have constructed.  1 of the 2 classes has the other list inside of it.  So I have to populate one list before I can populate the second list.  I have no problems creating the first list, but as I populate the second list I loose the first list.  Here is some code to help better illistrate my problem.

List<FileList> FList = new List<FileList>();

List<DriverEntry> DList = new List<DriverEntry>();

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

{

string[] temp = infoData[ i ];

if (previous[2] != temp[2])

{

DriverEntry DE = new DriverEntry(type, hashD, description, versionD, provider, DateTime.Now, FList); //DE Has the contents of the list just fine.  When I add it to DList it dissapears WHY

FList.Clear();

DList.Add(DE);

first = 0;

}

if (previous[2] == temp[2]) ;

{

string fileName = temp[0];

string test = appFolder + "Src\\" + temp[1] + "\\" + fileName;

FileInfo fi = new FileInfo(test);

long fiSize = fi.Length;

string version = temp[3];

string tmp = fileName + version;

Int64 hash = tmp.GetHashCode();

if (hash < 0)

{

hash *= -1;

}

FileList FL = new FileList(hash, fiSize, fileName);

FList.Add(FL);

}

if (first == 0)

{

type = temp[1];

description = temp[2];

versionD = temp[3];

provider = temp[4];

previous = temp;

string tmp = type + description + versionD + provider;

hashD = tmp.GetHashCode();

if (hashD < 0)

{

hashD *= -1;

}

first++;

}

}

I have marked with red commets where I am having problems.  when I create the object to be placed into the second list all of the values are correct.  It correctly hold the first list and all the values specific to it's class.  When I add it to the list of objects it erases the first list.  It say's the list for the first object in the list is null.  I don't understand why.  If you need more code let me know.  Thanks, Matt



Answer this question

List problems

  • AndyWillig

    Thanks I was being stupid. I just created a copy inside of the constructer and it works just fine now. Thanks
  • IamManick

    I assume that you are having problems with FList inside of DE. You are attempting to share the same list across multiple instances therefore each DE will point to the same FList (unless you internally copy the values, you didn't post the code for that). Additionally you clear FList through each iteration. So as a result you'll have DList with however many copies of DEs. Each DE's FList member will be empty. Assigning an element to any of the DE's FList member will also assign the same value to all the other DEs. You should probably create a new instance of FList for each iteration or, better yet, create it inside the DE class directly.

    Michael Taylor - 12/8/06


  • Siva116

    Ok I understand the problem now I wasn't paying attention to when the FList was going to null. I have this

    DriverEntry DE = new DriverEntry(type, hashD, description, versionD, provider, DateTime.Now, FList); //DE Has the contents of the list just fine. When I add it to DList it dissapears WHY

    FList.Clear(); //I am loosing it here.

    DList.Add(DE);

    even if I change the order I loose it

    DriverEntry DE = new DriverEntry(type, hashD, description, versionD, provider, DateTime.Now, FList); //DE Has the contents of the list just fine. When I add it to DList it dissapears WHY

    DList.Add(DE);

    FList.Clear(); //I am still loosing it here.

    in c++ I think I would need to impliment a copy constructer to deep copy the list do I have to do the same thing in c# and if so is it the same as in c++, or at least the same bassic idea. I guess I think I know what is going on here but am not 100% sure.


  • List problems