Jonathan Lind
Jonathan Lind

Reputation: 215

can't copy objects into new array

I have an array filled with objects of a type I have declared myself (called ServiceUnit). I now want to filter out only some of the objects from this array and what I'm doing to acheive this is that I have a new array with the correct size and just try to copy the objects in to the new one.

ServiceUnit[] serviceUnits = ProductConfiguration.ServiceGuide.GetServiceUnits();

ServiceUnitType serviceUnitType = ProductConfiguration.ServiceGuide.GetServiceUnitType(guid);
int size = 0;

/* COUNT THE NUMBER OF OBJECTS ADHERING TO MY CRITERIA */
foreach (ServiceUnit unit in serviceUnits)
{
    if (unit.ServiceUnitTypeAttributes[0].ServiceUnitType.Id == serviceUnitType.Id)
    {
        size++;
    }
}

/* ARRAY TO STORE FILTERED OBJECTS BASED ON A CRITERIA */
ServiceUnit[] serviceUnitsFiltered = new ServiceUnit[size];

/* LOOP AND CHECK IF ADHERING TO CRITERIA, THEN COPY TO NEW ARRAY */
for(int i=0; i<serviceUnits.Length; i++){
    if(serviceUnits[i].ServiceUnitTypeAttributes[0].ServiceUnitType == serviceUnitType){
        serviceUnitsFiltered[i] = serviceUnits[i];
    }
}

The problem I have is that the new array does have the right amount of objects in it, but they are not populated with the data from serviceUnits[i]. Instead they are just "nulled".

Any ideas on what might be wrong or how to solve it?

Upvotes: 1

Views: 76

Answers (4)

Fabjan
Fabjan

Reputation: 13676

You can use List<T> instead of using array :

/* LOOP AND CHECK IF ADHERING TO CRITERIA, THEN COPY TO NEW ARRAY */
List<ServiceUnit> serviceUnitsFiltered= new List<ServiceUnit>();

for(int i = 0; i < serviceUnits.Length; i++) 
    if(serviceUnits[i].ServiceUnitTypeAttributes[0].ServiceUnitType == serviceUnitType) 
       serviceUnitsFiltered.Add(serviceUnits[i]);

Or if you strictly need an array of ServiceUnit elements you can convert it using List<T>.ToArray() method

P.S. LINQ is one line solution but it is slower than simple for loop with just one if(condition) expression...

Upvotes: 0

Mark
Mark

Reputation: 1554

Why not just do...

ServiceUnit[] serviceUnits = ProductConfiguration.ServiceGuide.GetServiceUnits();

ServiceUnitType serviceUnitType = ProductConfiguration.ServiceGuide.GetServiceUnitType(guid);

serviceUnitsFiltered = serviceUnits.Where(unit=>unit.ServiceUnitTypeAttributes[0].ServiceUnitType.Id == serviceUnitType.Id).ToList();

Upvotes: 1

Vilx-
Vilx-

Reputation: 106912

The bug is in this line:

serviceUnitsFiltered[i] = serviceUnits[i];

The index to serviceUnitsFiltered shouldn't be i but something else. Probably a new variable that is incremented every time you add something to serviceUnitsFiltered, like:

serviceUnitsFiltered[j++] = serviceUnits[i];

Also consider using LINQ, it's shorter (albeit a tiny bit slower):

serviceUnitsFiltered = serviceUnits.Where(x=>x.ServiceUnitTypeAttributes[0].ServiceUnitType == serviceUnitType).ToArray();

Upvotes: 2

w.b
w.b

Reputation: 11228

You can do it much easier with LINQ:

serviceUnitsFiltered = serviceUnits.Where(su => su.ServiceUnitTypeAttributes[0].ServiceUnitType == serviceUnitType)
                                   .ToArray();

Upvotes: 1

Related Questions