I have a chance list and a name list, and I want to check the nth element of the chance list to see if the nth element of the name list should be approved or not. Here is a simplified version of the code:
I must keep these as lists and cannot change them to arrays. What can I do? Thanks in advance.
If I understand your questionâŚ
// make VERY sure that nameList is NOT longer than chanceList!!!
for (int i = 0; i < nameList.Count; i++)
{
if (chanceList[i] > randomNumber)
{
acceptedList.Add( nameList[i]);
}
}
Thatâs the only thing I can think of that makes sense.
You might also want to rechoose a new random number inside the loop? Not sure what you have in mind.
1 Like
Thatâs why such data should be stored in a single list that contains a class which groups the string and the chance value into one âthingâ. Not only does it avoid those alignment issues between the two lists, it also groups data that belongs together.
1 Like
to add to what kurt and bunny said, you can can declare a class within the scope of a class. it helps with organization when you feel like the new class doesnât deserve to really be a full fledged class like everyone else inside your system, but its more of a helper class for some other class.
public class MyClass{
List<HelperClass> acceptableNames;
public class HelperClass{
string name;
int acceptanceProbability;
}
}
if you really wanted to reference that class from somewhere else:
MyClass.HelperClass obj = ...;
There are times where the benefits of having seperate lists will justify the âriskâ. just try to encapsulate as much as possible (look into the programming concept of âencapsulationâ if unfamiliar and feeling curious)
1 Like