Actually, I have this :
public ArrayList _albumId;
public ArrayList _albumName;
public ArrayList _nbPhotos;
And when I have a new album, I add a line on each ArrayList. Do you know something to do it with only one list or array ? Thanks
First of all, don't use ArrayList
- use generic List<T>
.
Second - instead of having separate list for each attribute of album, create Album
class with all required attributes, and keep albums in List<Album>
:
public class Album
{
public int Id { get; set; }
public string Name {get; set; }
public int CountOfPhotos {get; set; }
}
Adding new album:
_albums.Add(new Album { Id = 2, Name = "Foo", CountOfPhotos = 42 });
Where albums list declared as
List<Album> _albums = new List<Album>();
Why keep information about the same thing broken apart into multiple arrays?
Create an Album
object. Something like:
public class Album
{
public int ID { get; set; }
public string Name { get; set; }
// other properties, etc.
}
And have a list of albums:
var albums = new List<Album>();
When adding a new album, add a new album:
var newAlbum = new Album
{
ID = someValue,
Name = anotherValue
}
albums.Add(newAlbum);
Keep information about an object encapsulated within that object, not strewn about in other disconnected variables.
As an analogy, consider the act of parking a car in a parking lot. With a collection of objects, the parking lot has a collection of spaces and each car goes into a space. Conversely, with separate arrays of values, the process for parking a car is:
Modeling encapsulated objects just makes more sense.
Use a generic list.
Define a class, for smaple:
public class Album
{
public int Id { get; set; }
public string Name { get; set; }
public string[] AlbumPhotos { get; set; }
}
And use it in a generic list:
var albums = new List<Album>();
albums.Add(new Album() { Id = 1, Name = "Ramones" };
albums.Add(new Album() { Id = 2, Name = "Leave Home" };
albums.Add(new Album() { Id = 3, Name = "Rocket To Russia" };
List
rather thanArrayList
.List
has type safety.ArrayList
, somewhere a puppy dies.