C
C#2y ago
Ossie

Index was out of range. How to check if list has items without triggering this error?

Hello, I have a list, with a list inside.
List<List<dynamic>> characterData
List<List<dynamic>> characterData
This is because I wan't to have 3 possible lists, with a list inside. However there isn't always 3 lists present. Sometimes it might only be 1 or 2 lists that are filled, and the last one is empty. My problem occurs when trying to check if the List has any value. This works fine if none of the lists have any value, by checking if characterData.Count > 0. However when this is not the case, I try to check if a value inside the list is NullOrEmpty, however this is causin the error Index was out of range. Which makes sense because the list doesn't exist, but how would I then check if the list itself exists inside the the other list? I hope this makes any sense, I will post a snippet. I would wanna check if characterData[i] exists, without getting an index error.
List<List<dynamic>> characterData // This get its value from another place, not important

for (int i = 0; i <= 2; i++)
{
if (characterData.Count > 0)
{
if (!string.IsNullOrEmpty(characterData[i][0].ToString())) // This give me a error, so how do I check if the list has any values, I'm just checking the first value as it seemed logically to me
{
// Rest of my code
}
}
}
List<List<dynamic>> characterData // This get its value from another place, not important

for (int i = 0; i <= 2; i++)
{
if (characterData.Count > 0)
{
if (!string.IsNullOrEmpty(characterData[i][0].ToString())) // This give me a error, so how do I check if the list has any values, I'm just checking the first value as it seemed logically to me
{
// Rest of my code
}
}
}
Thanks, Ossie
33 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Angius
Angius2y ago
Pattern matching? if (list is { Length: > 0 }) will check if a given list is not-null and if so, if it has a length of more than 0
Ossie
Ossie2y ago
This wouldn’t work for me, because it needs to loop 3 times no matter what, it does a else statements if the characterData[i] doesn’t exist. Is should have added that to the snippet, sorry characterData[i][0] always exists if characterData[i] exists. The problem id characterData[i] not existing evey time It could be like characterData[0] exists, but characterData[1] doesn’t But if characterData[i] exists, it will always have a [0] value inside, so don’t worry about that part Not sure how to implement this, can you give me a example? 🙂
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
var lists = new List<List<int>?>(){
new List<int>(),
new List<int>(){1, 2, 3},
null,
};

foreach (var list in lists)
{
if (list is {Count: > 0})
{
Console.WriteLine("Not null and has elements");
}
else
{
Console.WriteLine("Null or empty");
}
}
var lists = new List<List<int>?>(){
new List<int>(),
new List<int>(){1, 2, 3},
null,
};

foreach (var list in lists)
{
if (list is {Count: > 0})
{
Console.WriteLine("Not null and has elements");
}
else
{
Console.WriteLine("Null or empty");
}
}
Console Output
Null or empty
Not null and has elements
Null or empty
Null or empty
Not null and has elements
Null or empty
Compile: 707.534ms | Execution: 78.342ms | React with ❌ to remove this embed.
Ossie
Ossie2y ago
I don’t think this would work for me, as the size of characterData is dynamic, it can be either 1,2 or 3. It doesn’t have any standard values
Angius
Angius2y ago
list is {Count: 3} ¯\_(ツ)_/¯
Ossie
Ossie2y ago
This is why I get the error in the first place, I think. I’m really bad at explaining and I’m on phone
Angius
Angius2y ago
Also, as a side note, the very List<List<dynamic>> characterData seems iffy to me dynamic is code smell at best A noob trap that should be hidden behind a compiler flag at worst
Ossie
Ossie2y ago
Its cause it holds multiple types, both int and strings. Strings for like characterName and int for age etc. idk if it is the right way to go about it
Angius
Angius2y ago
Uh Use a class?
Ossie
Ossie2y ago
I can’t in this example
Angius
Angius2y ago
Why?
Ossie
Ossie2y ago
Do you know of FiveM?
Angius
Angius2y ago
First I hear of it Ah, wait, is that like some custom GTA V server or whatever?
Ossie
Ossie2y ago
Yea kinda Let me explain :
Angius
Angius2y ago
We get people who want help with hacking it every now and then lmao
Ossie
Ossie2y ago
Lol I’m just making things for it for fun 🙂 Anyways let me explain why no class 2 sec FiveM has serverside and client side. I have server side a class that has each players data etc. when they have chose their player characterBut this im creating is for setting up that player class, so this is before initializing the user class server side. The data I just here (characterData) is fetched from a database and sent to the client to display their characters
Angius
Angius2y ago
So? You can fetch it into a class And you can serialize that class to whatever format and send it wherever needed
Ossie
Ossie2y ago
fetch it into a class@¿
Angius
Angius2y ago
Yeah How are you connecting to the database? ADO?
Ossie
Ossie2y ago
im new with c#, I come from Lua, I don’t really get the concept fully of classes yet MysqlConnector I then store all the data in a list > serialize it with json > send it to the client > deserialize it and have characterData
Angius
Angius2y ago
I'd recommend Dapper, then
Ossie
Ossie2y ago
Idk what Dapper is
Angius
Angius2y ago
Dapper will let you fetch data directly into a class Or a list of classes Or whatever else you need Then you can work with C# like a normal human being instead of trying to hack things together with lists of lists of dynamics lol
Ossie
Ossie2y ago
But is a class neccesary for just very temperary data?
Angius
Angius2y ago
Necessary? No
Ossie
Ossie2y ago
Lol probably lua kicking in, I’m trying to build it like that i think lol
Angius
Angius2y ago
Makes life a whole lot easier tho
Ossie
Ossie2y ago
I don’t understand why it is easier tho, for me it seems even more complicated
Angius
Angius2y ago
You seem to be having issues with your hacky workaround code, though Doesn't seem easy to me
Ossie
Ossie2y ago
Yea lol Its cause the syntax is different, In C# you get an error for trying to access a list out of its index, in Lua you just get a null value, but no error, that is why I thought it might work Idk, I’m too tired to think, i will come back to this tmrw, thanks a lot for the tips 🙂
Angius
Angius2y ago
Because Lua has weak and dynamic type system C# has strong and static type system
Ossie
Ossie2y ago
Yea, I just gotta get used to it i guess