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.
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.
Thanks,
Ossie33 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
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 0This 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? 🙂
Angius#1586
REPL Result: Success
Console Output
Compile: 707.534ms | Execution: 78.342ms | React with ❌ to remove this embed.
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
list is {Count: 3}
¯\_(ツ)_/¯This is why I get the error in the first place, I think. I’m really bad at explaining and I’m on phone
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 worstIts 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
Uh
Use a class?
I can’t in this example
Why?
Do you know of FiveM?
First I hear of it
Ah, wait, is that like some custom GTA V server or whatever?
Yea kinda
Let me explain :
We get people who want help with hacking it every now and then lmao
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
So?
You can fetch it into a class
And you can serialize that class to whatever format and send it wherever needed
fetch it into a class@¿
Yeah
How are you connecting to the database?
ADO?
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
I'd recommend Dapper, then
Idk what Dapper is
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
But is a class neccesary for just very temperary data?
Necessary? No
Lol probably lua kicking in, I’m trying to build it like that i think lol
Makes life a whole lot easier tho
I don’t understand why it is easier tho, for me it seems even more complicated
You seem to be having issues with your hacky workaround code, though
Doesn't seem easy to me
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 🙂
Because Lua has weak and dynamic type system
C# has strong and static type system
Yea, I just gotta get used to it i guess