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; } 
  1. the first error not passing featuredstories in recursive call.
  2. the second error int[] (which shortcut array<int>) not have add method, , defined featuredstories 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

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -