C
C#12mo ago
!

C# array problem // Error loading configuration : Index was outside the bounds of the array

Hii! I'm new to this group i tried to fix it yesterday all day and decided to ask for help. I moved some settings files to a webserver and after updating code, i trigger the error in title. The problem is that it works perfect with index==0, but if i use any other index, it will give the error in title. As an exemple, if i select the second element in list(index==1) it will trigger that error, even if Server1.txt and Server2.txt have the same content. All the txt files have elements separated by space I'm open to any advice, thanks❤️
27 Replies
Angius
Angius12mo ago
Jesus Well, uh, In any case, if you get an index out of bounds when trying to access array[1] that means... that index does not exist Why— why are you using this.server2 kinds of fields just to move values from an array to a list...? Why webclient? Why casting to object...?
!
!OP12mo ago
i want to read all elements inside the txt files from a webserver, so i can change anything names without recompiling source..
Angius
Angius12mo ago
Sure, okay, you can do that You're just doing some really weird things along the way lol
!
!OP12mo ago
i'm aware that the code looks really messy 😄 but i can't understand where i check a wrong index, i only use index+1 once when reading server txt file, cuz they start from server1, and index is 0 based..
Angius
Angius12mo ago
Well, run the debugger
!
!OP12mo ago
if i select index0, it will load server1 perfectly, but if i select index 1, it fails to read server2.txt wich has exactly the same content i tried it, it reads all the values fine, but still triggers that index outside array
Angius
Angius12mo ago
Which line exactly? Post your code on $paste or somewhere and tell me the line number
MODiX
MODiX12mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
!
!OP12mo ago
https://paste.mod.gg/oyofkxceyggu/0 on line 94 & 103, 94 says there is not enough configuration, but i debugged the arrays & data received and worked even on index 1(server2.txt)
BlazeBin - oyofkxceyggu
A tool for sharing your source code with the world!
Angius
Angius12mo ago
ex.Message says index is out of range, yeah? But if so, then that means your check in line 101 ain't checking
!
!OP12mo ago
yep, i had to remove that to get the index outside array
Angius
Angius12mo ago
How does your config file look? Because .Split() with no delimiters splits by whitespace
MODiX
MODiX12mo ago
Angius
REPL Result: Success
"""
abc def ghi
jkl mno pqr
stu vwx yz
""".Split()
"""
abc def ghi
jkl mno pqr
stu vwx yz
""".Split()
Result: string[]
[
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqr",
"stu",
"vwx",
"yz"
]
[
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqr",
"stu",
"vwx",
"yz"
]
Compile: 389.884ms | Execution: 30.872ms | React with ❌ to remove this embed.
Angius
Angius12mo ago
So if you're storing each config in its own line, and it's all delimited by spaces... it's not doing what you think it's doing Why not use JSON in the first place?
!
!OP12mo ago
i'll try that, i started by reading the files from a txt file in main directory, so my next step was to move that code to webserver, and continue improving it
Angius
Angius12mo ago
JSON really would be so much more sane
// people.json
[
{ "name": "Bob", "age": 42, "likes": [ "cars", "cooking" ] },
{ "name": "Agnes", "age": 14, "likes": [ "fitness", "rugby" ] },
]
// people.json
[
{ "name": "Bob", "age": 42, "likes": [ "cars", "cooking" ] },
{ "name": "Agnes", "age": 14, "likes": [ "fitness", "rugby" ] },
]
record Person(string Name, int Age, string[] Likes);
record Person(string Name, int Age, string[] Likes);
using var client = new HttpClient();
var people = await client.GetFromJsonAsync<Person[]>("people.json");
using var client = new HttpClient();
var people = await client.GetFromJsonAsync<Person[]>("people.json");
Or, if you want to keep your current code, pay attention to what you're splitting your strings on Instead of yoloing it with .Split() that will split on every whitespace
!
!OP12mo ago
i'll try it, thanks❤️ yep, all files have the elements separated by space.... this is an exemple of server[x].txt : "0x01CC7A6C 0x01BDB658 0xC 0x18 0x200 0x204 0x208 0x0 0x4B0 0x4B4 0x4B8 0x640 0x180 0x20 0x50 0x019EA0C4 0x0 0x0 0x0 0x0 0x0 0x7A0 0x5C 0x01CC79EC 0x60 0x7E4 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0"
Angius
Angius12mo ago
So a
Bob42carscooking Agnes14fitnessrugby
Bob42carscooking Agnes14fitnessrugby
or
Bob 42 cars cooking Agnes 14 fitness rugby
Bob 42 cars cooking Agnes 14 fitness rugby
?
!
!OP12mo ago
it may look like a mess, but i'm better at php than c#, and i did a panel to manage all that mess. like this Bob 42 cars cooking Agnes 14 fitness rugby in all 3 files, the elements are separated by space, and all of them are on a single line
Angius
Angius12mo ago
OOF yeah, no wonder your code doesn't work
!
!OP12mo ago
:d it was working when i did the first version that was reading values from exe directory, but i wanted to move them to a webserver, and it become a big mess
Angius
Angius12mo ago
The format of your config would lend itself to being a big mess, yes
!
!OP12mo ago
i also use json for some parts of my code, but i would really need to make it as txt, at least this part, so i will save half the size of the file, there will be many post requests involving those files....
Angius
Angius12mo ago
At least use CSV then
Bob,42,cars,cooking
Agnes,14,fitness,rugby
Bob,42,cars,cooking
Agnes,14,fitness,rugby
That way you can split the individual configs on \n and individual items in each config on ,
!
!OP12mo ago
but it won't be the same result? atm i use \n for endline and " " for elements
Angius
Angius12mo ago
So you are not storing them all in a single line after all?
!
!OP12mo ago
so, to clarify it, there are 3 files, each with only one big line with elements separated by " " a file for binaries, one for names, and one for memory adresses
Want results from more Discord servers?
Add your server