Link to home
Start Free TrialLog in
Avatar of ianinspain
ianinspain

asked on

500pts: Creating an array of my class and assigning value gives error

Hi there,

Can anybody tell me why assigning a value to an array of clases i have created would give the following error?

Unhandled exception: Object reference not set to an instance of an object.

Here is my line to initialise the array with 300

Perfil.Preference[] localPreferences = new Perfil.Preference[300];

and then assignin this gives the error, the totalFiles at this point is 0 and the id = 11

localPreferences[totalFiles].ID = id;

Any ideas?

must be doing something wrong,

Thanks in advance for any info or help

Regards

Ian
ASKER CERTIFIED SOLUTION
Avatar of AGBrown
AGBrown
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ianinspain
ianinspain

ASKER

wow thanks andy... can't believe i missed that... is there anyway to do this all together ... or do i have to do each one separately?

Thanks

Ian
Ian,

No problem. The only way is to iterate over the array once you created the array:
for (int i = 0; i < localPreferences.Length; i++)
{
    localPreferences[i] = new Preference();
}

However, if you aren't sure that you are going to use every element, then I would just create the Preferences class as you need it.

I might be telling you things you already know here, but if not then they're worth knowing. If your array is going to change length a lot and you aren't trying to squeeze every last nanosecond of performance out of the application, then I would move the array to something like a List<Preference> (.NET 2.0), or an ArrayList (.NET 1.1). Similarly, if you are going to be doing other things to the array, like referring to each Preference through it's ID, and therefore searching the array a lot, I would ensure that the array is sorted by ID so that you can take advantage of BinarySearch to find each item (its a lot quicker). .NET 2.0 allows this through use of SortedList<Preference>. You can write a custom comparer for the Preference class to do the searches and sort comparisons on ID, or any other field you like.

Andy
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Arthur,

I'm interested in what you've said, but I'm not sure I understand. Surely if you create an array (localPreferences) of classes (Preference), you still need to put an instance of that class (Preference) into each element of the array (localPreferences) before you can use it, regardless of whether its the array is a static field or an instance field.

Or are you speaking with respect to the Perfil class, in which case although you don't need to create an instance of Perfil if localPreferences is a static field, you still need to assign the array to localPreferences field and an instance of the Preferences class into each element of the array.

Then as a seperate issue, if you only need a single instance of that localPreferences array for all threads and objects, it might be a case of deciding between a static class, or a singleton instance of a class. Arguably if Perfil is necessarily an instance class, not static, then maybe the static field for the array should be in a seperate class which is static/singleton to help keep coherence in the design? That would also help ensure that if you needed to put synchronisation etc. in place you would be able to segregate locking mechanisms or access methods to the central array resource away from the purpose of the Perfil class?

Andy
AGBrown,

thank you... i am ready to assign points here or shall i wait until Arthur replies?

Ian
Up to you, I don't mind. I think Arthur probably had more than a valid point - he is pretty good at this stuff! I just wanted to understand it.

Andy
that was my point about how the array is actually used.  If the array serves a One copy for all role, then make it a Static member, otherwise it would be ab instance member.

AW