C
C#β€’3y ago
hercules_x_x

any thoughts why this error ?

Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array. at inclass7.exercise7_1.Main(String[] args) in /Users/user/EXSAM/Program.cs:line 17
31 Replies
Kouhai
Kouhaiβ€’3y ago
i <= days.Length Means run till i equals the length of days array arrays in c# start at 0
hanu
hanuβ€’3y ago
if you count from 0 to n, that's total (n+1) numbers
hercules_x_x
hercules_x_xOPβ€’3y ago
so how to remove the error
Kouhai
Kouhaiβ€’3y ago
Do you know how for loops work?
Hulkstance
Hulkstanceβ€’3y ago
foreach (var i in Enumerable.Range(0, days.Length) or for (var i = 0; i < days.Length; i++) hf
Kouhai
Kouhaiβ€’3y ago
πŸ˜… They'll just copy paste it and won't understand why that's the solution
Hulkstance
Hulkstanceβ€’3y ago
true πŸ˜„
hercules_x_x
hercules_x_xOPβ€’3y ago
i swear i found why before i read lol just now after i keep thinking of this i did this yes u ever explain to me there is another thing actually i wana ask if that is ok
Kouhai
Kouhaiβ€’3y ago
Yeah sure go ahread
hercules_x_x
hercules_x_xOPβ€’3y ago
there is weird yellow warning teacher she keep saying ignore it It’s appear in readline
Kouhai
Kouhaiβ€’3y ago
What's the warning?
Hulkstance
Hulkstanceβ€’3y ago
you do double.Parse(Console.ReadLine())?
hercules_x_x
hercules_x_xOPβ€’3y ago
yessssssss
Hulkstance
Hulkstanceβ€’3y ago
use Convert.ToDouble(Console.ReadLine()) there is a difference between them if you hover it
hercules_x_x
hercules_x_xOPβ€’3y ago
can i understand actually why
Hulkstance
Hulkstanceβ€’3y ago
double.Parse has an additional ArgumentNullException – s is null. that's why
Kouhai
Kouhaiβ€’3y ago
Well, the reason you get this warning is because Console.ReadLine can return null double.Parse doesn't work with null
hercules_x_x
hercules_x_xOPβ€’3y ago
i don’t really understand null
Hulkstance
Hulkstanceβ€’3y ago
Hulkstance
Hulkstanceβ€’3y ago
hercules_x_x
hercules_x_xOPβ€’3y ago
u mean like return value?
Hulkstance
Hulkstanceβ€’3y ago
Console.ReadLine may be null
Kouhai
Kouhaiβ€’3y ago
null means no reference
hercules_x_x
hercules_x_xOPβ€’3y ago
ohhhh then what is the real use of parse
Kouhai
Kouhaiβ€’3y ago
To parse a string
Hulkstance
Hulkstanceβ€’3y ago
to try parse to int or something there are two patterns they are referred as Tester-Doer void Parse(...) and bool TryParse(..., out ...)
Kouhai
Kouhaiβ€’3y ago
I don't think they were taught the out key word πŸ˜…
Hulkstance
Hulkstanceβ€’3y ago
public struct DateTime
{
public static DateTime Parse(string dateTime)
{
...
}
public static bool TryParse(string dateTime, out DateTime result)
{
...
}
}
public struct DateTime
{
public static DateTime Parse(string dateTime)
{
...
}
public static bool TryParse(string dateTime, out DateTime result)
{
...
}
}
me neither but good to know
hercules_x_x
hercules_x_xOPβ€’3y ago
i am a bit lost to be honest
Hulkstance
Hulkstanceβ€’3y ago
if something goes sideways in Parse, it's supposed to throw an exception such as InvalidOperationException. TryParse however try/catch'es it, so it never throws and instead it returns a boolean let's say you're parsing an .xls document or something and u need to know if a cell is an integer you would do bool isInteger = int.TryParse(cell, out _) did you understand it that way? in your case it's trying to parse the string you write in the console to double
Want results from more Discord servers?
Add your server