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

Advanced
-Collapse +Expand Prism To/From
To/FromCODEGuides
-Collapse +Expand Prism Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Prism Store
PRESTWOODSTORE

Prestwood eMagazine

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

   ► KBProgrammingDelphi PrismOOP   Print This     
  From the November 2015 Issue of Prestwood eMag
 
Prism OOP:
Delphi Prism Member Field
 
Posted 11 years ago on 12/25/2008 and updated 9/4/2009
Prism Code Snippet:
 A flashcard from our Prism Flashcards Library
 A code snippet from our Prism Code Snippets Page

KB101760



General Info: Member Field

Also known as a Class Field.

A class variable defined with a specific class visibility, usually private visibility. A member property is different than a member field. A member property uses a member field to store values through accessor methods (getters and setters). For example, it is common to use a private member field to store the current value of a property. The current values of all the class member fields is the current state of the object.

Languages Focus: Member Field

What modifiers apply to member fields, if any? Typical member field modifiers include scope modifiers (private, protected, etc.) and read-only. Can you initialize the value of a member field when declared ensuring a default value?

Delphi Prism Member Field

In Prism you can set the visibility of a member field to any visibility: private, protected, public, assembly and protected or assembly or protected.

Prism supports the readonly modifier for member fields which is handy for constant like data. In this case, I chose not to preface my read-only member field with "F" so it's usage is just like a read-only property.

Prism also support the class modifier (static data) for member fields.

Delphi developers should notice the use of := to initialize a member field (in Delphi you use an =).

Syntax Example:

Cyborg = class(System.Object)

private

FSerialNumber: String:="A100";

public

FCyborgName: String;

FCyborgAge: Integer:=0;

class SeriesID: Integer:=100; readonly;

end;

A Simple Example

The following example uses our class above. Notice for the static member, you can use either an instance reference (a variable), or the type name (the class name. That's why static members are sometimes referred to as class members. Static members belong to the class and you can reference by the class name.

Note: In Prism, you can refer to static members using an instance or class type. In VB.Net you get a warning and in C# you get a compiler error. My preferred usage is to stick with the class type (the class name).

namespace CR_StaticMembers;
 
interface
 
uses
System.Drawing,
System.Collections,
System.Collections.Generic,
System.Linq,
System.Windows.Forms,
System.ComponentModel;
 
type
///
/// Summary description for MainForm.
///

MainForm = partial class(System.Windows.Forms.Form)
private
method button1_Click(sender: System.Object; e: System.EventArgs);
protected
method Dispose(disposing: Boolean); override;
public
constructor;
end;
 
  Cyborg = class(System.Object)
private
FSerialNumber: String:="A100";
public
FCyborgName: String;
FCyborgAge: Integer:=0;
 
    class SeriesID: Integer:=100; readonly;
end;
implementation
 
{$REGION Construction and Disposition}
constructor MainForm;
begin
//
// Required for Windows Form Designer support
//
InitializeComponent();
  //
// TODO: Add any constructor code after InitializeComponent call
//
end;
 
method MainForm.Dispose(disposing: Boolean);
begin
if disposing then begin
if assigned(components) then
components.Dispose();
 
    //
// TODO: Add custom disposition code here
//
end;
  inherited Dispose(disposing);
end;
{$ENDREGION}
 
method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);
begin
//Read static member field BEFORE we create our object.
//Notice the use of the class name, not an object name.
MessageBox.Show("We will now build a series " + Cyborg.SeriesID + " robot.");
 
  var MyRobot := New Cyborg;
 
  MyRobot.FCyborgName := "John";
MyRobot.FCyborgAge := 34;
  MessageBox.Show("We created a " + MyRobot.FCyborgAge + " year old robot named " + MyRobot.FCyborgName + ".");
 
  //You can refer to static members using an instance.
//(in VB.Net you get a warning, in C# you get a compiler error).
MessageBox.Show("A series " + MyRobot.SeriesID + " robot.");
 
  //However, my preferred usage is to stick with the class name.
MessageBox.Show("A series " + Cyborg.SeriesID + " robot.");
end;
 
end.

 

Note: The public member fields FCyborgName and FCyborgAge are used here for demonstration only. You normally want to make them private and access them via a member property. Also note that SeriesID starts with an uppercase "S" instead of the customary "F" because SeriesID behaves similar to a read-only properity.

More Info

Definition:  Member Field

Comments

1 Comments.
Share a thought or comment...
Comment 1 of 1

I believe in the .Net world they want member fields to start with a lower case letter.  So I would name it fSeriesID rather than FSeriesID.  Properties on the other hand they say to start with an upper case letter.

Posted 10 years ago
 
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 = P1272A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101760 Counter
12069
Since 12/25/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


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