fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: HELP ! - with syntax and lines etc.

HELP ! - with syntax and lines etc. 2 years 1 month ago #1

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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.



Although the C# code shown below is not that brilliant, it does have 5 lines as opposed to 9.



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.
Attachments:

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #2

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

Please Log in or Create an account to join the conversation.

Last edit: by Frank Maraite. Reason: Typo in Notify...

HELP ! - with syntax and lines etc. 2 years 1 month ago #3

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1406
  • Karma: 6
Hi Phil,

to save lines you can put more code on the same line even in X#:
property myProp as string
	get; return _cProp; end get
	set; _cProp := value; end set
end property

And you could use auto properties
property myProp as string auto

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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #4

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1406
  • Karma: 6
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #5

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
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #6

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:
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

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #7

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
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 :-





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.
Attachments:

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #8

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
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #9

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
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #10

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1406
  • Karma: 6
Hi Jack,

the idea about the preprocessor is great and could solve the code generation issue in an elegant manner!

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #11

  • wriedmann
  • wriedmann's Avatar
  • Away
  • Posts: 1406
  • Karma: 6
Hi Frank, hi Phil,

really my ViewModel properties look different:
property Value1 as string
  get
    return super:_Get<string>()
  end get
  set
    super:_Set<string>( value )
  end set
end property

because I have a base class with the following code from which all ViewModel classes inherit:
public abstract class BindableBase implements INotifyPropertyChanged              
  protect _properties := Dictionary<string, object>{} as Dictionary<string, object> 
  public event PropertyChanged as PropertyChangedEventHandler

protected method _Get<T>( [CallerMemberName] name := null as string ) as T
  local value := null as object

  if _properties.TryGetValue( name, out value )
    if value == null
      return Default(T)
    else
      return (T) value
    endif
  endif

  return Default(T)

protected method _Set<T>( value as T, [CallerMemberName] name := null as string ) as void

  if ( Equals( value, _Get<T>(name) ) )
    return    
  endif
  _properties[name] := value
  OnPropertyChanged( name )
        
return

protected virtual method OnPropertyChanged([CallerMemberName] propertyName := null as string ) as void
  local handler as PropertyChangedEventHandler
    	
  handler := self:PropertyChanged
  if handler != null
    handler( self, PropertyChangedEventArgs{ propertyName } )
  endif
        
  return
            
end class

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #12

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Hi Frank, and all,

Does the changed code in the attached small image meet with your full approval ?



Before the pre-processor we can probably do a lot with this !?

Any comments or further ideas ?

Regards,
Phil.
Attachments:

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #13

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Jack,

Can you explain in a little more detail please?

Because I have always been creating new and .NET apps / code, I have done little to nothing with the compiler and its switches ;-0((

TIA,
Phil.

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #14

Hi Phil,

I would omit the Self in front of _firstname. Too much to read. I think with the underscore you show us: this is a private var.

Later this day I may publish my XIDE snippet for that.
See my following reply to Jack.

Frank

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #15

Jack,

I know the power of #command and I like you showed this.
I commented in the context of Phil's question where.

I still don't like a #command solution or an auto solution. Whenever we code something we should answer the question: what is the cost of change. When doing like me or Phil now, we only add some lines of code if we want.

In case of a command: What if we need special behavior that cannot be resolved in a general manner. The same with the auto. I think it is too much work later. A snippet would do it at first place with a few key strokes. And then it's right.

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #16

Wolfgang,

this code is what is - not exactly - in MVVMlight. And it's ok to avoid being undepended from this library. But I stongly recommend to return a logic instead of a void showing changed or not. In you calling code you can ignore it if you like.

Frank

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #17

  • Phil Hepburn
  • Phil Hepburn's Avatar Topic Author
  • Offline
  • Posts: 737
  • Karma: 7
Okay Frank,

I like your suggestion AND I have tried to show the new format without the 'self:' before the 'NotifyPropertyChanged'.



I like what I now have in my code, and think that a snippet solution to my problem is probably best, and what I want.

Now then, how do I make / write / enter, and then use such a snippet in Visual Studio ?

Any ideas anyone ?
Here we go a Googling .....
Cheers,
Phil.
Attachments:

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. - /ppo 2 years 1 month ago #18

Phil,
The /ppo compiler switch just write the <source>.prg into a file <source>.ppo. If you don't set the switch the pre-processor output will only be available in memory. In essence if you don't have any defines/#command/#translate etc the .prg and the .ppo will have exactly the same content.

I in principle always have the /ppo switch set in debug mode. If you work with #command/#translate a lot then the .ppo is where you look at debugging your #command errors.

Hope this was the explanation you wanted.

Jack
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #19

Frank,

If we look back at Clipper and to a large extend VO/Vulcan, all commands were actually #command pre-processor driven.

I agree there are some cases where it might just be too much for the pre-processor to handle, but in your case of the auto, we can easily have an optional "auto" in the command if required. There are so many possibilities, my motto will be that when you see repetition investigate the possibility to create a #command to ease the amount of typing.

Jack
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

HELP ! - with syntax and lines etc. 2 years 1 month ago #20

Hi Wolfgang,

Thanks. Credit to Johan for putting me on track with the #command. Not too far in the future hopefully we will have it available in X# too.

Jack
______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
  • 2