C
C#•10mo ago
The Dead Hand

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

Did you find this page helpful?