- Home /
Custom Class List - Passing the Class Vs Passing an Index
Hi,
I'm currently working on something using Lists and custom classes. I was just curious if it is better to pass the class itself over to the various functions, or pass the index in the list. Or is there another better way I haven't seen yet?
Here's an example of what I mean.
public List<CustomClass> customClass;
void Start() {
editListItem (customClass[3]);
}
void editListItem (CustomClass cc) {
// do stuff with cc.
}
Vs. doing it like this
public List<CustomClass> customClass;
void Start() {
editListItem (3);
}
void editListItem (int ccIndex) {
// do stuff with customClass[ccIndex].
}
I've used both and both work, but is one better than the other and why? The only obvious pro/con I can see is that passing an index can start to get a bit much when you're dealing with nested lists and the call to your list item can be 3 pages wide!
I have no knowledge of memory handling in Unity beyond beginner level, but I always intuitively felt that passing a whole class might be bad from an efficiency standpoint. Am I way off?
I would personally do the latter like so
public List<CustomClass> customClass;
void Start() {
editListItem (3);
}
void editListItem (int ccIndex) {
if ((ccIndex < customClass.Count) && (customClass[ccIndex] != null))
{
// do stuff with customClass[ccIndex]
}
}
But like you said either way works.
Your answer
Follow this Question
Related Questions
Getting information of which Dropdown menu has been changed 0 Answers
Comparing each random element in an array with each other element 3 Answers
Select other index in list 0 Answers
check if something is on a list by one argument 1 Answer
Does a new instance of a custom class return null? 2 Answers