1. 首页
  2. 易语言 - EasyLanguage
  3. Classes
  4. GlobalDictionary: new vs. Create()

GlobalDictionary: new vs. Create()

Here is an example of two ways of instantiating a new GlobalDictionary object:

GDict = New GlobalDictionary ;

vs.

GDict = GlobalDictionary.Create() ;

Same goes for Dictionary object:

Dict = New Dictionary ;

vs.

Dict = Dictionary.Create() ;

The use of “new” versus “Create()” in the examples above work the same way. However, there is an additional “Create” method which is overloaded (i.e., it has input parameters that are required to be passed into the method when calling it). This is discussed below.

In the case of the Dictionary class, there is only 1 create method and the object’s scope is limited to the code document where it is setup and used, so there are no other considerations when creating the object – as stated before, either “new” or “Create()” can be used.

In the case of the GlobalDictionary class, the ability to share across processes was introduced and hence another method was created (see snapshot below). Notice that the use of this “Create” method has 2 input parameters, Share and name. Share is used to configure the sharing across processes (“True” will share, “False” will not share). “Name” is used to name the GlobalDictionary. The name needs to be the same in the different code documents that will used to set and get values from that particular GlobalDictionary so the code knows which GlobalDictionary to use. Obviously, this means you can have multiple GlobalDictionary objects with different names.

Currently, the GlobalDictionary cannot be used to pass objects by reference if set to share across processes. If the GlobalDictionary is to be used inside one process, objects can be shared.

Best programming practice is to use this overloaded Create method (the one with parameters) and pass in “true” for Share. That way, it will work should the data need to be shared across processes.

更新于 2020年2月27日

这篇文章对您有帮助吗?

相关的话题

留言评论