PB未公开的特性:用indirect申明对象属性

在Google论坛上看到这个帖子,介绍PB中indirect语法的用途。这个特性对提高代码可读性和面向对象特点非常有帮助,可以实现VB中的 Property Get, Property Set 属性声明语法。

 

Michael Walker 
Cascadia Software

 

 

PowerBuilder does have an unsupported feature know as indirection. Indirection allows you to define an accessor (get) and modifier (set) function where you define your instance variable. Then when a developer modifies the instance variable, the functions get called behind the scenes.

 

I can't stress enough that this is unsupported, you will not get any tech support if this causes problems, and Sybase can remove this feature at any time.

 

I do know that it works on version 4 - 7. I haven't tried it yet on version 8. Here's how it works...

 

Declare your instance variables like the following

 

Public: 
Indirect String is_Name{ of_setname( *value*), of_getname()} 


Private: 
String is_Private_Name

 

In effect what this does, is create two instance variables, one public named is_Name, and one private named is_Private_Name. Now you will need to create the of_setname and of_getname functions, like you normally would, that modify or return the value of is_Private_Name. Of course you can put any validation you want in those functions to stop certain modifications to the value. So if a developer writes the following code...

 

ObjectReference.is_Name = "Mike" //This code will call of_setname

 

and that is how it works. It's pretty cool except that it is unsupported. There were minor issues in PowerBuilder 4, where you couldn't do this in a case statement or a messagebox but I know both of those issues were fixed in PB 5 and 6. I would certainly pay extra attention when testing this area.

 

Good Luck

 

你可能感兴趣的:(PB未公开的特性:用indirect申明对象属性)