defining Properties - syntax of all variations

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

defining Properties - syntax of all variations

Post by Phil Hepburn »

Hi Team and all Forum guys,

I am adding a sub-section to the 'ClickStartXSharp' eNotes on Properties and the correct syntax to use to define them. I wish to include all variations.
XSprops_01.jpg
XSprops_01.jpg (51.03 KiB) Viewed 443 times
I see from the release notes that 'auto' adds a field to support the property. What is its name ?

If I use the one line 'get set' variation, does this do the same as 'auto' - do I get a backing field?

Enough for now, more later perhaps.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

defining Properties - syntax of all variations

Post by Phil Hepburn »

whoops ! - sorry !!

seem to have chopped my last message before the end.

I have gotten the full Property definition OK I think :-
XSprops_02.jpg
XSprops_02.jpg (115.11 KiB) Viewed 443 times
Can anyone suggest other variations that I may have missed while making the two images in this post and my last one ?

Cheers,
Phil.
Wales, UK.
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

defining Properties - syntax of all variations

Post by wriedmann »

Hi Phil,

with the "auto" you get effectively a backingfield - that means the compiler creates itself the storage for the variable.

This X# code

Code: Select all

class Properties
protect _cValue			as string
	
constructor()
	
_cValue	:= ""
	
return
	
property FullForm as string
  get
    return _cValue
  end get
  set
    _cValue := value
  end set
end property

property ShortForm as string get _cValue set _cValue := value
property AutoProperty as string auto
	
end class
generates this code:
properties.png
properties.png (9.21 KiB) Viewed 443 times
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

defining Properties - syntax of all variations

Post by wriedmann »

Hi Phil,

basically I know about 3 different property formats:

the standard one:

Code: Select all

property Phil as string
  get
    return _cPhil
  end get
  set
    _cPhil := value
  end set
end property
where you can of course omit the get or the set block.

And then is the short form (which I like most)

Code: Select all

property Phil as string get _cPhil set _cPhil := value
and you can omit there also the set or the get block.

And as last there is the automatic property:

Code: Select all

property Phil as string auto
And all the modifiers like static, protect, private, public are admitted too.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

defining Properties - syntax of all variations

Post by Phil Hepburn »

Thanks a lot Wolfgang,

I will work with this in the eNotes.

What you have written adds a couple more things to what I already had found. So well done.

Regards,
Phil.
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

defining Properties - syntax of all variations

Post by wriedmann »

Hi Phil,

only to add something: in my ViewModels that implement the INotifyPropertyChanged interface, I have this form of property:

Code: Select all

public property ConfigImage as string set self:_Set<string>( value ) get self:_Get<string>()
because these inherit from my ExpandoBase class with the relative get/set methods that send also the notification.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply