public static void Set(this DropDownList ddl, string findByVal)
{ // attempts to set a DDL to the 'findByVal'
try { ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(findByVal)); }
catch { }; // otherwise statys at the default, avoids errors
}
<%
' Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
'
' This work is licensed under the Creative Commons Attribution License. To view
' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
' 94305, USA.
' Returns the triangular number for a given number.
function triangularNumber(someNumber)
dim i
dim result
result = 0
for i = 1 to someNumber
result = result + i
next
triangularNumber = result
end function
%>
<%
' Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
'
' This work is licensed under the Creative Commons Attribution License. To view
' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
' 94305, USA.
sub echo(someText)
Response.Write Replace(someText, "\n", vbCrLf)
end sub
%>
<%
' Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
'
' This work is licensed under the Creative Commons Attribution License. To view
' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
' 94305, USA.
function nl2br(someText)
nl2br = Replace(someText, vbCrLf, "<br />")
end function
%>
<%
' Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
'
' This work is licensed under the Creative Commons Attribution License. To view
' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
' 94305, USA.
' Returns the stardate for a given date.
' Requires str_pad()
function stardate(someDateTime)
dim season
dim episode
dim fractime
season = cStr(Year(someDateTime) - 1947)
episode = str_pad(Round(1000 / 366 * DatePart("y", someDateTime), 0), 3, "0", "left")
fractime = left(cStr((Hour(someDateTime) * 60 + Minute(someDateTime)) / 144), 1)
stardate = season & episode & "." & fractime
end function
%>
Function TimeAgo(dtField)
days = DateDiff("d", dtField, Now)
If days > 365 Then
years = DateDiff("yyyy", dtField, Now)
If years = 1 Then
ago = "Expired " & years & " year ago"
Else
ago = "Expired " & years & " years ago"
End If
ElseIf days > 30 Then
months = DateDiff("m", dtField, Now)
If months = 1 Then
ago = "Expired " & months & " month ago"
Else
ago = "Expired " & months & " months ago"
End If
Else
ago = "Expired " & days & " days ago"
End If
TimeAgo = ago
End Function
Sub Shuffle (ByRef arrInput)
'declare local variables:
Dim arrIndices, iSize, x
Dim arrOriginal
'calculate size of given array:
iSize = UBound(arrInput)+1
'build array of random indices:
arrIndices = RandomNoDuplicates(0, iSize-1, iSize)
'copy:
arrOriginal = CopyArray(arrInput)
'shuffle:
For x=0 To UBound(arrIndices)
arrInput(x) = arrOriginal(arrIndices(x))
Next
End Sub
Function CopyArray (arr)
Dim result(), x
ReDim result(UBound(arr))
For x=0 To UBound(arr)
If IsObject(arr(x)) Then
Set result(x) = arr(x)
Else
result(x) = arr(x)
End If
Next
CopyArray = result
End Function
Function RandomNoDuplicates (iMin, iMax, iElements)
'this function will return array with "iElements" elements, each of them is random
'integer in the range "iMin"-"iMax", no duplicates.
'make sure we won't have infinite loop:
If (iMax-iMin+1)>iElements Then
Exit Function
End If
'declare local variables:
Dim RndArr(), x, curRand
Dim iCount, arrValues()
'build array of values:
Redim arrValues(iMax-iMin)
For x=iMin To iMax
arrValues(x-iMin) = x
Next
'initialize array to return:
Redim RndArr(iElements-1)
'reset:
For x=0 To UBound(RndArr)
RndArr(x) = iMin-1
Next
'initialize random numbers generator engine:
Randomize
iCount=0
'loop until the array is full:
Do Until iCount>=iElements
'create new random number:
curRand = arrValues(CLng((Rnd*(iElements-1))+1)-1)
'check if already has duplicate, put it in array if not
If Not(InArray(RndArr, curRand)) Then
RndArr(iCount)=curRand
iCount=iCount+1
End If
'maybe user gave up by now...
If Not(Response.IsClientConnected) Then
Exit Function
End If
Loop
'assign the array as return value of the function:
RandomNoDuplicates = RndArr
End Function
Function InArray(arr, val)
Dim x
InArray=True
For x=0 To UBound(arr)
If arr(x)=val Then
Exit Function
End If
Next
InArray=False
End Function
'usage:
Dim arrTest
arrTest = Array(5, 8, 10, 15, 2, 30)
Call Shuffle(arrTest)
Response.Write(Join(arrTest, "<br />"))
' This function will DEAL the "needed"
' number of values from the given "inArray"
'
' If the value for "needed" matches the
' upper bound of the "inArray", then the
' entire "inArray" is dealt out.
'
' NOTE: As written, this code never uses
' or touches element zero of the inArray
' and puts no value in element zero of the
' outArray. (Obviously, easy to change.)
'
Function Shuffle( inArray, needed )
' find out how many input elements there are...
incnt = UBound( inArray )
' then create the output array to be the size
' requested via the "needed" argument
dim outArray
redim outArray( needed )
' now we will select the number of values
' specified as "needed"...
For i = 1 To needed
' choose a random number from 1 to our
' current input array usage size...
choose = Int( incnt * Rnd(1) ) + 1
' put that chosen element into the next
' slot in the output array...
outArray( i ) = inArray( choose )
' here's the tricky part: Since we just
' used the "choose" element, we don't need
' it any more...we replace it with the last
' element of the in-use part of the array!
inArray( choose ) = inArray( incnt )
' and then we (effectively) shrink the array!
' Next time through the loop, there will be
' one fewer elements in the array to choose
' from...because we have (effectively) deleted
' the one just chosen!
incnt = incnt - 1
Next
' return the shuffled output
Shuffle = outArray
End Function
' This is just a convenience function
'
' If you need *all* the "cards" in a deck of a given
' size shuffled, and the "name" of a card can just be
' its numeric position in the unshuffled deck, then
' just call ShuffleDeck, passing the size of the deck
' to be shuffled.
'
Function ShuffleDeck( deckSize )
Dim i, deck()
ReDim deck( deckSize )
For i = 1 To deckSize
deck(i) = i
Next
ShuffleDeck = Shuffle( deck, deckSize )
End Function
%>
<HTML><BODY>
<%
Randomize
ar = Array(0,"you","can","put","anything","in","the","array","of","course")
str = Mid( Join(ar," "), 2 )
Response.Write "Picking 4 words from this list: <STRONG>" _
& str & "</STRONG><OL>" & vbNewLine
sh = Shuffle( ar, 4 )
For i = 1 to 4
Response.Write "<LI>" & sh(i) & vbNewLine
Next
Response.Write "</OL><P> <P>" & vbNewLine
Response.Write "Shuffling a 'deck' of 20 numbered cards.<BR>" _
& "The cards were originally numbered from 1 to 20.<P>" & vbNewLine
sh = ShuffleDeck( 20 )
str = Mid( Join( sh, "," ), 2 )
Response.Write "The shuffled deck: <STRONG>" & str & "</STRONG>" & vbNewLine
%>