Highlighted
Absent Member.
Absent Member.
2049 views

Sharing an instance of an object between classes.

Jump to solution

Hi,

I have a program consisting of 2 forms and several classes, i.e. customerclass, materialclass etc. These call an interop xfakt.dll which requires a new login for each new created instance.

What I need is to share one call with all forms and classes like:

01 xfakt.  type xfakt.app .

Method...

set xfakt to new xfakt.app()

I need to share this instance of xfakt with all other classes.

Thanks in advance.

0 Likes
2 Solutions

Accepted Solutions
Highlighted
Outstanding Contributor.
Outstanding Contributor.

Hallo Karl

You can possibly define xfakt as a static property in a class. So you can invoke it by myclass.xfakt.

Freundliche Grüsse

Werner Lanter

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

Not sure how the login requirement fits in but the usual approach is to implement the singleton pattern:

en.wikipedia.org/.../Singleton_pattern

Essentially, a class with a static method that maintains a single instance of an object. The class has a private constructor so all those that require the object must do so through the get_Instance method.

View solution in original post

0 Likes
4 Replies
Highlighted
Outstanding Contributor.
Outstanding Contributor.

Hallo Karl

You can possibly define xfakt as a static property in a class. So you can invoke it by myclass.xfakt.

Freundliche Grüsse

Werner Lanter

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

Not sure how the login requirement fits in but the usual approach is to implement the singleton pattern:

en.wikipedia.org/.../Singleton_pattern

Essentially, a class with a static method that maintains a single instance of an object. The class has a private constructor so all those that require the object must do so through the get_Instance method.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

I think this would be the way to do it. Do you have any cobol examples on how to build the singleton class and the calls to it from other classes?

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

I am attaching an example that contains a WinForms project that has a form and a couple of additional  classes including one that contains the static method that will either create a new object if required or return the current one if it already exists. This object will then be accessed in both the form and another class to show that the object is indeed the same one.

Thanks.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.