Page 1 of 3
HELP ! - with syntax and lines etc.
Posted: Tue Feb 14, 2017 11:41 am
by Phil Hepburn
Hi guys,
I need help from you over some syntax stuff that I am not very good at ;-0((
In the code I have been doing for ages, related to data binding of public properties on my defined business classes, I found myself typing lines of similar code, over and over again - many times.
First of all, do I really need to have private 'fields' behind my public properties ?
In the following image I show you how I have been coding my properties, and line 63 is crucial to the working of data binding - we MUST keep track of our changes.
- LineSeparator_01.jpg (94.69 KiB) Viewed 1587 times
Although the C# code shown below is not that brilliant, it does have 5 lines as opposed to 9.
- LineSeparator_02.jpg (46.76 KiB) Viewed 1587 times
Is there any correct way to still do what I am trying to do - AND - also save on some repeated text, typing, and screen space ?
Forgive me for being stupid - BUT - is there such a thing in X# as a line character which allows us to put multiple line code on one physical line, and have the parts separated. Sort of the reverse of the ' ; ' (semi-colon) in X#.
Can anyone suggest anything helpful ?
Fingers crossed,
Phil.
P.S. I looked in Robert's XSharp CHM and although Property details were there they did not address my issue.
Phil.
Wales, UK.
HELP ! - with syntax and lines etc.
Posted: Tue Feb 14, 2017 11:54 am
by Frank Maraite
Phil,
PUBLIC PROPERTY FirstName as STRING
GET ; RETURN _firstname ; END GET
SET ; IF _firstname != VALUE
_firstname := VALUE
NotifyPropertyChanged( "FirstName" )
ENDIF ; END SET ; END PROPERTY
PRIVATE _firstname AS STRING
if you like.
With MVVMLight I write
PUBLIC PROPERTY FirstName as STRING
GET ; RETURN _firstname ; END GET
SET ; @@SET( VALUE, REF _firstname ) ; END SET
END PROPERTY
PRIVATE _firstname AS STRING
I always have the PRIVATE right after the PROPERTY. It is always the first thing when doing refactoring: put the privates and locals where they belong. This kind of privates right after the property, locals just before there first use. Then you easily see where you can split your code into smaller methods.
Frank
HELP ! - with syntax and lines etc.
Posted: Tue Feb 14, 2017 11:54 am
by wriedmann
Hi Phil,
to save lines you can put more code on the same line even in X#:
Code: Select all
property myProp as string
get; return _cProp; end get
set; _cProp := value; end set
end property
And you could use auto properties
The main problem is the NotifyPropertyChanged() you need for databinding.
IMHO there should be an option or an attribute in the class to add such a call into an AUTO property, and I have spoken with Robert at the last conference about this.
Such an option would make X# the best language for ViewModels, even for C# programmers.
Robert promised that later such an option would be added to the language.
Currently there is a product on the market that adds such code to .NET assemblies, but unfortunately I have forgotten its name.
Wolfgang
HELP ! - with syntax and lines etc.
Posted: Tue Feb 14, 2017 12:03 pm
by wriedmann
Hi Phil,
the commercial product is PostSharp:
https://www.postsharp.net/.
And I have found an article about this problem:
http://justinangel.net/automagicallyimplementinginotifypropertychanged
Wolfgang
HELP ! - with syntax and lines etc.
Posted: Tue Feb 14, 2017 12:05 pm
by lumberjack
Phil,
Not sure it is possible in X# yet, but Robert said he is working on the pre-processor.
If the #command was supported, you can use something that Johan posted a while ago, e.g.:
PROPERTY Abc AS STRING GETSET _abc
Which would correctly translate with the following #command as he shared:
#command PROPERTY <n> AS <t> GETSET <v> =>;
PROPERTY <n> AS <t>;;
GET;;
RETURN SELF:<v>;;
END GET;;
SET;;
SELF:<v> := VALUE;;
SELF:NotifyPropertyChanged(<"n">);;
END SET;;
END PROPERTY
HTH,
Jack
HELP ! - with syntax and lines etc.
Posted: Tue Feb 14, 2017 12:17 pm
by Frank Maraite
Jack, Wolöfgang,
you raise the PropertyChangedEvent even when the property did'nt change. We should avoid this.
I should have written the complete code I use:
Code: Select all
PUBLIC PROPERTY FirstName as STRING
GET ; RETURN _firstname ; END GET
SET ; IF @@SET( VALUE, REF _firstname ) // TRUE only if VALUE != _firstname
NOP // At first no action here. The compiler should optimze this.
ENDIF
END SET
END PROPERTY
PRIVATE _firstname AS STRING
Of course I have a snippet for that. With this approach
- we say the truth: the property really has changed. No wrong, eventually time consuming or confusing reactions, at other places.
- we can react on the change here.
Otherwise we can register to this event and do something there.
If we do it in an auto way, we have to change the code later. That's not good.
Frank
HELP ! - with syntax and lines etc.
Posted: Tue Feb 14, 2017 12:32 pm
by Phil Hepburn
Okay guys, Jack, Wolfgang, Frank .....
Thank you all for helping.
I now have my first step to improving my code - BUT - Frank is right and I think I should look to improving it further.
Images attached of my recent improvements :-
- LinesSave_01.jpg (36.78 KiB) Viewed 1587 times
- LinesSave_02.jpg (31.06 KiB) Viewed 1587 times
Now then, just a thought on the notification !? Are we saying that we wish to notify the fact that the value of the property changes, or indeed that there was an action to change (or attempt to change) the property.
To my mind the two things are different, and, I wonder if we should be going for the action and not the value aspect of this.
Any thoughts and ideas ?
Jack, I like your idea of the pre-processor, and I did try something along these lines in VN when Johan posted. My worry is always about straying from the publicly available straight an narrow - and if we are to be using .NET then do so in a manner which can be put back into C# (manually) without too much problem.
I remember getting myself into all sorts of bother when using WPF controls from a pre-release 'pack' then having namespace issues when the tool became part of the standard toolbox - still hit this problem a week ago when trying to XPort one of my 38 apps from the past.
I may be too tempted by the shorthand however ;-0)
Cheers,
Phil.
HELP ! - with syntax and lines etc.
Posted: Tue Feb 14, 2017 12:36 pm
by lumberjack
Hi Frank,
I also did not do the complete required implementation, but as you stated in a #command the following can be changed as per your hint:
SELF:<v> := VALUE;;
Should read as follow in the #command:
IF SELF:<v> <> VALUE;;
SELF:<v> := VALUE;;
SELF:NotifyPropertyChanged(<"n">);;
ENDIF;;
How one implement it is dependent on the requirement. What I wanted to emphasize is the power of the pre-processor, we can have a one liner command with NO replication tailored to "auto" generate the code we need.
Hope this clears any confusion.
Jack
HELP ! - with syntax and lines etc.
Posted: Tue Feb 14, 2017 12:40 pm
by lumberjack
Hi Phil,
Well when you scared of translating to c#, you can always set the compiler switch /ppo and use the .ppo file to translate to c#, I do it all the time.
Jack
HELP ! - with syntax and lines etc.
Posted: Tue Feb 14, 2017 1:25 pm
by wriedmann
Hi Jack,
the idea about the preprocessor is great and could solve the code generation issue in an elegant manner!
Wolfgang