Declaring Global Variables - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Declaring Global Variables

zicheziche Posts: 47Member
How do you declare a variable in a form that can be accessed and mobified by other forms in an application?

Comments

  • softmansoftman Posts: 157Member
    Declare it in the Public section.

    public
    MyVariable : String;

    U can use the variable in the other form, if the corresponding form is in the uses list, like Form1.Myvariable := 'Hello';

    I hope that's what you need.

    SoftMan

    : How do you declare a variable in a form that can be accessed and mobified by other forms in an application?
    :
  • MasterijnMasterijn Posts: 188Member
    : Declare it in the Public section.
    :
    : public
    : MyVariable : String;
    :
    : U can use the variable in the other form, if the corresponding form is in the uses list, like Form1.Myvariable := 'Hello';
    :
    : I hope that's what you need.
    :
    : SoftMan
    :
    : : How do you declare a variable in a form that can be accessed and mobified by other forms in an application?
    : :
    :
    This is just for clarification:
    It's best to see forms as objects. Objects have from an outside point of view: properties. From the inside objects have: fields. They start normally with a F... It's a very good practice to declare the above as a property. I know that in the beginning this seems like just extra work, but later on it's purposes become very clear. This is because setting a property normally (95%) results in some extra processing. If it doesn't than the chances are high that it shouldn't be a property (variable) on that object at all.


  • dj_sanniedj_sannie Posts: 92Member
    : : Declare it in the Public section.
    : :
    : : public
    : : MyVariable : String;
    : :
    : : U can use the variable in the other form, if the corresponding form is in the uses list, like Form1.Myvariable := 'Hello';
    : :
    : : I hope that's what you need.
    : :
    : : SoftMan
    : :
    : : : How do you declare a variable in a form that can be accessed and mobified by other forms in an application?
    : : :
    : :
    : This is just for clarification:
    : It's best to see forms as objects. Objects have from an outside point of view: properties. From the inside objects have: fields. They start normally with a F... It's a very good practice to declare the above as a property. I know that in the beginning this seems like just extra work, but later on it's purposes become very clear. This is because setting a property normally (95%) results in some extra processing. If it doesn't than the chances are high that it shouldn't be a property (variable) on that object at all.
    :
    :
    :
    Hi Masterijn,

    I dont't understand you're whole post.
    In view a form is a OBject. And i know that normaly variables inside a object are declared as "Private f....: Type;" And that the can be modified as a Property by other objects. I have learned and discovered that this often is to eliminate ilegal values for the variable.

    But i'm wondering what u mean with "This is because setting a property normally (95%) results in some extra processing. If it doesn't than the chances are high that it shouldn't be a property (variable) on that object at all."

    And i don't think ziche knows how to declare a property.
    [code]
    unit Unit1;

    interface

    uses
    Forms;

    type
    TForm1 = class(TForm)
    private
    { Private declarations }
    fStringA: String;
    fStringB: String;

    procedure WriteStringB( Input: String );
    public
    { Public declarations }
    published
    { Published declarations }
    property UncheckedString: String read fStringA write fStringA;
    property CheckedString: String read fStringB write WriteStringB;
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.WriteStringB( Input: String );
    begin
    // Here u can do check on the new value for fStringB.
    fStringB := Input;
    end;

    end.
    [/code]

    Greetz DJ Sannie.
  • MasterijnMasterijn Posts: 188Member
    : : : Declare it in the Public section.
    : : :
    : : : public
    : : : MyVariable : String;
    : : :
    : : : U can use the variable in the other form, if the corresponding form is in the uses list, like Form1.Myvariable := 'Hello';
    : : :
    : : : I hope that's what you need.
    : : :
    : : : SoftMan
    : : :
    : : : : How do you declare a variable in a form that can be accessed and mobified by other forms in an application?
    : : : :
    : : :
    : : This is just for clarification:
    : : It's best to see forms as objects. Objects have from an outside point of view: properties. From the inside objects have: fields. They start normally with a F... It's a very good practice to declare the above as a property. I know that in the beginning this seems like just extra work, but later on it's purposes become very clear. This is because setting a property normally (95%) results in some extra processing. If it doesn't than the chances are high that it shouldn't be a property (variable) on that object at all.
    : :
    : :
    : :
    : Hi Masterijn,
    :
    : I dont't understand you're whole post.
    : In view a form is a OBject. And i know that normaly variables inside a object are declared as "Private f....: Type;" And that the can be modified as a Property by other objects. I have learned and discovered that this often is to eliminate ilegal values for the variable.
    :
    : But i'm wondering what u mean with "This is because setting a property normally (95%) results in some extra processing. If it doesn't than the chances are high that it shouldn't be a property (variable) on that object at all."
    :
    : And i don't think ziche knows how to declare a property.
    : [code]
    : unit Unit1;
    :
    : interface
    :
    : uses
    : Forms;
    :
    : type
    : TForm1 = class(TForm)
    : private
    : { Private declarations }
    : fUncheckedString: String;
    : fCheckedString: String;
    :
    : procedure SetUnCheckedString( Value: String );
    : procedure SetCheckedString( Value: String );
    : procedure SetChecked( Value: Boolean);
    : public
    : { Public declarations }
    procedure UpdateCaption;
    : published
    : { Published declarations }
    : property UnCheckedString: String read fUncheckedString write SetUnCheckedString;
    : property CheckedString: String read fCheckedString write SetCheckedString;
    : property Checked: Boolean read FChecked write SetChecked;
    : end;
    :
    : var
    : Form1: TForm1;
    :
    : implementation
    :
    : {$R *.dfm}
    :
    : procedure TForm1.SetUnCheckedString( Value: String );
    : begin
    : fUnCheckedString := Value;
    UpdateCaption;
    : end;
    : procedure TForm1.SetCheckedString( Value: String );
    : begin
    : fCheckedString := Value;
    UpdateCaption;
    : end;

    procedure TForm1.SetChecked( Value: Boolean);
    : begin
    fChecked := Value;
    UpdateCaption;
    : end;


    procedure TForm1.UpdateCaption;
    begin
    if fChecked then
    Caption := CheckedString
    else
    Caption := UnCheckedString;
    end;
    :
    : end.
    : [/code]
    :
    : Greetz DJ Sannie.
    :
    Thanxs for the example. I'll clarify my meanings with this question: Why would you want a property CheckedString if it doesn't change anything withing the form? Let's say that in your example it is a string for some Checked state. Then the showing of this string should be updated after changing the property. This is normally done in the setter. This is what i mean about extra processing. It's seldom (the 5%) that a property can be changed without affecting something else. The power of properties is just that when changing it's value, the object in its whole can act on it.
    B.T.W it's easer to declare properties with
    [code]
    property CheckedString: string; // Now press
    [/code]
    B.T.W it's better to leave the field naming as F+
    You're right a property also ensures the state of the object, by validating it's value.


  • zibadianzibadian Posts: 6,349Member
    : : : : Declare it in the Public section.
    : : : :
    : : : : public
    : : : : MyVariable : String;
    : : : :
    : : : : U can use the variable in the other form, if the corresponding form is in the uses list, like Form1.Myvariable := 'Hello';
    : : : :
    : : : : I hope that's what you need.
    : : : :
    : : : : SoftMan
    : : : :
    : : : : : How do you declare a variable in a form that can be accessed and mobified by other forms in an application?
    : : : : :
    : : : :
    : : : This is just for clarification:
    : : : It's best to see forms as objects. Objects have from an outside point of view: properties. From the inside objects have: fields. They start normally with a F... It's a very good practice to declare the above as a property. I know that in the beginning this seems like just extra work, but later on it's purposes become very clear. This is because setting a property normally (95%) results in some extra processing. If it doesn't than the chances are high that it shouldn't be a property (variable) on that object at all.
    : : :
    : : :
    : : :
    : : Hi Masterijn,
    : :
    : : I dont't understand you're whole post.
    : : In view a form is a OBject. And i know that normaly variables inside a object are declared as "Private f....: Type;" And that the can be modified as a Property by other objects. I have learned and discovered that this often is to eliminate ilegal values for the variable.
    : :
    : : But i'm wondering what u mean with "This is because setting a property normally (95%) results in some extra processing. If it doesn't than the chances are high that it shouldn't be a property (variable) on that object at all."
    : :
    : : And i don't think ziche knows how to declare a property.
    : : [code]
    : : unit Unit1;
    : :
    : : interface
    : :
    : : uses
    : : Forms;
    : :
    : : type
    : : TForm1 = class(TForm)
    : : private
    : : { Private declarations }
    : : fUncheckedString: String;
    : : fCheckedString: String;
    : :
    : : procedure SetUnCheckedString( Value: String );
    : : procedure SetCheckedString( Value: String );
    : : procedure SetChecked( Value: Boolean);
    : : public
    : : { Public declarations }
    : procedure UpdateCaption;
    : : published
    : : { Published declarations }
    : : property UnCheckedString: String read fUncheckedString write SetUnCheckedString;
    : : property CheckedString: String read fCheckedString write SetCheckedString;
    : : property Checked: Boolean read FChecked write SetChecked;
    : : end;
    : :
    : : var
    : : Form1: TForm1;
    : :
    : : implementation
    : :
    : : {$R *.dfm}
    : :
    : : procedure TForm1.SetUnCheckedString( Value: String );
    : : begin
    : : fUnCheckedString := Value;
    : UpdateCaption;
    : : end;
    : : procedure TForm1.SetCheckedString( Value: String );
    : : begin
    : : fCheckedString := Value;
    : UpdateCaption;
    : : end;
    :
    : procedure TForm1.SetChecked( Value: Boolean);
    : : begin
    : fChecked := Value;
    : UpdateCaption;
    : : end;
    :
    :
    : procedure TForm1.UpdateCaption;
    : begin
    : if fChecked then
    : Caption := CheckedString
    : else
    : Caption := UnCheckedString;
    : end;
    : :
    : : end.
    : : [/code]
    : :
    : : Greetz DJ Sannie.
    : :
    : Thanxs for the example. I'll clarify my meanings with this question: Why would you want a property CheckedString if it doesn't change anything withing the form? Let's say that in your example it is a string for some Checked state. Then the showing of this string should be updated after changing the property. This is normally done in the setter. This is what i mean about extra processing. It's seldom (the 5%) that a property can be changed without affecting something else. The power of properties is just that when changing it's value, the object in its whole can act on it.
    : B.T.W it's easer to declare properties with
    : [code]
    : property CheckedString: string; // Now press
    : [/code]
    : B.T.W it's better to leave the field naming as F+
    : You're right a property also ensures the state of the object, by validating it's value.
    :
    :
    :
    Another advantage of properties is that one can override them by creating a new access method for the descendant. In this case that is not a big advantage, but when writing OOP data-structures this can become vital.
Sign In or Register to comment.