Please try this:
local o1 := CreateObject( "PaymentService.BluetoothConnector" )
local o2 := TOleAuto():New( o1:Invoke( "GetInstance" ) )
COM does not directly support the Singleton pattern, but it does not strictly prohibit it either. It's just that there's no registry setting that says "always serves the same object." In fact, the standard COM instance mechanism requires that a truly new object be returned each time you call it (this mechanism is used internally by new operators and CreateInstance (). This means that to create a valid COM singleton, you cannot allow your clients to do it themselves all of this can be done, but it is tricky and rarely necessary.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyTest
{
public class Singleton
{
public string GetCardType()
{
return "MoneyBack";
}
public int GetCreditLimit()
{
return 15000;
}
public int GetAnnualCharge()
{
return 500;
}
public string GetCardTypeRet( string ret )
{
//Console.WriteLine("Counter Value " + ret); // due to testing from the console application ....
return ret;
}
}
//---------------------------------------------
public class Car
{
public string GetCardColorRet(string ret)
{
return ret;
}
public object MyFunction(int something = 0, string somethingelse = "zzz") // the arguments are due to future testing
{
var ret = new Singleton();
return ret;
}
public object GetObj() // the method, which I will call, internally raises the singleton instance.
{
SingletonTest fromEmployee = SingletonTest.GetInstance;
return fromEmployee;
}
}
//---------------------------------------------
public sealed class SingletonTest //
{
private static int counter = 0;
/*
* Private property initilized with null
* ensures that only one instance of the object is created
* based on the null condition
*/
private static SingletonTest instance = null;
/*
* public property is used to return only one instance of the class
* leveraging on the private property
*/
public static SingletonTest GetInstance
{
get
{
if (instance == null)
instance = new SingletonTest();
return instance;
}
}
/*
* Private constructor ensures that object is not
* instantiated other than with in the class itself
*/
private SingletonTest()
{
counter++;
//Console.WriteLine("Counter Value " + counter.ToString() + " formirao instancu Singleton"); // due to testing from the console application ....
}
/*
* Public method which can be invoked through the singleton instance !!!
*/
public void PrintDetails(string message) // due to testing from the console application ....
{
//Console.WriteLine(message);
}
// added ... test function !!!
public string GetCardTypeRet(string ret)
{
return ret;
}
public string TestConnectionDLL(string ret)
{
return ret;
}
}
}
// visible class
o1 := CREATEOBJECT("MyTest.Singleton") // namespace->MyTest public class->Singleton
alert(valtype(o1)) // "O"
alert(o1:GetCardTypeRet("Tu sam 2!!!!!!")) // -> "Tu sam 2!!!!!!"
o2 := CREATEOBJECT("MyTest.Car") //
alert(valtype(o2)) // -> "O"
alert(valtype( o2:GetObj() ) ) // -> "O"
o3 := o2:GetObj() // -> objekt - dignuta instanca
alert( o3:TestConnectionDLL("string_ret") ) // -> "string_ret" the test calls methods from a singleton instance
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 92 guests