C
C#ā€¢2w ago
Citrus limon

āœ… How do I get rid of 'System.ArgumentOutOfRangeException'

here the 'l'astname' substring doesnt come as output because of this exception can someone help me fix it?
No description
28 Replies
canton7
canton7ā€¢2w ago
Let's look at the docs (https://learn.microsoft.com/en-us/dotnet/api/system.string.substring?view=net-8.0#system-string-substring(system-int32-system-int32)):
public string Substring (int startIndex, int length); length Int32 The number of characters in the substring.
It's a length, not an end index
Citrus limon
Citrus limonā€¢2w ago
that means if i write 7,12 it would work?
qqdev
qqdevā€¢2w ago
The lastname has only 5 characters
Citrus limon
Citrus limonā€¢2w ago
yeah The counting feels confusing like
qqdev
qqdevā€¢2w ago
Is this an assignment?
Citrus limon
Citrus limonā€¢2w ago
no just me watching tutorials and doing random stuff
qqdev
qqdevā€¢2w ago
Because you could also just split at the white space (" ") Which would work with every name Assuming there is no middle name
Citrus limon
Citrus limonā€¢2w ago
i dont get it.....
qqdev
qqdevā€¢2w ago
var fullname = "jasmini salty";
var split = fullname.Split(" ");
var firstname = split[0];
var lastname = split[1];
var fullname = "jasmini salty";
var split = fullname.Split(" ");
var firstname = split[0];
var lastname = split[1];
Kinda like that
Citrus limon
Citrus limonā€¢2w ago
i dont know the split yet so i was doing it with substring
qqdev
qqdevā€¢2w ago
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
Citrus limon
Citrus limonā€¢2w ago
but i think i didnt get the full concept of substring right
qqdev
qqdevā€¢2w ago
Yeah, the 2nd parameter of Substring(...) is the length Not an index As canton7 pointed out
Citrus limon
Citrus limonā€¢2w ago
oh i get it so basically the first nukbe ris starting point
qqdev
qqdevā€¢2w ago
Exactly
Citrus limon
Citrus limonā€¢2w ago
and second number is the lenght
qqdev
qqdevā€¢2w ago
I strongly recommend to add parameter hints here Like the following
var firstname = fullname.Substring(startIndex: 0, length: 7);
var firstname = fullname.Substring(startIndex: 0, length: 7);
Makes it more readable
Citrus limon
Citrus limonā€¢2w ago
where do i find it?
Citrus limon
Citrus limonā€¢2w ago
this pops out for me
Citrus limon
Citrus limonā€¢2w ago
No description
qqdev
qqdevā€¢2w ago
You tried to create a substring which exceeds the boundaries of the string
Citrus limon
Citrus limonā€¢2w ago
yeah ahh thx man you helped me out i appreciate it
qqdev
qqdevā€¢2w ago
You are welcome!
Citrus limon
Citrus limonā€¢2w ago
found the coding dictionary :D
qqdev
qqdevā€¢2w ago
Haha, exactly Visual Studio should also be able to tell you the parameter names
Citrus limon
Citrus limonā€¢2w ago
C# holybook
qqdev
qqdevā€¢2w ago
Our bible šŸ™