Your full service technology partner! 
-Collapse +Expand
Search Prism Group:

-Collapse +Expand Prism To/From
-Collapse +Expand Prism Study Test
-Collapse +Expand Prism Store

Prestwood eMagazine

April Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi PrismDelphi for ....   Print This     
  From the January 2009 Issue of Prestwood eMag
Prism Delphi for .Net Archive:
Scoping in ASP.NET (Delphi) Objects
Posted 14 years ago on 5/23/2006 and updated 1/20/2009
Take Away:

Private and public variables on objects may not behave the way you'd expect, causing you to have to declare them more globally.


Scoping in ASP.NET with Delphi can be a tricky thing.  Let's look at a simple hello world program consisting of a drop-down edit box with a set of greetings ("Greetings", "Hello", "Hi", and "Goodbye", just to be silly), an edit box for the user to type in his or her name, a Button to perform the operation, and a label to display the output in.

On the SelectedIndexChanged event for the drop-down, I have placed the following code: a way of capturing what the string is every time it changes...

    sGreetingString := DropDownList(sender).Items[DropDownList(sender).SelectedIndex].ToString;

Then, in the Click event for the button, I do the following:

    lblHelloWorld.Text := sGreetingString + ' ' + ebInput.Text;

For completeness, in the Page_Load event for the form, I initialize the internal variable to match the value of the first drop-down item's text:

    sGreetingString := 'Hello';

So now, the user can choose a "greeting", type in a name, and click the button, the label will show the greeting following by a space and then the typed text.

But where do we declare the string variable?

From an OOP perspective, it seems natural to place it in the private (or protected) section of TWebForm1.  However, if we do this, it loses its value every time the button is pressed.  I believe that this is because a NEW TWebForm1 object is created each time the server is accessed, resetting the value to "Hello", rather than the user selection.

The solution to this was to declare the variable "outside" the class definition, placing it above the Event Handlers thus:

    sGreetingString : String = 'Hello';    // make sure this always matches DropDownList.Items.Item[0]

In this way, the form behaves as expected.


Share a thought or comment...
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P147A1
Enter key:
KB Post Contributed By Joshua Delahunty:
I am a software engineer with many years professional experience, using everything from 6809 assembly language to C, C++, Delphi, Paradox for Windows, and Borland JBuilder.
Visit Profile

 KB Article #100377 Counter
Since 4/2/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site:

©1995-2020 Prestwood IT Solutions.   [Security & Privacy]