Lately, all of my focus has been on the
Olympics so things have been a little quiet. In my
last post I talked about an
ASP.NET Random Number Generator created with
BDS 2006. Believe it or not, it is surprisingly simple to get something like
this up and running. I have worked on a number of ASP.NET applications developed with
BDS 2006, in both the internally and externally hosted environments. Typically, any SNAFUs that I have encountered (excluding code typos and incorrectness) have been due to server configuration issues and not a problem with the tool used to create the application.
To get the
Random Number Generator running I first needed a Virtual Directory on the server hosting the application. I started out with a new
ASP.NET Web Application and saved it with the same name as the application name for the Virtual Directory (by default it the name of the virtual directory).

I then created a
‘form’ and decorated it with a few items from the
Web Controls section of the
Tool Palette.

With the layout of the form in place a few lines of code were needed to get things going (I created a few functions of my own for dealing with input):
function TWebForm1.IsNumeric(str: string): Boolean;
var
code: integer;
v: integer;
begin
Val(str,v,code);
Result:= (code = 0);
end;
function TWebForm1.ToInteger(str: string): Integer;
var
code: integer;
v: integer;
begin
Val(str,v,code);
if (code = 0) then
Result:= v
else
Result:= 0;
end;
procedure TWebForm1.txtCount_TextChanged(sender: System.Object; e: System.EventArgs);
begin
if Not(IsNumeric(TextBox(Sender).Text)) then
begin
TextBox(Sender).ForeColor:= Color.Red;
TextBox(Sender).Font.Bold:= True;
TextBox(Sender).Text:= '#ERROR#';
end
else
begin
TextBox(Sender).ForeColor:= Color.Black;
TextBox(Sender).Font.Bold:= False;
end;
end;
procedure TWebForm1.btnGenerate_Click(sender: System.Object; e: System.EventArgs);
var
i, y: Integer;
z: single;
lowerbound, upperbound, counter: integer;
intRnd: Single;
// mylist: System.Collections.Hashtable;
mylist: System.Collections.SortedList;
st: System.Text.StringBuilder;
DictEntry: DictionaryEntry;
Enumerator: IEnumerator;
begin
// mylist:= Hashtable.Create;
myList:= SortedList.Create;
lbNumbers.Items.Clear;
lbStats.Items.Clear;
upperbound:= ToInteger(txtUpperBound.Text);
lowerbound:= ToInteger(txtLowerBound.Text);
counter:= ToInteger(txtCount.Text);
if (IsNumeric(txtUpperBound.Text) and
IsNumeric(txtLowerBound.Text) and
IsNumeric(txtCount.Text)) then
begin
Randomize;
For i := 1 To counter do
begin
intRnd := Int((upperbound - lowerbound + 1) * Random + lowerbound);
// HashTable for statistics
if Not mylist.ContainsKey(intRnd.ToString('#,###')) then
mylist.Add(intRnd.ToString('#,###'),'1')
else
begin
y:= ToInteger(mylist.Item[intRnd.ToString].ToString) + 1;
mylist.Item[intRnd.ToString]:= y.ToString('#,###');
end;
if (chkDupes.Checked and
(lbNumbers.Items.FindByText(intRnd.ToString('#,###')) = Nil))
or Not(chkDupes.Checked) then
lbNumbers.Items.Add(intRnd.ToString('#,###'));
end;
// enumerate hash for statistical output
Enumerator := Mylist.GetEnumerator;
st:= StringBuilder.Create;
while Enumerator.MoveNext do
begin
st.Remove(0,st.Length);
DictEntry := DictionaryEntry(Enumerator.Current);
z:= (ToInteger(DictEntry.Value.ToString)/counter * 100);
st.Append(z.ToString('00.00') + '% - ');
st.Append(DictEntry.Key.ToString + ' - (');
st.Append(DictEntry.Value.ToString + ' entries)');
lbStats.Items.Add(st.ToString);
end;
end;
end;
That’s pretty much all the code that is needed. In order to ‘manually publish’ the application, after testing the code and ensuring the application functions as desired, copy the .DLL from application’s Bin directory and the form’s .aspx file to the hosting site (if you do not have a web.config file on the published site you will need to copy that file as well).
With
BDS 2006 you can get this up and running rather quickly, I think it takes longer to write this post; unfortunately it didn’t serve up any winning numbers for me…….
Labels: .NET, ASP.NET, Code, Delphi