c# - How to pass an integer array into a Razor function? -
i writing function select 3 articles json feed @ random. have created function generates random number between 2 given end points , have loop iterates 3 times output article content on page.
the random number function working successfully, , loop output article info page. randomnumber function needs run 3 times 3 random numbers , need ensure once randomnumber1 has been picked, can't picked again. have created array (featuredstories) store picked numbers having trouble passing getrandomnumber function.
@{ random rnd = new random(); var featuredstories = new list<int>(); } @functions { public int getrandomnumber(int min, int max, random rnd, int[] featuredstories) { int randomnumber = rnd.next(min, max); if (featuredstories.contains(randomnumber)){ randomnumber = getrandomnumber(min, max, rnd); }else{ featuredstories.add(randomnumber); } return randomnumber; } @for(var = 1; < 4; i++) { int randomno = getrandomnumber(1, items.count(), rnd, featuredstories); }
i getting error:
razor syntax error. no overload method 'getrandomnumber' takes 3 arguments
your code has 2 errors in fact:
@functions { public int getrandomnumber(int min, int max, random rnd, (2) int[] featuredstories) { int randomnumber = rnd.next(min, max); if (featuredstories.contains(randomnumber)){ randomnumber = getrandomnumber(min, max, rnd); (1) }else{ featuredstories.add(randomnumber); } return randomnumber; }
- the first error not passing
featuredstories
in recursive call. - the second error
int[]
(which shortcutarray<int>
) not haveadd
method, , definedfeaturedstories
list<int>
@ top of page.
so, fix these, change method this:
@functions { public int getrandomnumber(int min, int max, random rnd, list<int> featuredstories) { int randomnumber = rnd.next(min, max); if (featuredstories.contains(randomnumber)) { randomnumber = getrandomnumber(min, max, rnd, featuredstories); } else { featuredstories.add(randomnumber); } return randomnumber; }
Comments
Post a Comment