0

I am creating a website and wish to make a 'flashcard' page which shows interesting facts about the topic the site is based on (music).

I have created an arraylist and added some 'facts' to it as a string value.

I have a textbox on my page and I have a button and I want to show a different fact each time the button is clicked to the textbox.

What would be the best way to go about it?

Sorry I am a newbie here and am just getting to grips with ASP.NET and VS.

EDIT

Thanks, I have now changed it to list. Now I have stored multiple string values to that list and have set a string field called 'abc' (like so);

public partial class _Default : System.Web.UI.Page
{

    private String abc;

    public void do9()
    {
        List<String> list = new List<String>();
        list.Add("aaa");
        list.Add("bbb");
        list.Add("ccc");
        list.Add("ddd");
        list.Add("eee");

        foreach (String prime in list) // Loop through List with foreach
        {
            abc = prime;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        do9();
        TextBox1.Text = abc;
    }

}

Now how can I return a different list value upon the buttonclicked event? Currently it only returns 'eee'. Say I want it to return "aaa" etc instead when the button is clicked each time.

Thanks again!

James Mayson
  • 11
  • 1
  • 4
  • `list[i]` will give you item at position `i`. Example: list[0] = "aaa". http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=VS.90%29.aspx – manji May 17 '11 at 00:09

1 Answers1

1

Arraylist fell out of style with the generics that we got back in .NET 2.0. You should consider using a List<string> instead. I also suggest a shuffling algorithm like the Fisher-Yates shuffle.

I probably wouldn't do this server side at all but more likely in JavaScript. Here's Fisher-Yates shuffle in JavaScript.

EDIT

Here's one way to get a random element from a List.

private static List<string> QuoteArray = new List<string> 
    {
        "All generalizations are false, including this one.",
        "Be careful about reading health books. You may die of a misprint.",
        "If you tell the truth, you don't have to remember anything.",
        "It usually takes me more than three weeks to prepare a good impromptu speech",
        "Water, taken in moderation, cannot hurt anybody.",
        "When I was younger I could remember anything, whether it happened or not."
    };

private static int LastQuoteIndex;

and in som class somewhere:

int i = 0;
var rnd = new Random();
do
{
    i = rnd.Next(QuoteArray.Count);
}
while (i == LastQuoteIndex);

LastQuoteIndex = i;
TextBox1.Text = QuoteArray[i];
Community
  • 1
  • 1
Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106