C
C#5d ago
Kushiwushi

Cannot apply indexing with [] to an expression of type 'int'

I am trying to convert an array string to an int array and try to access certain indexes
c#

string words = kv.words;
string timeList = kv.startTimeMs;
string[] timeMs = timeList.Split(", ");
int[] time = Array.ConvertAll(timeMs, int.Parse);
foreach (int yes in time)
{
int x = 0;
Console.WriteLine(yes[0]);
x++;
}


c#

string words = kv.words;
string timeList = kv.startTimeMs;
string[] timeMs = timeList.Split(", ");
int[] time = Array.ConvertAll(timeMs, int.Parse);
foreach (int yes in time)
{
int x = 0;
Console.WriteLine(yes[0]);
x++;
}


I can't access the index as I have tried on line 9 ty ^^
7 Replies
TheRanger
TheRanger5d ago
u mean line 8? yes is an integer, u cant access it like its an array
Kushiwushi
KushiwushiOP5d ago
yea do i do int yes{} in time ?
TheRanger
TheRanger5d ago
what does {} represent in that context?
Kushiwushi
KushiwushiOP5d ago
i mean [] but that doesn't seem to work how do i access it like an array then actually am i even doing it correctly xd
TheRanger
TheRanger5d ago
time is already an array tho what ur doing is iterating through each number of the array
Kushiwushi
KushiwushiOP5d ago
hmm okay i'll keep that in mind ig i fixed it by replacing yes[0] to time[0] instead ty for the insight ^^ though i can't seem to go higher than 0 for some reason? i think it's because all the "time" numbers are on index 0 instead of being distributed evenly throughout the array but that's another issue ig
Thinker
Thinker5d ago
When you're using a foreach loop, the loop will run for each element in the array, time in this case You don't need to index time when using a foreach loop, the loop itself will essentially do the indexing for you

Did you find this page helpful?