Wednesday, November 2, 2011

Display Random Quotes


Suppose we want to display random quotes in our website. We can use .Net ArrayList class to accomplish this

Example:

-C# code-----------------------------------------------------------------------
string GenerateRandomQuotes()
{
ArrayList a1 = new ArrayList();
a1.Add("Whenever you are confronted with an opponent. Conquer him with love. --- Mahatma Gandhi");
a1.Add(
"What will tell in the end will be character and not a knowledge of letters. --- Mahatma Gandhi");
a1.Add("Nonviolence is the greatest force at the disposal of mankind. It is mightier than the mightiest weapon of destruction devised by the ingenuity of man. --- Mahatma Gandhi on nonviolence");
a1.Add(
"Live as if you were to die tomorrow...Learn as if you live forever --- Mahatma Gandhi");
a1.Add("Defeat cannot dishearten me. It can only chasten me. I know that God will guide me. Truth is superior to man s wisdom. --- Mahatma Gandhi");
a1.Add(
"You must be the change you wish to see in the World. __ Mahatma Gandhi");
a1.Add("I have nothing new to teach the world, truth and non-violence are as old as the hills. --- Mahatma Gandhi");
1.Add(
"Happiness is when what you think, what you say, and what you do are in harmony. --- Mahatma Gandhi");
a1.Add("When I despair, I remember that all through history the way of truth and love has always won. There have been tyrants and murderers and for a time they seem invincible but in the end, they always fall -- think of it, ALWAYS.. --- Mahatma Gandhi");
a1.Add(
"Fear is not a disease of the body; fear kills the soul.' --- Mahatma Gandhi");
a1.Add("The difference between what we do and what we are capable of doing would suffice to solve most of the world's problems. --- Mahatma Gandhi");
a1.Add(
"Generations to come...will scarse believe that such a one as this ever walked upon this earth.. --- Albert Einstein about Mahatma Gandhi");
// quotes can be added in array list

Random objRan = new Random();
return a1[objRan.Next(a1.Count)].ToString();
}



--VB.NET code----------------------------------------------------------------------------------------

Public Function GenerateRandomQuotes() As String

Dim col As New ArrayList
col.Add(
"Whenever you are confronted with an opponent. Conquer him with love. --- Mahatma Gandhi")

col.Add("What will tell in the end will be character and not a knowledge of letters. --- Mahatma Gandhi")
col.Add(
"Nonviolence is the greatest force at the disposal of mankind. It is mightier than the mightiest weapon of destruction devised by the ingenuity of man. --- Mahatma Gandhi on nonviolence")
col.Add("Live as if you were to die tomorrow...Learn as if you live forever --- Mahatma Gandhi")
col.Add(
"Defeat cannot dishearten me. It can only chasten me. I know that God will guide me. Truth is superior to man s wisdom. --- Mahatma Gandhi")
col.Add("You must be the change you wish to see in the World. __ Mahatma Gandhi")
col.Add(
"I have nothing new to teach the world, truth and non-violence are as old as the hills. --- Mahatma Gandhi")
col.Add("Happiness is when what you think, what you say, and what you do are in harmony. --- Mahatma Gandhi")
col.Add(
"When I despair, I remember that all through history the way of truth and love has always won. There have been tyrants and murderers and for a time they seem invincible but in the end, they always fall -- think of it, ALWAYS..' --- Mahatma Gandhi")
col.Add("Fear is not a disease of the body; fear kills the soul.' --- Mahatma Gandhi")

col.Add("The difference between what we do and what we are capable of doing would suffice to solve most of the world's problems. --- Mahatma Gandhi")
col.Add("Generations to come...will scarse believe that such a one as this ever walked upon this earth.. --- Albert Einstein about Mahatma Gandhi")
Dim objRan As New Random

GenerateRandomQuotes = CStr(col(objRan.Next(col.Count)))

End Function

No comments:

Post a Comment

Open default email app in .NET MAUI

Sample Code:  if (Email.Default.IsComposeSupported) {     string subject = "Hello!";     string body = "Excellent!";    ...