C
C#ā€¢5mo ago
Kit N Kat

āœ… 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ā€¢5mo 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
Kit N Kat
Kit N KatOPā€¢5mo ago
that means if i write 7,12 it would work?
qqdev
qqdevā€¢5mo ago
The lastname has only 5 characters
Kit N Kat
Kit N KatOPā€¢5mo ago
yeah The counting feels confusing like
qqdev
qqdevā€¢5mo ago
Is this an assignment?
Kit N Kat
Kit N KatOPā€¢5mo ago
no just me watching tutorials and doing random stuff
qqdev
qqdevā€¢5mo ago
Because you could also just split at the white space (" ") Which would work with every name Assuming there is no middle name
Kit N Kat
Kit N KatOPā€¢5mo ago
i dont get it.....
qqdev
qqdevā€¢5mo 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
Kit N Kat
Kit N KatOPā€¢5mo ago
i dont know the split yet so i was doing it with substring
qqdev
qqdevā€¢5mo ago
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
Kit N Kat
Kit N KatOPā€¢5mo ago
but i think i didnt get the full concept of substring right
qqdev
qqdevā€¢5mo ago
Yeah, the 2nd parameter of Substring(...) is the length Not an index As canton7 pointed out
Kit N Kat
Kit N KatOPā€¢5mo ago
oh i get it so basically the first nukbe ris starting point
qqdev
qqdevā€¢5mo ago
Exactly
Kit N Kat
Kit N KatOPā€¢5mo ago
and second number is the lenght
qqdev
qqdevā€¢5mo 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
Kit N Kat
Kit N KatOPā€¢5mo ago
where do i find it?
Kit N Kat
Kit N KatOPā€¢5mo ago
this pops out for me
Kit N Kat
Kit N KatOPā€¢5mo ago
No description
qqdev
qqdevā€¢5mo ago
You tried to create a substring which exceeds the boundaries of the string
Kit N Kat
Kit N KatOPā€¢5mo ago
yeah ahh thx man you helped me out i appreciate it
qqdev
qqdevā€¢5mo ago
You are welcome!
Kit N Kat
Kit N KatOPā€¢5mo ago
found the coding dictionary :D
qqdev
qqdevā€¢5mo ago
Haha, exactly Visual Studio should also be able to tell you the parameter names
Kit N Kat
Kit N KatOPā€¢5mo ago
C# holybook
qqdev
qqdevā€¢5mo ago
Our bible šŸ™
Want results from more Discord servers?
Add your server