Cool idea for object New initialization defaults

Post

Posted
Rating:
#1 (In Topic #1003)
Guru
BruceSteers is in the usergroup ‘Guru’
I just had a cool idea that i thought was worth sharing for when creating a new object instance.

I added this for my _new() method…

Code (gambas)

  1.  
  2.   If Init Then
  3.     For Each sVar As Variant In Init
  4.       Object.SetProperty(Me, Init.Key, sVar)
  5.     Next
  6.  
  7.  
  8.  

the class in question is called ExternalTool.class and has these properties…
Property Text As String Use $sText
Property SaveBefore As Boolean Use $bSaveBefore
Property UseTerminal As Boolean Use $bUseTerm
Property Mode As Integer Use $bMode
Property CopyToClipboard As Boolean Use $bCopyToClip

New with that _new() method i am free to configure any and all properties upon initializing.

Like this..

Code (gambas)

  1. Dim hExt As ExternalTool
  2. hExt = New ExternalTool(["SaveBefore": True,
  3.                          "CopyToClipboard": True,
  4.                          "Mode": ExternalTool.Insert])
  5.  

That just struck me as very useful and worth sharing.
It saves adding lots of individual optional parameters to the _new() method
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
I think I like this.
What happens if you give it a property that does not exist?

Code (gambas)

  1. hExt = New ExternalTool(["SaveAfter": True,
  2.                          "CopyToClipboard": True,
  3.                          "Mode": ExternalTool.Insert])

The way I have always done this type of thing is via a  static "Create" method. i.e.

Code (gambas)

  1. Static Function Create(somedata as whatever) as ThisThing
(if that is in anyway clear?)

rgrds
b

Online now: No Back to the top
1 guest and 0 members have just viewed this.